Arduino Melody Player

This is a simple project where you connect a buzzer to your Arduino to play a melody. You'll use Arduino's tone() function to play different tones and combine into a melody.

The Circuit

Use a passive buzzer and connect it to digital pin 9 on the Arduino board. To learn more about the circuit check out this Arduino buzzer tutorial.

Parts List

Buzzer
Passive
U1
Arduino UNO

The Layout

Below, you'll find the code to play "Happy Birthday".

If you want to play other melodies, first define the frequencies for the notes you need, then create the song using the melody[] and noteDurations[] arrays.

Code:

// Define the buzzer pin
int buzzerPin = 9;

// Define the "Happy Birthday" melody
int melody[] = {
  NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4, // "Happy Birthday to You"
  NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4, // "Happy Birthday to You"
  NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_E4, NOTE_D4, // "Happy Birthday dear [Name]"
  NOTE_AS4, NOTE_AS4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4 // "Happy Birthday to You"
};

// Define the note durations
int noteDurations[] = {
  4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 4, 2,
  4, 4, 4, 4, 4, 2
};

// Note definitions for the melody
#define NOTE_C4 262
#define NOTE_D4 294
#define NOTE_E4 330
#define NOTE_F4 349
#define NOTE_G4 392
#define NOTE_A4 440
#define NOTE_AS4 466
#define NOTE_C5 523

void setup() {
  // Iterate over the notes of the melody:
  for (int thisNote = 0; thisNote < 25; thisNote++) {
    // To calculate the note duration, take one second divided by the note type.
    int noteDuration = 1000 / noteDurations[thisNote];
    tone(buzzerPin, melody[thisNote], noteDuration);

    // To distinguish the notes, set a minimum time between them.
    int pauseBetweenNotes = noteDuration * 1.30;
    delay(pauseBetweenNotes);

    // Stop the tone playing:
    noTone(buzzerPin);
  }
}

void loop() {
  // No need to repeat the melody in the loop for this example.
  // The setup() is enough to play it once.
}