Nibbles and Bits
The Care and Feeding of My Pet Arduino


'My Dog Has Fleas' - A Binary Music Machine
« 1  2  3 4 5 6 7 8 9 10 11 12 »

Section 2 - Sketch Research
Now that I have a speaker, I spent a bit of time experimenting with my breadboard and a few parts. I knew that I would have to somehow turn a pin on and off very quickly to get it to create anything that might resemble a pleasant sound. That actually worked, but while skimming through the reference section of the Arduino Site something caught my eye: We have a command called: tone () !

So with this in mind, the goal of this unit is to write two sketches. First a simple one that will play four notes with four buttons. They are the strings on a ukulele: G C E A.

'My Dog Has Fleas!'
(If you don't get it, click here ).

The second sketch will be more sophisticated. We will use what we have been learning about binary numbers to create an Music Machine that will play two octaves with only four buttons!

For now, the info on the right is from the Arduino Web Site. You can study it here, or click the logo above and jump to the full page.

Next Section »


Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can be specified, otherwise the wave continues until a call to noTone(). The pin can be connected to a piezo buzzer or other speaker to play tones.

Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency.

Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).

NOTE: if you want to play different pitches on multiple pins, you need to call noTone() on one pin before calling tone() on the next pin.

tone(pin, frequency)
tone(pin, frequency, duration)

pin: the pin on which to generate the tone

frequency: the frequency of the tone in hertz - unsigned int

duration: the duration of the tone in milliseconds (optional) - unsigned long