LilyPad Buzzer Hookup Guide
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.
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
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.language:c
/******************************************************************************
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);
delay(delayTime);
tone(buzzerPin, D);
delay(delayTime);
tone(buzzerPin, E);
delay(delayTime);
tone(buzzerPin, F);
delay(delayTime);
tone(buzzerPin, G);
delay(delayTime);
tone(buzzerPin, A);
delay(delayTime);
tone(buzzerPin, B);
delay(delayTime);
tone(buzzerPin, C1);
delay(delayTime);
// Use noTone() to shut off the buzzer and delay to create a 'rest'
noTone(buzzerPin);
delay(delayTime);
}
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.
When prototyping with the buzzer, sounds can get quite annoying, especially in large groups. Rather than unplugging or powering down your LilyPad Arduino to stop the sounds, we recommend unclipping an alligator clip from one side of the buzzer to quickly quiet it. For projects sewn together with conductive thread, stitch a LilyPad Slide Switch between the LilyPad Arduino pin and the positive side of the buzzer as a way to quickly toggle the sound ON/OFF while letting the rest of the code run.