riduino

RiDuino Projects: Playing the National Anthem Using Piezo Speaker

Rating: 4 votes, 4.50 average.
Piezo speakers are quite fun components. They can play as well as detect frequencies. The basic idea behind making them play tunes is to send a "Square wave" of the corresponding frequency. By Square wave, I mean , the HIGH and LOW time should be equal.

How do we get the high and low time out of note frequencies?
High time = Low Time = 1/(2*NoteFrequency)

For Example, a basic C note (western musical notation) is 261 Hz. (http://www.phy.mtu.edu/~suits/notefreqs.html)
1/(2*261) = 0.001915 seconds = 1915 microseconds.
So a HIGH pulse (1) for 1915 microseconds and a LOW pulse (0) for 1915 microseconds will play a C note.

Getting to work

Make Connections with the RiDuino as follows.
Click image for larger version. 

Name:	RiBuzzer_bb.jpg 
Views:	304 
Size:	40.9 KB 
ID:	242

Now, I googled for the National Anthem western notations and found this site
http://www.keylessonline.com/song/mi...nalAnthem.html

The basic idea behind using RiDuino is to be able to avail the massive code libraries and examples available for the Arduino.

So going through the tutorial available , http://www.arduino.cc/en/Tutorial/Melody, and making the required changes.

Code:
int speakerPin = 9; //Speaker is connected to Pin 9 of the Board.

int length = 27; // the number of notes 
char notes[] = "cdeeeeeeeeeeedef eeedddbdc "; // a space represents a rest 
int beats[] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 2, 1, 1, 1, 1, 2, 4 };//Duration of each note
int tempo = 300; //Change song speed here

void playTone(int tone, int duration) {  //creating a square wave for the given 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) { //Assigning high time for the notes
  char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' };
  int tones[] = { 956, 851, 758, 716, 638, 568, 1014, 478 }; 

  // play the tone corresponding to the note name 
  for (int i = 0; i < 8; i++) { 
    if (names[i] == note) { 
      playTone(tones[i], duration); 
    }
  }
}

void setup() { 
  pinMode(speakerPin, OUTPUT); // Setting pin 9 as an OutPut Pin
}

void loop() {  //Main function
  for (int i = 0; i < length; i++) {  //For the length of the tune array
    if (notes[i] == ' ') {  //take space as rest
      delay(beats[i] * tempo); // rest 
    } else { 
      playNote(notes[i], beats[i] * tempo); //play the corresponding note for the corresponding beat
    } 

    // pause between notes 
    delay(tempo / 2); 
  } 
}
So as we can see, the note High times were generated by using the formula. Then change the notes, assign the corresponding beats, burn it and
YOU ARE DONE!

Here is the final video with the piezo speaker playing the first two lines of the national anthem.



Total time taken to build : 20 minutes (including coding time)

Lots more to come, stay on the lookout.
Tags: None Add / Edit Tags
Categories
Tutorial

Comments

  1. sushmair's Avatar
    I liked your description of a piezo element's reaction to a square wave. A lot of beginners (and maybe a few not so beginners) don't understand those basics.

    Rgds,
    sushmair.
  2. keenhohn's Avatar
    OWO, that is great.. . Good job. And the anthem really sounded clear. Thank you for sharing the code.