December 2014 61
makes “noise” is being generous
— so let’s ask, what’s cheaper?
Now we’re getting somewhere,
since these things are cheap and
don’t require any external
The piezo speaker included
in the Arduino 101 projects kit
(available through the NV
Webstore) comes with long straight legs. You should bend
its legs as shown in Figure 3 so that it can be used
conveniently on the Arduino proto shield breadboard
(also shown in Figure 3).
We utilize the PWM peripheral that we discussed for
using to control the angle of servomotors. You can set
these PWMs so that they generate an audio frequency
associated with a musical note. For instance, to generate
the c musical note, we create an output waveform (see
Figure 4) that turns on and off with a frequency of
261 cycles per second. Each of these on/off cycles occurs
in 1/261 of a second, or 0.003831 seconds.
Since we are dealing with microseconds, we multiply
this by 1,000,000 to get 3,831 microseconds per cycle.
Since we need to cycle the pin (turn the pin on and off) in
that time, we turn it on for 3831/2 = 1,915 microseconds
(throwing away the fractional part) and off for 1,915
microseconds giving us a total of 3,830. We lost one due
to our not wanting to use fractions, but who is going to
miss a microsecond? Yeah that sounds like a lot of work
for the computer, but fortunately the CPU can simply tell
the PWM to do this, and it does it in the background with
no further intervention by the CPU until time to stop
outputting that sound.
Sound Using Interrupts
Earlier in this chapter, we discussed the Arduino
example program, toneMelody that plays a sequence of
notes and uses the delay() function to time the notes. The
main problem with the methods shown in the toneMelody
example from the Arduino IDE is that it uses the delay()
function to time the duration of the tone in the melody.
The delay() blocks the CPU while playing a tone, so it
can do nothing but wait for the tone to finish before it
does the next thing. That doesn’t matter in the
toneMelody example since all the Arduino is doing is
playing a tune. If you want to do anything else while you
play a tune, however, you’ll need to use interrupts. As
discussed above, by using interrupts the CPU can do
millions of operations while each tone of a tune plays in
We want to have the CPU set the duration in a
peripheral timer and have that timer interrupt the CPU
when the duration is passed, so that it can stop the
current tone and then instruct the PWM to play the next
tone. We will generate tones using the PWM and keep
track of the duration of the tone using a timer interrupt.
The duration of each tone will vary depending on the tune
being played, but each duration will be in common units.
For instance, we might have a melody with 100 mS units
of duration. The shortest time we can play a tone will be
100 mS, and each longer interval will be some number of
We then keep an array of durations which tells us
how many of these periods to play a tone. For instance, if
in my tone array at postion 10 we find a c note, we then
look in the duration array at position 10 and see that it is
5. Since we know that the minimum duration is 100 mS,
we will start that c note on the PWM and set the timer to
interrupt the CPU when 5*100 mS have passed; that is
500 mS, or half a second. In order to play a melody this
way, we will need the tone array, the duration array, the
number of tones we are going to play, and the minimum
duration of a tone.
For example, we use the following data to play Happy
■ FIGURE 3: Piezo with legs bent for breadboard.
■ FIGURE 4:
A c note