Explore Upcoming Workshops Near You and Ignite Your Passion for Innovation . Reserve a Seat today!

tect-corner-img

Happy Birthday Song Using Arduino and Embedded Learner Board

Birthdays are always a special occasion. If you want to make your loved one’s special day a unique one then Moonpreneur has brought you a video that will teach you how to make a machine sing the happy birthday song using Arduino and display their names on an LCD display!

To build this project we will be using an Embedded Learner Board (a robotics learning kit from Moonpreneur) and with the help of Arduino, we will play multiple tunes by precisely coding for each of them.

Use the following code to create this Arduino music project:

Connections

ELB v2 Arduino UNO

+5V

5V

GND

GND

D7

7

D6

6

D5

5

D4

4

EN

3

RS

2

SW1

8

BUZZER

9

 

Birthday Melody

Code:

/*
ELB-v2   UNO
+5V    – 5V
GND    – GND
D7     – 7
D6     – 6
D5     – 5
D4     – 4
EN     – 3
RS     – 2
SW1    – 8
BUZZ.  – 9
*/
#include <LiquidCrystal.h>
const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
const int SW1 = 8; //push button on D8
const int buzzer = 9; // Buzzer on D9

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 = 150;
void playTone(int tone, int duration) {
for (long i = 0; i < duration * 1000L; i += tone * 2) {
  digitalWrite(buzzer, HIGH);
  delayMicroseconds(tone);
  digitalWrite(buzzer, 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,

                956834765593468346224,

                655 , 715 };

int SPEED = 5;

// play the tone corresponding to the note name

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

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

void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(SW1, INPUT);
  lcd.begin(16, 2);
  lcd.setCursor(1,0);
  lcd.print(“HAPPY BIRTHDAY”);
  lcd.setCursor(4,1);
  lcd.print(“TO YOU”);
}

void loop() {
  bool buttonState =digitalRead(SW1);
  delay(100);
  if(buttonState == LOW){
    delay(50);
    for (int i = 0; i < length; i++) {
        if (notes[i] == ‘ ‘) {
        delay(beats[i] * tempo); // rest
  }
        else {
            playNote(notes[i], beats[i] * tempo);
  }
  // pause between notes
  delay(tempo);
}
}
}

Other Exciting Tone:

Harry Potter

Code:

/*
ELB-v2   UNO
+5V    – 5V
GND    – GND
D7     – 7
D6     – 6
D5     – 5
D4     – 4
EN     – 3
RS     – 2
SW1    – 8
BUZZ.  – 9
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

const int SW1 = 8; //push button on D8
const int buzzer = 9; // Buzzer on D9

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define REST 0

// change this to make the song slower or faster
int tempo = 144;

int melody[] = {
  // Hedwig’s theme fromn the Harry Potter Movies
  // Socre from https://musescore.com/user/3811306/scores/4906610
 
  REST, 2, NOTE_D4, 4,
  NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4,
  NOTE_G4, 2, NOTE_D5, 4,
  NOTE_C5, -2,
  NOTE_A4, -2,
  NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4,
  NOTE_F4, 2, NOTE_GS4, 4,
  NOTE_D4, -1,
  NOTE_D4, 4,

  NOTE_G4, -4, NOTE_AS4, 8, NOTE_A4, 4, //10
  NOTE_G4, 2, NOTE_D5, 4,
  NOTE_F5, 2, NOTE_E5, 4,
  NOTE_DS5, 2, NOTE_B4, 4,
  NOTE_DS5, -4, NOTE_D5, 8, NOTE_CS5, 4,
  NOTE_CS4, 2, NOTE_B4, 4,
  NOTE_G4, -1,
  NOTE_AS4, 4,
   
  NOTE_D5, 2, NOTE_AS4, 4,//18
  NOTE_D5, 2, NOTE_AS4, 4,
  NOTE_DS5, 2, NOTE_D5, 4,
  NOTE_CS5, 2, NOTE_A4, 4,
  NOTE_AS4, -4, NOTE_D5, 8, NOTE_CS5, 4,
  NOTE_CS4, 2, NOTE_D4, 4,
  NOTE_D5, -1,
  REST,4, NOTE_AS4,4

  NOTE_D5, 2, NOTE_AS4, 4,//26
  NOTE_D5, 2, NOTE_AS4, 4,
  NOTE_F5, 2, NOTE_E5, 4,
  NOTE_DS5, 2, NOTE_B4, 4,
  NOTE_DS5, -4, NOTE_D5, 8, NOTE_CS5, 4,
  NOTE_CS4, 2, NOTE_AS4, 4,
  NOTE_G4, -1,
  };

int notes = sizeof(melody) / sizeof(melody[0]) / 2;
int wholenote = (60000 * 4) / tempo;
int divider = 0, noteDuration = 0;

void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(SW1, INPUT);
  lcd.begin(16, 2);
  lcd.setCursor(4,0);
  lcd.print(“Expecto”);
  lcd.setCursor(4,1);
  lcd.print(“Patronum”);
//to print in single line uncomment the block below
/*  lcd.setCursor(0,0);
  lcd.print(“Expecto Patronum”);
*/
}
void loop() {
  if(digitalRead(SW1)==LOW){
  for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {

    divider = melody[thisNote + 1];
    if (divider > 0) {
      noteDuration = (wholenote) / divider;
    } else if (divider < 0) {
      noteDuration = (wholenote) / abs(divider);
      noteDuration *= 1.5; // increases the duration in half for dotted notes
    }
   
    tone(buzzer, melody[thisNote], noteDuration*0.9);
    delay(noteDuration);
    noTone(buzzer);
  }
}
}

