top of page

Arduino Birthday Project

Updated: Dec 11, 2021

This DIY exercise is a simple project which involves both artistic and engineering skills. Upon completion, this project could be presented as a birthday gift to your loved ones. The intended outcome of this project is to create the "Happy Birthday" tune using a buzzer and adding different colored LED's, which light up at each note, to make the project more visually appealing. The above-mentioned buzzer and LED's are controlled by an Arduino Uno board. This small assignment could also be used as a quick introduction to commonplace electronic components used in a wide range of electric circuits. The components used in this specific circuit, shown in the video, are listed below, with a very brief explanation of what each component does.


Components

Arduino Uno - Arduino Uno is a microcontroller with several input/output pins, used to control hardware using software. The software program could be downloaded for free off of Arduino's website. The Arduino Uno can be considered as the "brains" of this project.

Current Limiting Resistor - Any electric circuit has a power source which supplies voltage (measured in volts, V) across components and, consequently, leads to current, which is the flow of electric charge, flowing in the circuit. Current is measured in amperes, A and can be determined using Ohm's Law, V=IR. Resistors are used to limit the current flowing through electric components, thus protecting them from burning out. Resistance is measured in ohms. In this case six 220 ohm resistors are used to protect each LED. Resistors have no polarity.

LED - Light emitting diodes or LED's are semiconductor materials which emit light when supplied with voltage. The light coming from LED's are the result of photons being emitted as a result of electrons jumping a band gap. The color of the LED depends on the size of this band gap (a property of the semiconductor). LED's have polarity, and, therefore, have to be connected correctly: long pin connected to voltage supply and short pin connected to ground. For this project, three LED's (red, yellow, and green) are used, each preceded by a 220 ohm resistor to protect it from burning out.

RGB LED - An RGB LED is an LED which primarily has three LED's embedded in it: red, green, and blue. The beauty of this LED comes in the fact that different degrees of the primary colors (red, green, and blue) can be input to produce other colors like orange and purple, for example. Since an RGB LED is made up of three LED's, it requires three other 220 ohm resistors to be put in series before each pin.

Passive Buzzer - A passive buzzer is a component which produces noise when supplied with a voltage. Passive buzzers are different from active buzzers in that they can produce different pitched sounds based on how quickly they are turned on and off, making them perfect for this project. Passive buzzers have polarity and have to be connected correctly.


Code

int buzzPin=2;

int bluePin=3;

int greenPin=5;

int redPin=6;

int redPin2=9;

int yellowPin=11;

int greenPin2=12;

int j;

int quaver=500;

int semiquaver=250;

int crotchet=1000;

int finish=10000;

int g=2551;

int a=2273;

int c=1911;

int b=2025;

int d=1703;

int e=1517;

int f=1432;

int g2=1276;


