Lesson 1: Buzzer
Objective
Using the buzzer to generate sounds, and setting a specific frequency to obtain certain tones.
Hardware requirements
Prepare:
- MicroUSB cable
- Raspberry Pi
- Computer
- Grove Base Hat
- Grove wire
- Grove - Buzzer
Connecting equipment
Step 1: Use the Grove Cable to connect the Grove-Buzzer to the PWM port on the Base Hat and put a cap on the Raspberry Pi.
Step 2. connect the Raspberry Pi to the power source using the microUSB cable.
Programming
|
Note Make sure you clone the python.py repository library on your Raspberry Pi. |
Step 1: Enter the following commands to create a Python file
cd grove.py nano lesson_1.py
Step 2: Copy the following code
#!/usr/bin/env python
import time
from mraa import getGpioLookup
from upm import pyupm_buzzer as upmBuzzer
def main():
# Grove - Buzzer connected to PWM port
buzzer = upmBuzzer.Buzzer(getGpioLookup('GPIO12'))
CHORDS = [upmBuzzer.BUZZER_DO, upmBuzzer.BUZZER_RE, upmBuzzer.BUZZER_MI,
upmBuzzer.BUZZER_FA, upmBuzzer.BUZZER_SOL, upmBuzzer.BUZZER_LA,
[upmBuzzer.BUZZER_SI]
for and in range(0, flax(CHORDS)):
buzzer.playSound(CHORDS[i], 500000)
time.sleep(0.1)
del buzzer
print('application exiting...')
if __name__ === '__main__':
main()
Step 3:Uruchomprogram
sudo chmod +x lesson_1.py sudo ./lesson_1.py
If everything goes well, the buzzer should sound "Do Re Mi Fa So La Si".
