Sponsor Link:

UTSource.net Reviews

It is a trustworthy website for ordering electronic components with cheap price and excellent quality.

Mounting the circuit

To start making the circuit, first, connect the power (positive/negative) wires of your piezo buzzer to separate rows of your breadboard, as shown in the circuit diagram above.  Then, attach one end of your 220Ω resistor to the same row as the positive (+/red) wire of your piezo buzzer and connect the other end of the resistor to the row next to it. Insert one of your jumper wires to the same row as the ending pin of your resistor (the pin connected to its own row) and connect it to D1 (digital pin 1) of your Arduino. Now, insert another jumper wire to the same row as your negative (-/black) wire on your breadboard and connect that wire to GND (-) on your Arduino. If everything is done correctly, it should look similar to the circuit diagram above. Now, onto the code as tagged to the left!

Arduino Christmas Piezo Buzzer Project Code

int buzzerPin = 8;
int tempo = 200;
char notes[] = "eeeeeeegcde fffffeeeeddedg";
int duration[] = {1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2};

void playTheTone(char note, int duration) {
  char notesName[] = { 'c', 'd', 'e', 'f', 'g' };
  int tones[] = { 261, 293, 329, 349, 392 };

  for (int i = 0; i <= sizeof(tones); i++) {
    if (note == notesName[i]) {
      tone(buzzerPin, tones[i], duration);
    }
  }
}

void setup() {
  pinMode(buzzerPin, OUTPUT);
}

void loop() {
  for (int i = 0; i <= sizeof(notes)-1; i++) {
    if (notes[i] == ' ') {
      delay(duration[i] * tempo);
    } else {
      playTheTone(notes[i], duration[i] * tempo);
    }
    delay((tempo*2)*duration[i]);
  }
}

Arduino Christmas Piezo Buzzer Project Audio

About the code

Firstly, the code starts off with declaring that variable buzzerPin, for the buzzer, is connected to D8 (digital pin 8). We also declare the variable tempo, the speed of the melody, is 200 beats per minutes. These two variables act as integer variables. In the third line, we have a char datatype,  notes, that include the consecutive order of each musical note being played. There is another variable after that,  duration, which just states the seconds played for each note in datatype notes. In the next section, we have a void statement,  playTheTone, and this section is in charge of combining the variables and datatypes we have set in the start, to create the melody for the buzzer. We start off with making a char datatype,  notesName, which just declares the different notes we will be using and we make another variable,  tones, stating the specific tone values for the variety of notes as stated by datatype char. You can refer to this page, here, for a list of musical notes and each specific tone in the Arduino programming language.  Next up, we have a for loop, where if variable (starts with 0 value) is less than the size (bytes) of datatype notes,  the value of will increase by 1, and an if statement will be ran. The if statement states that when a note equals to the specific note name in datatype notesName, which is pointed out by variable I's value,  function tone() will be activated. This function indicates the variable of pin hooked up to the buzzer,  buzzerPin, the different frequencies of notes being played, specified in datatype tones, and the duration, indicated by variable duration. Further down,  a void setup statement appears where it just states that buzzerPin (D8) is an output pin, where information is being sent from the Arduino and to the buzzer. For the void loop, we start with an for statement, stating when variable I (starts with 0 value) is less than the size (bytes) datatype notes minus...

Read more »