Project tutorial
Piezo "happy birthday"

Piezo "happy birthday" © GPL3+

The first song in the piezo song series

  • 23,459 views
  • 7 comments
  • 14 respects

Components and supplies

Apps and online services

About this project

The second song in the piezo song series!

First plug in one side of the piezo to pin 9 and the other to ground.

Then upload the code to your arduino!

Code

happy birthday songArduino
int speakerPin = 9;

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(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); // rest

   } else {

     playNote(notes[i], beats[i] * tempo);

   }

   // pause between notes

   delay(tempo);

}

}

Schematics

download (1).jpg
Download%20(1)

Comments

Similar projects you might like

Happy Birthday Melody On Arduino Nano With 3-way Polyphony

Project tutorial by Liss

  • 15,160 views
  • 8 comments
  • 53 respects

Happy Birthday Melody with Lights and Sounds

Project tutorial by msr048

  • 2,909 views
  • 0 comments
  • 19 respects

Happy Birthday: Lights and Sounds

Project tutorial by jsheng

  • 5,705 views
  • 2 comments
  • 21 respects

DHT11 Sensor with LEDs and a Piezo Speaker

Project tutorial by Tech with Matthew

  • 10,499 views
  • 4 comments
  • 22 respects

The 12. Birthday - Project 12

Project tutorial by Thomas Angielsky

  • 914 views
  • 0 comments
  • 4 respects
Add projectSign up / Login