Games of Thrones

Code:

/*
ELB-v2   UNO
+5V    – 5V
GND    – GND
D7     – 7
D6     – 6
D5     – 5
D4     – 4
EN     – 3
RS     – 2
SW1    – 8
BUZZ.  – 9
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

const int SW1 = 8; //push button on D8
const int buzzer = 9; // Buzzer on D9

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978

void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(SW1, INPUT);
  lcd.begin(16, 2);
  lcd.setCursor(0,0);
  lcd.print(“Winter Is Coming”);
//To print Winter Is Coming in two lines uncooment this block
/* lcd.setCursor(0,0);
  lcd.print(“Winter Is”);
  lcd.setCursor(0,1);
  lcd.print(“Coming”);
*/
}

void loop() {
  if(digitalRead(SW1)==LOW){
    GameOfThrones();
}
}

void GameOfThrones() {
  for(int i=0; i<4; i++) {
    tone(buzzer, NOTE_G4);
    delay(500);
    noTone(buzzer);
    tone(buzzer, NOTE_C4);
    delay(500);
    noTone(buzzer);
    tone(buzzer, NOTE_DS4);
    delay(250);
    noTone(buzzer);

    tone(buzzer, NOTE_F4);
    delay(250);
    noTone(buzzer);
  }

  for(int i=0; i<4; i++){
    tone(buzzer, NOTE_G4);
    delay(500);
    noTone(buzzer);
    tone(buzzer, NOTE_C4);
    delay(500);
    noTone(buzzer);
    tone(buzzer, NOTE_E4);
    delay(250);
    noTone(buzzer);
    tone(buzzer, NOTE_F4);
    delay(250);
    noTone(buzzer);
  }

    tone(buzzer, NOTE_G4);
    delay(1500);
    noTone(buzzer);
    tone(buzzer, NOTE_C4);
    delay(1500);
    tone(buzzer, NOTE_E4);
    delay(250);
    tone(buzzer, NOTE_F4);
    delay(250);  
    tone(buzzer, NOTE_G4);
    delay(1000);
    noTone(buzzer);
    tone(buzzer, NOTE_C4);
    delay(1000);
    tone(buzzer, NOTE_DS4);
    delay(250);
    noTone(buzzer);
    tone(buzzer, NOTE_F4);
    delay(250);
    noTone(buzzer);
   
  for(int i=0; i<3; i++) { 
    tone(buzzer, NOTE_D4);
    delay(500);
    noTone(buzzer);
    tone(buzzer, NOTE_G3);
    delay(500);
    noTone(buzzer);
    tone(buzzer, NOTE_AS3);
    delay(250);
    noTone(buzzer);
    tone(buzzer, NOTE_C4);
    delay(250);
    noTone(buzzer);
  }
 
      tone(buzzer, NOTE_D4);
      delay(1500);
      noTone(buzzer);
      tone(buzzer, NOTE_F4);
      delay(1500);
      noTone(buzzer);
      tone(buzzer, NOTE_AS3);
      delay(1000);
      noTone(buzzer);
      tone(buzzer, NOTE_DS4);
      delay(250);
      noTone(buzzer);
      tone(buzzer, NOTE_D4);
      delay(250);
      noTone(buzzer);
      tone(buzzer, NOTE_F4);
      delay(1000);
      noTone(buzzer);
      tone(buzzer, NOTE_AS3);
      delay(1000);
      noTone(buzzer);
      tone(buzzer, NOTE_DS4);
      delay(250);
      noTone(buzzer);
      tone(buzzer, NOTE_D4);
      delay(250);
      noTone(buzzer);
      tone(buzzer, NOTE_C4);
      delay(500);
      noTone(buzzer);

  for(int i=0; i<3; i++) {
      tone(buzzer, NOTE_GS3);
      delay(250);
      noTone(buzzer);
      tone(buzzer, NOTE_AS3);
      delay(250);
      noTone(buzzer);
      tone(buzzer, NOTE_C4);
      delay(500);
      noTone(buzzer);
      tone(buzzer, NOTE_F3);
      delay(500);
      noTone(buzzer);
  }

    tone(buzzer, NOTE_G4);
    delay(1000);
    noTone(buzzer);
    tone(buzzer, NOTE_C4);
    delay(1000);
    noTone(buzzer);
    tone(buzzer, NOTE_DS4);
    delay(250);
    noTone(buzzer);
    tone(buzzer, NOTE_F4);
    delay(250);
    noTone(buzzer);
    tone(buzzer, NOTE_G4);
    delay(1000);
    noTone(buzzer);
    tone(buzzer, NOTE_C4);
    delay(1000);
    noTone(buzzer);
    tone(buzzer, NOTE_DS4);
    delay(250);
    noTone(buzzer);
    tone(buzzer, NOTE_F4);
    delay(250);
    noTone(buzzer);
    tone(buzzer, NOTE_D4);
    delay(500);
    noTone(buzzer);

  for(int i=0; i<4; i++) {
    tone(buzzer, NOTE_G3);
    delay(500);
    noTone(buzzer);
    tone(buzzer, NOTE_AS3);
    delay(250);
    noTone(buzzer);
    tone(buzzer, NOTE_C4);
    delay(250);
    noTone(buzzer);
    tone(buzzer, NOTE_D4);
    delay(500);
    noTone(buzzer);
    }
}

