This circuit investigates how to operate a piezo element (creates sound) using the analog system and pulse frequency.
Equipment
- 1 x Piezo Element
- 4 x Wires
- 1 x Breadboard & breadboard sheet
- 1 x Arduino Uno
Program Details
This circuit is probably one of the most easily assembled circuits, yet very effective. Assembly takes about 2-4 minutes. Simply attach the wires to the appropriate pins, and make sure one of them (9) is analog. This gives output signals to the piezo element, whose pin is connected parallel to the pin 9 wire on the breadboard. The assembled circuit looks like this.
The program for this circuit uses various functions to play the note. First we introduce the number of notes, the notes, beats and temp. Then each note is given a frequency. The program then combines all the variables with the appropriate operators (multiplication and division), to play the note for the designated amount of time. The main function, which plays the tune is a repeating loop at the end of the code, after setting everything up. More detail is in the comments in the last section of this report.
Results
Music to my ears! After watching LEDs for all this time, finally we can hear something different. The programming associated to this circuit plays a simple melody, however by adding your own notes, you can play any tune! Definitely trying this one later.
Tips
Assembly is not hard to follow. Simply be careful with your programming, make sure the numerical values correspond. Also, make sure you understand the program format before initializing it.
Next Steps & Associated Program Modifications
There are several tricks you can play on with this program. This includes changing the tempo (int tempo=#), tuning the notes by adjusting their individual frequencies as desired, and changing the melody!! I think that last one is fun, so below is the code for playing Happy Birthday.
int speakerPin = 9; //the piezo element is connected to this digital pin
int length = 13; // the number of notes in this tune+1
char notes[] = "ccdcfeccdcgf "; // the notes in this tune, where each note
//corresponds to a specific frequency
int beats[] = { 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 2, 4 }; //the beat, or length of
//each note introduced above
int tempo = 300; //the tempo for the tune
void playTone(int tone, int duration)
{
for (long i = 0; i < duration * 1000L; i += tone * 2) //sets up how long the tone is played
{
digitalWrite(speakerPin, HIGH);//turns the piezo on
delayMicroseconds(tone); //for the length of the tone
digitalWrite(speakerPin, LOW);//then the piezo is turned off
delayMicroseconds(tone); //delayed for length of tone
}
}
void playNote(char note, int duration)
{
char names[] = { 'c', 'd','f', 'e', 'g', 'f' }; //the different notes for the tune
int tones[] = { 1915, 1700, 1432, 1519, 1275,};
//their corresponding tone calculated by: 1/(2*notefrequency)
for (int i = 0; i < 6; i++)
{
if (names[i] == note)
{
playTone(tones[i], duration); //plays the note and tones introduced just above
}
}
}
void setup()
{
pinMode(speakerPin, OUTPUT); //setting pin 9 as output
}
void loop()
{
for (int i = 0; i < length; i++)
{
if (notes[i] == ' ') //this section plays all the notes introduced earlier in
//the array notes[], until all notes (12), the # less than variable 'length' is played
{
delay(beats[i] * tempo); // after the 'for' section there is a rest before repeating
}
else
{
playNote(notes[i], beats[i] * tempo);
}
delay(tempo / 2);
}
}
Reference: Spark Fun Inventor's Guide
No comments:
Post a Comment