Introduction: Happy Birthday Tune With Arduino Uno Code

About: Arduion Projects, IoT and Electronics

Happy Birthday Music Using Arduino

In this arduino project I will show you how to connect speaker with arduino board

and Happy Birthday with arduino code

Step 1: Required Components

Happy Birthday Music using Arduino Uno

Required components for this project:

  • Arduino uno
  • 8 ohms Speaker/Buzzer
  • Bc547 Transistor
  • Breadboard and Jumper Wires

My previous Arduino projects, using speaker and buzzer

Astronomia Coffin Dance Meme Music Using Arduino uno

Multiple Leds With Arduino Uno | Led Chaser

Fire alarm system using arduino and IR sensor

Step 2: Wiring Connections

Here I have used Speker with BC547 transistor,

this transistor used as amplifier to increase the volume of speaker

Connect speaker to 8th pin of arduino uno


Step 3: Code

Happy Birthday Arduino Code

Copy and paste it to Arduino IDE

int speakerPin = 9; // Buzzer pin
int length = 28; // the number of notes
char notes[] = "GGAGcB GGAGdc GGxecBA yyecdc";
int beats[] = {2,2,8,8,8,16,1,2,2,8,8,8,16,1,2,2,8,8,8,8,16,1,2,2,8,8,8,16};
int tempo = 200;// time delay between notes 

void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
   digitalWrite(speakerPin, HIGH);
   delayMicroseconds(tone);
   digitalWrite(speakerPin, LOW);
   delayMicroseconds(tone);
}
}

void playNote(char note, int duration) {
char names[] = {'C', 'D', 'E', 'F', 'G', 'A', 'B',           

                 'c', 'd', 'e', 'f', 'g', 'a', 'b',

                 'x', 'y' };

int tones[] = { 1915, 1700, 1519, 1432, 1275, 1136, 1014,

                 956,  834,  765,  593,  468,  346,  224,

                 655 , 715 };

int SPEE = 5;

// play the tone corresponding to the note name

for (int i = 0; i < 17; i++) {

   if (names[i] == note) {
    int newduration = duration/SPEE;
     playTone(tones[i], newduration);
   }
}
}

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

void loop() {
for (int i = 0; i < length; i++) {
   if (notes[i] == ' ') {
     delay(beats[i] * tempo); // delay between notes
   } else {
     playNote(notes[i], beats[i] * tempo);
   }
   // time delay between notes
   delay(tempo);
}
}

Step 4: Final Testing

Final Testing is shown in the video,

Arduino Contest 2020

Participated in the
Arduino Contest 2020