Pink Panther

Code:

/*
ELB-v2   UNO
+5V    – 5V
GND    – GND
D7     – 7
D6     – 6
D5     – 5
D4     – 4
EN     – 3
RS     – 2
SW1    – 8
BUZZ.  – 9
*/
#include <LiquidCrystal.h>
LiquidCrystal lcd(2, 3, 4, 5, 6, 7);

const int SW1 = 8; //push button on D8
const int buzzer = 9; // Buzzer on D9

#define NOTE_B0  31
#define NOTE_C1  33
#define NOTE_CS1 35
#define NOTE_D1  37
#define NOTE_DS1 39
#define NOTE_E1  41
#define NOTE_F1  44
#define NOTE_FS1 46
#define NOTE_G1  49
#define NOTE_GS1 52
#define NOTE_A1  55
#define NOTE_AS1 58
#define NOTE_B1  62
#define NOTE_C2  65
#define NOTE_CS2 69
#define NOTE_D2  73
#define NOTE_DS2 78
#define NOTE_E2  82
#define NOTE_F2  87
#define NOTE_FS2 93
#define NOTE_G2  98
#define NOTE_GS2 104
#define NOTE_A2  110
#define NOTE_AS2 117
#define NOTE_B2  123
#define NOTE_C3  131
#define NOTE_CS3 139
#define NOTE_D3  147
#define NOTE_DS3 156
#define NOTE_E3  165
#define NOTE_F3  175
#define NOTE_FS3 185
#define NOTE_G3  196
#define NOTE_GS3 208
#define NOTE_A3  220
#define NOTE_AS3 233
#define NOTE_B3  247
#define NOTE_C4  262
#define NOTE_CS4 277
#define NOTE_D4  294
#define NOTE_DS4 311
#define NOTE_E4  330
#define NOTE_F4  349
#define NOTE_FS4 370
#define NOTE_G4  392
#define NOTE_GS4 415
#define NOTE_A4  440
#define NOTE_AS4 466
#define NOTE_B4  494
#define NOTE_C5  523
#define NOTE_CS5 554
#define NOTE_D5  587
#define NOTE_DS5 622
#define NOTE_E5  659
#define NOTE_F5  698
#define NOTE_FS5 740
#define NOTE_G5  784
#define NOTE_GS5 831
#define NOTE_A5  880
#define NOTE_AS5 932
#define NOTE_B5  988
#define NOTE_C6  1047
#define NOTE_CS6 1109
#define NOTE_D6  1175
#define NOTE_DS6 1245
#define NOTE_E6  1319
#define NOTE_F6  1397
#define NOTE_FS6 1480
#define NOTE_G6  1568
#define NOTE_GS6 1661
#define NOTE_A6  1760
#define NOTE_AS6 1865
#define NOTE_B6  1976
#define NOTE_C7  2093
#define NOTE_CS7 2217
#define NOTE_D7  2349
#define NOTE_DS7 2489
#define NOTE_E7  2637
#define NOTE_F7  2794
#define NOTE_FS7 2960
#define NOTE_G7  3136
#define NOTE_GS7 3322
#define NOTE_A7  3520
#define NOTE_AS7 3729
#define NOTE_B7  3951
#define NOTE_C8  4186
#define NOTE_CS8 4435
#define NOTE_D8  4699
#define NOTE_DS8 4978
#define REST      0

int tempo = 120;

int melody[] = {
  // Pink Panther theme
  // Score available at https://musescore.com/benedictsong/the-pink-panther
  // Theme by Masato Nakamura, arranged by Teddy Mason

  REST,2, REST,4, REST,8, NOTE_DS4,8,
  NOTE_E4,-4, REST,8, NOTE_FS4,8, NOTE_G4,-4, REST,8, NOTE_DS4,8,
  NOTE_E4,-8, NOTE_FS4,8,  NOTE_G4,-8, NOTE_C5,8, NOTE_B4,-8, NOTE_E4,8, NOTE_G4,-8, NOTE_B4,8,  
  NOTE_AS4,2, NOTE_A4,-16, NOTE_G4,-16, NOTE_E4,-16, NOTE_D4,-16,
  NOTE_E4,2, REST,4, REST,8, NOTE_DS4,4,

  NOTE_E4,-4, REST,8, NOTE_FS4,8, NOTE_G4,-4, REST,8, NOTE_DS4,8,
  NOTE_E4,-8, NOTE_FS4,8,  NOTE_G4,-8, NOTE_C5,8, NOTE_B4,-8, NOTE_G4,8, NOTE_B4,-8, NOTE_E5,8,
  NOTE_DS5,1,  
  NOTE_D5,2, REST,4, REST,8, NOTE_DS4,8,
  NOTE_E4,-4, REST,8, NOTE_FS4,8, NOTE_G4,-4, REST,8, NOTE_DS4,8,
  NOTE_E4,-8, NOTE_FS4,8,  NOTE_G4,-8, NOTE_C5,8, NOTE_B4,-8, NOTE_E4,8, NOTE_G4,-8, NOTE_B4,8,  
 
  NOTE_AS4,2, NOTE_A4,-16, NOTE_G4,-16, NOTE_E4,-16, NOTE_D4,-16,
  NOTE_E4,-4, REST,4,
  REST,4, NOTE_E5,-8, NOTE_D5,8, NOTE_B4,-8, NOTE_A4,8, NOTE_G4,-8, NOTE_E4,-8,
  NOTE_AS4,16, NOTE_A4,-8, NOTE_AS4,16, NOTE_A4,-8, NOTE_AS4,16, NOTE_A4,-8, NOTE_AS4,16, NOTE_A4,-8,  
  NOTE_G4,-16, NOTE_E4,-16, NOTE_D4,-16, NOTE_E4,16, NOTE_E4,16, NOTE_E4,2,
};

int notes = sizeof(melody) / sizeof(melody[0]) / 2;
int wholenote = (60000 * 4) / tempo;

int divider = 0, noteDuration = 0;

void setup() {
  pinMode(buzzer, OUTPUT);
  pinMode(SW1, INPUT);
  lcd.begin(16, 2);
  lcd.setCursor(2,0);
  lcd.print(“PINK PANTHER”);
}

void loop() {
  if(digitalRead(SW1)==LOW){
  for (int thisNote = 0; thisNote < notes * 2; thisNote = thisNote + 2) {
    divider = melody[thisNote + 1];
    if (divider > 0) {
      noteDuration = (wholenote) / divider;
    } else if (divider < 0) {
      noteDuration = (wholenote) / abs(divider);
      noteDuration *= 1.5; // increases the duration in half for dotted notes
    }

    tone(buzzer, melody[thisNote], noteDuration * 0.9);

    delay(noteDuration);
    noTone(buzzer);
  }
}
}