You want to make a buzzing sound with the Raspberry Pi.
Use a piezo-electric buzzer connected to a GPIO pin.
Most small piezo buzzers work just fine using the arrangement shown in Figure 9-3. The one I used is an Adafruit supplied component (see “Miscellaneous” on page 382). You can connect the buzzer pins directly to the Raspberry Pi using female-to-female headers (see “Prototyping Equipment” on page 380).
These buzzers use very little current. However, if you have a large buzzer or just want to play it safe, then put a 470Ω resistor between the GPIO pin and the buzzer lead.
Paste the following code into the IDLE (Recipe 5.2) or nano (Recipe 3.6) editors. Save the file as buzzer.py. You can also download the program from the Downloads section of the Rasperry Pi Cookbook website.
import RPi.GPIO as GPIO import time buzzer_pin = 18 GPIO.setmode(GPIO.BCM) GPIO.setup(buzzer_pin, GPIO.OUT) def buzz(pitch, duration): period = 1.0 / pitch delay = period / 2 cycles = int(duration * pitch) for i in range(cycles): GPIO.output(buzzer_pin, True) time.sleep(delay) GPIO.output(buzzer_pin, False) time.sleep(delay) while True: pitch_s = raw_input(“Enter Pitch (200 to 2000): “) pitch = float(pitch_s) duration_s = raw_input(“Enter Duration (seconds): “) duration = float(duration_s) |
When you run the program, it will first prompt you for the pitch in Hz and then the duration of the buzz in seconds:
$ sudo python buzzer.py Enter Pitch (2000 to 10000): 2000 Enter Duration (seconds): 20 |
Discussion
Piezo buzzers don’t have a wide range of frequencies, nor is the sound quality remotely good. However, you can vary the pitch a little. The frequency generated by the code is very approximate.
The program works by simply toggling the GPIO pin 18 on and off with a short delay in between. The delay is calculated from the pitch. The higher the pitch (frequency), the shorter the delay needs to be.
See Also
You can find the datasheet for the piezo buzzer here: http://bit.ly/Iwkv2R.
Table A-9. Miscellaneous | |
1200mAh LiPo battery | Adafruit: 258 |
5V relay | SparkFun: COM-00100 |
5V panel meter | SparkFun: TOL-10285 |
Servo motor | SparkFun: ROB-09065, Adafruit: 1449 |
5V 1A power supply | Adafruit: 276 |
Low power 6V DC motor | Adafruit: 711 |
0.1 inch header pins | SparkFun: PRT-00116, Adafruit: 392 |
5 V 5-pin unipolar stepper motor | Adafruit: 858 |
12 V, 4-pin bipolar stepper motor | Adafruit: 324 |
Magician chassis with gearmotors | SparkFun: ROB-10825 |
Tactile push switch | SparkFun: COM-00097, Adafruit: 504 |
Miniature slide switch | SparkFun: COM-09609, Adafruit: 805 |
Rotary encoder | Adafruit: 377 |
4×3 keypad | SparkFun: COM-08653 |
Piezo buzzer | SparkFun: COM-07950, Adafruit: 160 |
Table A-3. Prototyping equipment | |
Description | Suppliers |
M-M jumper wires | SparkFun: PRT-08431, Adafruit: 759 |
M-F jumper wires | SparkFun: PRT-09140, Adafruit: 825 |
F-F jumper wires | SparkFun: PRT-08430, Adafruit: 794 |
Half-sized breadboard | SparkFun: PRT-09567 Adafruit: 64 |
Pi Cobbler | Adafruit: 1105 |