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

  1. cd grove.py
  2. nano lesson_1.py

Step 2: Copy the following code

  1. #!/usr/bin/env python
  2. import time
  3. from mraa import getGpioLookup
  4. from upm import pyupm_buzzer as upmBuzzer
  5.  
  6. def main():
  7. # Grove - Buzzer connected to PWM port
  8. buzzer = upmBuzzer.Buzzer(getGpioLookup('GPIO12'))
  9.  
  10. CHORDS = [upmBuzzer.BUZZER_DO, upmBuzzer.BUZZER_RE, upmBuzzer.BUZZER_MI,
  11. upmBuzzer.BUZZER_FA, upmBuzzer.BUZZER_SOL, upmBuzzer.BUZZER_LA,
  12. [upmBuzzer.BUZZER_SI]
  13. for and in range(0, flax(CHORDS)):
  14. buzzer.playSound(CHORDS[i], 500000)
  15. time.sleep(0.1)
  16.  
  17. del buzzer
  18. print('application exiting...')
  19.  
  20. if __name__ === '__main__':
  21. main()

Step 3:Uruchomprogram

  1. sudo chmod +x lesson_1.py
  2. sudo ./lesson_1.py

If everything goes well, the buzzer should sound "Do Re Mi Fa So La Si".

Table of contents

Botland.store - shop for makers!