void setup() {

// put your setup code here, to run once:

pinMode(buzzPin,OUTPUT);

pinMode(bluePin,OUTPUT);

pinMode(greenPin,OUTPUT);

pinMode(redPin,OUTPUT);

pinMode(redPin2,OUTPUT);

pinMode(yellowPin,OUTPUT);

pinMode(greenPin2,OUTPUT);

Serial.begin(9600);


void loop() {

// put your main code here, to run repeatedly:

for(j=1;j<=50;j=j+1){

digitalWrite(bluePin,HIGH);

digitalWrite(greenPin,LOW);

digitalWrite(redPin,LOW);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(g);

digitalWrite(buzzPin,LOW);

delayMicroseconds(g);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(semiquaver);

for(j=1;j<=50;j=j+1){

digitalWrite(bluePin,LOW);

digitalWrite(greenPin,LOW);

digitalWrite(redPin,LOW);

digitalWrite(redPin2,HIGH);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(g);

digitalWrite(buzzPin,LOW);

delayMicroseconds(g);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(semiquaver);

for(j=1;j<=100;j=j+1){

digitalWrite(bluePin,LOW);

digitalWrite(greenPin,LOW);

digitalWrite(redPin,LOW);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,HIGH);

digitalWrite(greenPin2,LOW);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(a);

digitalWrite(buzzPin,LOW);

delayMicroseconds(a);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(quaver);

for(j=1;j<=100;j=j+1){

digitalWrite(bluePin,LOW);

digitalWrite(greenPin,LOW);

digitalWrite(redPin,LOW);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(g);

digitalWrite(buzzPin,LOW);

delayMicroseconds(g);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(quaver);

for(j=1;j<=100;j=j+1){

digitalWrite(bluePin,LOW);

digitalWrite(greenPin,LOW);

digitalWrite(redPin,LOW);

digitalWrite(redPin2,HIGH);

digitalWrite(yellowPin,HIGH);

digitalWrite(greenPin2,LOW);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(c);

digitalWrite(buzzPin,LOW);

delayMicroseconds(c);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(quaver);

for(j=1;j<=150;j=j+1){

analogWrite(bluePin,0);

analogWrite(greenPin,15);

analogWrite(redPin,255);

digitalWrite(redPin2,HIGH);

digitalWrite(yellowPin,HIGH);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(b);

digitalWrite(buzzPin,LOW);

delayMicroseconds(b);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(crotchet);

for(j=1;j<=50;j=j+1){

analogWrite(bluePin,255);

analogWrite(greenPin,0);

analogWrite(redPin,200);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(g);

digitalWrite(buzzPin,LOW);

delayMicroseconds(g);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(semiquaver);

for(j=1;j<=50;j=j+1){

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,HIGH);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(g);

digitalWrite(buzzPin,LOW);

delayMicroseconds(g);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(semiquaver);

for(j=1;j<=100;j=j+1){

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,HIGH);

digitalWrite(greenPin2,LOW);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(a);

digitalWrite(buzzPin,LOW);

delayMicroseconds(a);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(quaver);

for(j=1;j<=100;j=j+1){

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(g);

digitalWrite(buzzPin,LOW);

delayMicroseconds(g);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(quaver);

for(j=1;j<=100;j=j+1){

analogWrite(bluePin,30);

analogWrite(greenPin,0);

analogWrite(redPin,255);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(d);

digitalWrite(buzzPin,LOW);

delayMicroseconds(d);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(quaver);

for(j=1;j<=150;j=j+1){

analogWrite(bluePin,255);

analogWrite(greenPin,200);

analogWrite(redPin,0);

digitalWrite(redPin2,HIGH);

digitalWrite(yellowPin,HIGH);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(c);

digitalWrite(buzzPin,LOW);

delayMicroseconds(c);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(crotchet);

for(j=1;j<=50;j=j+1){

analogWrite(bluePin,255);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(g);

digitalWrite(buzzPin,LOW);

delayMicroseconds(g);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(semiquaver);

for(j=1;j<=50;j=j+1){

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(g);

digitalWrite(buzzPin,LOW);

delayMicroseconds(g);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(semiquaver);

for(j=1;j<=100;j=j+1){

analogWrite(bluePin,255);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(g2);

digitalWrite(buzzPin,LOW);

delayMicroseconds(g2);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(quaver);

for(j=1;j<=100;j=j+1){

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,HIGH);

digitalWrite(yellowPin,HIGH);

digitalWrite(greenPin2,LOW);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(e);

digitalWrite(buzzPin,LOW);

delayMicroseconds(e);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(quaver);

for(j=1;j<=100;j=j+1){

analogWrite(bluePin,255);

analogWrite(greenPin,0);

analogWrite(redPin,200);

digitalWrite(redPin2,HIGH);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(c);

digitalWrite(buzzPin,LOW);

delayMicroseconds(c);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(quaver);

for(j=1;j<=100;j=j+1){

analogWrite(bluePin,255);

analogWrite(greenPin,200);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,HIGH);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(b);

digitalWrite(buzzPin,LOW);

delayMicroseconds(b);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(quaver);

for(j=1;j<=150;j=j+1){

analogWrite(bluePin,255);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,HIGH);

digitalWrite(yellowPin,HIGH);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(a);

digitalWrite(buzzPin,LOW);

delayMicroseconds(a);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(crotchet);

for(j=1;j<=50;j=j+1){

analogWrite(bluePin,0);

analogWrite(greenPin,15);

analogWrite(redPin,255);

digitalWrite(redPin2,HIGH);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(f);

digitalWrite(buzzPin,LOW);

delayMicroseconds(f);

}

delay(semiquaver);

for(j=1;j<=50;j=j+1){

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,HIGH);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(f);

digitalWrite(buzzPin,LOW);

delayMicroseconds(f);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(semiquaver);

for(j=1;j<=100;j=j+1){

analogWrite(bluePin,255);

analogWrite(greenPin,0);

analogWrite(redPin,200);

digitalWrite(redPin2,HIGH);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(e);

digitalWrite(buzzPin,LOW);

delayMicroseconds(e);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(quaver);

for(j=1;j<=100;j=j+1){

analogWrite(bluePin,255);

analogWrite(greenPin,200);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,HIGH);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(c);

digitalWrite(buzzPin,LOW);

delayMicroseconds(c);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(quaver);

for(j=1;j<=100;j=j+1){

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,HIGH);

digitalWrite(yellowPin,HIGH);

digitalWrite(greenPin2,LOW);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(d);

digitalWrite(buzzPin,LOW);

delayMicroseconds(d);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(quaver);

for(j=1;j<=250;j=j+1){

analogWrite(bluePin,255);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,HIGH);

digitalWrite(yellowPin,HIGH);

digitalWrite(greenPin2,HIGH);

digitalWrite(buzzPin,HIGH);

delayMicroseconds(c);

digitalWrite(buzzPin,LOW);

delayMicroseconds(c);

}

analogWrite(bluePin,0);

analogWrite(greenPin,0);

analogWrite(redPin,0);

digitalWrite(redPin2,LOW);

digitalWrite(yellowPin,LOW);

digitalWrite(greenPin2,LOW);

delay(finish);

}

Code Explanation

Declared Variables - The first set of variables declared refer to the pins connected to the LED's. redPin, greenPin, and bluePin refer to the RGB LED, and redPin2, yellowPin, and greenPin2, refer to the three other individual LED's. The next set of variables which include quaver, semiquaver, and crotchet refer to the delay in milliseconds between each note. The final set of variables refer to the notes which are made by the buzzer. The frequency of the notes can be found online. By dividing 1 by the frequency and then multiplying by 1000, the delay in microseconds can be calculated and input to the buzzer. So, for example, an A4 note has a frequency of 440 Hz. Dividing 1 by 440 and multiplying by 1000 gives 2273 microseconds, which is how quickly the buzzer switches on and off, giving off an A note. Each declared variable refers to what note the buzzer gives off (g refers to a G note, a to an A note, b to a B note, etc.). The declared variable j is used in the for loops to hold each note for a specific period of time.

Void Setup - This section of the code runs once only and, in this case, is used to declare the pins connected to the LED's and buzzer as output pins.

Void Loop - This section loops continuously and is where the main code is written. The version of "Happy Birthday" played in this project is in the C major scale with the following notes: G G A G C B, G G A G D C, G G G2 E C B A, F F E C D C.

The first three lines in each for loop refer to the RGB LED. By analog writing to each LED with varying degrees, different colors can be used. Analog writing in Arduino uses 8 bits of 0's and 1's (in binary) which results in 256 states. The degree of each LED can be controlled by analog writing to this LED (0 being the lowest degree and 255 being the highest degree). Taking purple as an example, the RGB can be used by inputting high level of blue (255), medium level of red (200), and low level of green (0).

The following three lines refer to digital writing to the three separate LED's. The colors included in this particular code were chosen at random.

The last four lines in each for loop refer to how quickly the passive buzzer turns on and off. By changing the delay (in microseconds) after each ON and OFF, the note given off by the buzzer can be changed according to the song.

After each for loop, all the LED's and the buzzer are switched off before switching on again at the start of the following note.


Same Concept Can Be Applied to Other Songs!

Many different songs can be played using this same circuit design. The only difference comes in the how quickly the passive buzzer turns on and off, giving off different notes. By getting the frequency of the desired notes of a particular song, the delay can then be easily calculated and input to the buzzer, resulting in the producing many different songs and having an endless amount of fun.



Comentários


bottom of page