Introduction: Digital Music Using a Piezo Buzzer

Piezo buzzers are used for making beeps, tones and alerts. This cheap electronic component are generally capable of producing frequencies around 2KHz to 10KHz. These tiny little buzzers are commonly used in a wide array of applications, from residential to industrial applications, mainly as 'beepers' in security alarms and timers.

This project is about the use of a Piezo-element buzzer with an Arduino, as well as the coding needed to drive a Piezo buzzer to produce different tones and digital sound or alerts.

Step 1: Tools and Materials

  • Arduino Uno
  • Piezo Buzzer
  • Breadboard
  • Jumper Cables

Step 2: Connecting the Buzzer to the Arduino

Connecting the buzzer to the Arduino is very straight forward and can be done with as easy as two connections.
Though, be wary of where the pin is aligned to in the breadboard, since the piezo buzzer's outer surface is so large it is difficult to see.

  1. Connect one of the buzzer pins to ground.
  2. Connect the remaining pin to the digital pin 9 of the Arduino.

Voila, the circuit is complete!

Step 3: Coding

const int buzzerPin = 9;
// We'll set up an array with the notes we want to play
// change these values to make different songs!
// Length must equal the total number of notes and spaces 
const int songLength = 18;
// Notes is an array of text characters corresponding to the notes
// in your song. A space represents a rest (no tone)
char notes[] = "cdfda ag cdfdg gf "; // a space represents a rest
// Beats is an array of values for each note and rest.
// A "1" represents a quarter-note, 2 a half-note, etc.
// Don't forget that the rests (spaces) need a length as well.
int beats[] = {1,1,1,1,1,1,4,4,2,1,1,1,1,1,1,4,4,2};
// The tempo is how fast to play the song.
// To make the song play faster, decrease this value.
int tempo = 113;
void setup() 
  pinMode(buzzerPin, OUTPUT);
void loop() 
  int i, duration;
  for (i = 0; i < songLength; i++) // step through the song arrays
    duration = beats[i] * tempo;  // length of note/rest in ms
    if (notes[i] == ' ')          // is this a rest? 
      delay(duration);            // then pause for a moment
    else                          // otherwise, play the note
      tone(buzzerPin, frequency(notes[i]), duration);
      delay(duration);            // wait for tone to finish
    delay(tempo/10);              // brief pause between notes
  // We only want to play the song once, so we'll pause forever:
  // If you'd like your song to play over and over,
  // remove the above statement
int frequency(char note) 
  // This function takes a note character (a-g), and returns the
  // corresponding frequency in Hz for the tone() function.
  int i;
  const int numNotes = 8;  // number of notes we're storing
  // The following arrays hold the note characters and their
  // corresponding frequencies. The last "C" note is uppercase
  // to separate it from the first lowercase "c". If you want to
  // add more notes, you'll need to use unique characters.
  // For the "char" (character) type, we put single characters
  // in single quotes.
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523};
  // Now we'll search through the letters in the array, and if
  // we find it, we'll return the frequency for that note.
  for (i = 0; i < numNotes; i++)  // Step through the notes
    if (names[i] == note)         // Is this the one?
      return(frequencies[i]);     // Yes! Return the frequency
  return(0);  // We looked through everything and didn't find it,
              // but we still need to return a value, so return 0.

Step 4: Buzzer Sound Demo

Try to change the code and produce different sounds with different orders of tones and frequencies. Create your own music with a buzzer.
Stay creative!


About This Instructable




More by JeanC117:Sukiyaki!Chinese Meatball Soup With Napa CabbageTonkatsu (Deep Fried Pork)
Add instructable to: