Happy Birthday Song Using Arduino

by VishalJegadeesan in Circuits > Arduino

662 Views, 1 Favorites, 0 Comments

Happy Birthday Song Using Arduino

Arduino Project Series #1 - Happy Birthday Song with Speaker

This is My First Project of the Arduino Project Series. In this Project I have created a simple circuit which plays the Happy Birthday Song through the Speaker connected with the Arduino.

Follow Me for More Videos: YouTube | Instagram

Want to learn More on Robotics - Enroll for my 5-Days Robotics BootCamp Program @ Rs.499
Click Here to Know More

Components Required

2021-05-16_23-24-00.jpg
2021-05-16_23-23-36.jpg
2021-05-16_23-24-54.jpg
2021-05-16_23-20-25.jpg
2021-05-16_23-22-58.jpg

In order to do this Experiment you will require the following components:

  1. Breadboard
  2. Arduino
  3. Speaker (not more than 3W)
  4. Transistor - BC547
  5. Jumper Wires

Connection

Happy Birthday Song.png

Make the Connections as shown in the above picture

Code for Arduino

Upload the Following Code into Your Arduino

	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);
		}
	}