Archive | March, 2014

Raspberry Pi Buzzer

31 Mar

Raspberry Pi Buzzer


I hadn’t done anything with my Raspberry Pi in a while.  Homework has been taking a long time, the County Science Fair is in 2 days, and I’ve been working on improving my computer programming skills.  Next week is Spring Break, so I decided to work on a project.  I have a copy of The Raspberry Pi Cookbook, a book with some great projects in it.  I decided to do the “Make a Buzzing Sound” activity.


In this activity, you connect a piezo speaker to pin 18 (and the other wire to ground) on the Pi.  Then a Python script is used to make the speaker vibrate.  I didn’t have a piezo speaker, so I took a regular magnetic speaker out of an old toy that I had laying around to see if that would work.


I used a female/female hookup wire to connect the speaker to the GPIO pins and typed in the python code.  My stupid keyboard randomly repeats letters when typing into a terminal window or Idle, but not anywhere else, so this itself was a challenge.  Here’s the code from the book and then I’ll show you how I cleaned it up.


(Sorry, the indenting doesn’t seem to be working in WordPress)

import RPi.GPIO as GPIO   #import the GPIO library

import time                          #import the time library

buzzer_pin = 18                   #set the buzzer pin variable to number 18

GPIO.setmode(GPIO.BCM)#Use the Broadcom method for naming the GPIO pins

GPIO.setup(buzzer_pin, GPIO.OUT)  #Set pin 18 as an output pin

def buzz(pitch, duration):   #create the function “buzz” and feed it the pitch and duration)

period = 1.0 / pitch     #in physics, the period (sec/cyc) is the inverse of the frequency (cyc/sec)

delay = period / 2     #calcuate the time for half of the wave

cycles = int(duration * pitch)   #the number of waves to produce is the duration times the frequency

for i in range(cycles):    #start a loop from 0 to the variable “cycles” calculated above

GPIO.output(buzzer_pin, True)   #set pin 18 to high

time.sleep(delay)    #wait with pin 18 high

GPIO.output(buzzer_pin, False)    #set pin 18 to low

time.sleep(delay)    #wait with pin 18 low

while True:    #start infinite loop

pitch_s = raw_input(“Enter Pitch (200 to 2000): “)   #ask the user to type in the pitch

pitch = float(pitch_s)    #convert user input to a floating decimal

duration_s = raw_input(“Enter Duration (seconds): “)  #ask the user to type in the duration

duration = float(duration_s)  #convert user input to a floating decimal

buzz(pitch, duration)  #feed the pitch and duration to the function, “buzz”


Since I was having trouble with my keyboard, there were a couple of places where I cleaned up the code to simplify.


I changed these lines:

buzzer_pin = 18

GPIO.setup(buzzer_pin, GPIO.OUT)


To this:

GPIO.setup(18, GPIO.OUT)


I also changed these lines:

pitch_s = raw_input(“Enter Pitch (200 to 2000): “)

pitch = float(pitch_s)


To this:

Pitch = float(raw_inut(“Enter Pitch: “))


Below is the video of the program running.  It wasn’t perfect.  The sound was pretty choppy when I got to the higher frequencies.  Also, the higher frequency (pitch) sounds lasted much longer than the duration that I typed in.


Now, I can use this as a sound effect in a video game.  I could build a little speaker into a video game controller like the Wiimote.  It could be part of an alarm system.  There are many possible uses for this simple buzzer.