Sounding Off
Adding Sound to Your Arduino Robot
by Gordon McComb
Discuss this article in the SERVO Magazine forums
at ht tp:// forum.servomagazine.com.
My first robot with sound used the
mechanism from an old cassette
recorder. Music and sound effects
were stored on short tapes. The
whole thing was mechanically driven
with a solenoid that turned the
PLAY button on and off. The biggest
problem: There was no way to know
what musical jingle the robot was
going to play next.
Thanks to several ready-made add-on boards for the Arduino and other popular microcontrollers, you can
incorporate melodies, effects, and other sounds to your
robots. You have full control over what’s played — from
FIGURE 1. Pulse width modulation is a common technique
for making sound using a digital output. By using pulses
within the range of human hearing (about 20 Hz to 20
kHz) and changing the timing of the pulses, the signal is
heard as a sound tone.
short quarter-second gunshots to 10 minute lullabies.
In this article, you’ll read about how to give your
mechanical creations completely electronic sound effects,
music, and even voice. The subject of robotic sound is a
large one, and a single installment isn’t enough to
address everything. So this time around, I’ll cover basic
sound generation using just the Arduino — with and
without external amplification — plus adding low cost
effects, music, and voice co-processor chips to simplify the
noise making.
Next time, I’ll cover even fancier sounding off methods,
including playing WAV, MP3, and other digital files, making
music with MIDI, and building your own sound co-processor.
Important note: The demo code in this article is
intended for the Arduino 1.0 integrated development
environment (IDE). If you haven’t already, be sure to
download the latest Arduino IDE software. You can install it
side-by-side with any previous version you already have. This
is recommended, since some Arduino examples you’ll find
on the Web may not yet be compatible with the 1.0
software.
The Sound of Arduino Music
Any microcontroller with a pulse width modulation
(PWM) feature can be used to produce sound effects and
music. PWM is a series of pulses as
shown in Figure 1; when the pulse
frequency is within the range of
hearing — about 20 Hz to 20 kHz — it
comes out as a tone. The sound is
heard by passing the pulses by way of
the microcontroller’s I/O pins through
a speaker or amplifier.
By varying the frequency of the
PWM, you make high, middle, and
low tones. You can produce music
with one PWM output (that’s called
monophonic), or you can combine
the outputs of two or more PWM
FIGURE 2. Attach the piezo element
directly to the Arduino I/O pins. To
simplify the connection (so the two
wires from the speaker are next to
each other), I'm using one of the pins
as ground. Refer to the sketches for
how this is done.
44 SERVO 03.2012