In my quest to try out everything the Arduino can possibly do, I decided to hook up a buzzer and try messing around with generating some tones. Since my little 8ohm buzzer seemed to have gone missing, I substituted one of Sparkfun’s audio jack breakout boards and a cheap pair of earbuds I had lying around. DO NOT use a quality set of earbuds or headphones, or any set that you wouldn’t want damaged – we’ll see why in a minute. Just connect ground to ground and either the left or right channel (or both) to one of the digital pins – I used 13 so the LED would light up with the rhythm of the music. I eventually found a couple more buzzers and hooked them in.

Arduinos with Buzzers
My new Boarduino USB from Adafruit is on the left in a breadboard and the Duemilanove is on the right.
The most basic way to create audio using the Arduino is essentially bit-banging one of the digital pins – write high and low at the frequency you wish to generate. The downside is that this creates a nearly square wave, which sound very harsh and unnatural. Also, the “high” is 5V, which is a very high amplitude to be supplying to headphones. Constant use at this amplitude (volume) will probably damage the drivers in earbuds – use the cheap earbuds that came with your MP3 player. Using a standard 8ohm buzzer is of course the preferred method if you have one lying around. I found a buzz function online to control this – pass it the pin to buzz on, the frequency, and the length, and it does the work for you.
void buzz(int targetPin, long frequency, long length) {
long delayValue = 1000000/frequency/2; // calculate the delay value between transitions
//// 1 second's worth of microseconds, divided by the frequency, then split in half since
//// there are two phases to each cycle
long numCycles = frequency * length/ 1000; // calculate the number of cycles for proper timing
//// multiply frequency, which is really cycles per second, by the number of seconds to
//// get the total number of cycles to produce
for (long i=0; i < numCycles; i++){ // for the calculated length of time...
digitalWrite(targetPin,HIGH); // write the buzzer pin high to push out the diaphram
delayMicroseconds(delayValue); // wait for the calculated delay value
digitalWrite(targetPin,LOW); // write the buzzer pin low to pull back the diaphram
delayMicroseconds(delayValue); // wait againf or the calculated delay value
}
}
The downside is that this uses the delay function. That means your controller can't do anything else while waiting for the end of the delay. You would also have to use a delay function between the notes to create spaces. This is sufficient for a small buzzer project, but if you're looking to do more complex things (especially signals other than square wave), try consulting the Arduino Playground.
I was able to find a script for the Mario Bros theme online, and covert it into a series of buzz and delay calls. Not all the notes are correct and some of the spacing is off, but it's good enough to get the idea. The entire Arduino script is available here.
Trackbacks & Pingbacks 1
[...] having a musical note in my head, I had to have a google for the tone generation element of this project. This was the easy part. Just connect a speaker [...]
Post a Comment