Project 5 PIEZO BUZZER
Project 5 PIEZO BUZZER
In this circuit, you’ll use the MICROCONTROLLER and a small buzzer to make music, and you’ll
learn how to program your own songs using arrays.
Page | 1
Parts Needed
Grab the following quantities of each part listed to build this circuit:
NEW COMPONENTS
BUZZER: The buzzer uses a small magnetic coil to vibrate a metal disc inside a plastic
housing. By pulsing electricity through the coil at different rates, different frequencies
(pitches) of sound can be produced. Attaching a potentiometer to the output allows you
to limit the amount of current moving through the buzzer and lower its volume.
NEW CONCEPTS
RESET BUTTON: The MICROCONTROLLER has a built-in reset button. This button will reset
the board and start the code over from the beginning, running setup()then loop().
ARRAYS are used like variables, but they can store multiple values. The simplest array is just a list.
Imagine that you want to store the frequency for each note of the C major scale. We could make seven
variables and assign a frequency to each one, or we could use an array and store all seven in the same
list. To refer to a specific value in the array, an index number is used. Arrays are indexed from 0. For
example, to call the first element in the array, use array_name[0]; to call the second element, use
array_name[1]; and so on.
HOOKUP GUIDE
Page | 2
SOURCE CODE:
void setup()
Page | 3
{
pinMode(speakerPin, OUTPUT); //set the output pin for the speaker
}
void loop()
{
int currentFrequency = 0; //the frequency that we find when we look up a frequency in the arrays
int beatLength = 150; //the length of one beat (changing this will speed up or slow down the tempo of
the song)
//play the frequency that matched our letter for the number of beats passed to the play function
tone(speakerPin, currentFrequency, beats * beatLength);
delay(beats * beatLength); //wait for the length of the tone so that it has time to play
delay(50); //a little delay between the notes makes the song sound more natural