LilyPad Buzzer Hookup Guide

Contributors: Gella
Favorited Favorite 6

Making Sounds

Inside the buzzer is a coil of wire and a small magnet. When current flows through this coil, it becomes magnetized and pulls towards the magnet, which makes a tiny "click". When done thousands of times per second, the clicks create tones. We can use commands in Arduino to click the buzzer at specific frequencies, which we hear as different pitches. To create a musical note, we'll need two things: a pitch and a duration.

A tone’s pitch is what we perceive when we think of a note as being very high (screams, forks scratching plates, etc.) versus very low (like earth-rumbling bass). The pitch of a tone is very closely related to the frequency played through a speaker. If we toggle a pin from HIGH-to-LOW then LOW-to-HIGH 440 times per second, for example, it produces a 440 Hz (hertz) frequency - a “middle A” pitch. Humans can hear frequencies ranging from 20 (low-pitch, bass) to 20,000 Hz (high-pitch, “ow, my ears”).

We can also program the duration of a tone - the length of time a pitch is played. In our program, we’ll use the delay function to set the duration. Playing a tone with Arduino is very easy. Just give it a pitch, and it will start toggling the output pin for you. Much like analog output, you can set it and forget it; the tone won’t stop playing until you tell it to.

- excerpt from The Digital Sandbox Arduino Companion

Playing Notes

Upload the following code to your LilyPad Arduino, making sure to select the correct LilyPad board from the drop down menu below. The LilyPad Arduino Simple, LilyPad Arduino, and LilyPad Development Board, and Development Board Simple all use a LilyPad ATmega 328. Choose LilyPad Arduino USB if using a LilyPad Arduino USB.

Don't forget to select the Serial Port that your LilyPad is connected to.

If prototyping with a LilyPad Development Board Simple, change buzzerPin to 9.
If prototyping with a LilyPad Development Board, change buzzerPin to 7.

Note: This example assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE.

If you have not previously installed an Arduino library, please check out our installation guide.


LilyPad Buzzer Example
SparkFun Electronics

This example code shows how to hook up a LilyPad Buzzer to play a simple song 
using the tone() function and setting variables for each note.

Buzzer connections:
   * + pin to 5
   * - to -

// Which pin the buzzer is attached to
int buzzerPin = 5;

// Delay in milliseconds
int delayTime = 500; 

// Notes and their frequencies
const int C = 1046;
const int D = 1175;
const int E = 1319;
const int F = 1397;
const int G = 1568;
const int A = 1760;
const int B = 1976;
const int C1 = 2093;
const int D1 = 2349;

void setup()
    // Set the buzzer pin as an OUTPUT
    pinMode(buzzerPin, OUTPUT);

void loop()
  // Use the tone() function to play each note in a scale
  tone(buzzerPin, C);
  tone(buzzerPin, D);
  tone(buzzerPin, E);
  tone(buzzerPin, F);
  tone(buzzerPin, G);
  tone(buzzerPin, A);
  tone(buzzerPin, B);
  tone(buzzerPin, C1);
  // Use noTone() to shut off the buzzer and delay to create a 'rest'

Upload this code to your LilyPad Arduino and listen - the code plays a scale. To make the notes, we give the tone function two pieces of information - the pin the buzzer is attached to and the frequency we want to play -tone(pin, frequency). To make a note last a certain amount of time, we use a delay() in between notes. At the top of the sketch we created variables for musical notes with the frequency in hertz. To make a pause or rest, we can use the noTone() function followed by a delay.

Try using the tone() and noTone() functions to compose a simple song. One drawback of this code is that the sounds never stop. Next we'll learn how to trigger sounds with an input so they are not constantly playing.