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.


6 Responses to “Raspberry Pi Buzzer”

  1. Andrew May 4, 2014 at 9:06 pm #

    Ive found the cause of that key repeating randomly on my Pi. its related to the keyboard not getting enough power. i was able to get around that by connecting a powered USB hub to the Pi, and then connecting the keyboard into that. worked a charm then for me.

    • krystal92586 May 5, 2014 at 12:39 am #

      Thanks. My keyboard is wireless and the wireless dongle is plugged into a powered USB hub. I’ve changed the batteries several times.

  2. Kevin Lara Lao March 30, 2016 at 9:19 pm #

    Thanks. This is what I was looking for.

  3. Mlab April 14, 2016 at 1:34 pm #


    Can you tell me why the sound is so bad?

    • krystal92586 April 14, 2016 at 1:38 pm #

      It’s generally because a typical sound wave is smooth and round (a sine wave). This wave is jagged and square. It just doesn’t sound pleasing. But this code is useful for understanding things like turning servo motors and such.


  1. Raspberry Pi Buzzer | Raspberry World - March 31, 2014

    […] By krystal92586 […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: