■FIGURE 11. Close-up of LEDand
pushbutton breadboard layout.
in the brightest part of the sweep.
The concepts behind PWM are worthy of a full
Workshop (or two), but as you will see from the source
code, it is actually a fairly simple concept. There are two
loops: one for fading in and the other for fading out. Each
loop steps through 0 to 255 in increments of five and uses
that value in the Arduino analog Write() function that sets
the length of time the LED is turned on in each cycle.
When you look at the LED sans shaking, it seems to
brighten and fade smoothly since the eye/brain smoothes
out rapidly blinking lights. This phenomenon is called
persistence of vision (POV) and is the same thing that
makes movies and TV seem to move smoothly when, in
fact, you are seeing a sequence of still images.
For this exercise, we will assume that you learned
enough in the first two examples to convert the Arduino
Fading example code yourself (if not, you can find the
converted example in Workshop10.zip). Please note that
when I did this last example, I forgot to close the Arduino
IDE — which had the com port open causing an AVRDude
synch error — AND I forgot to add the F_CPU to the
wiring.c file. You have to be patient and persistent doing
this kind of work. So, don’t feel too bad when you make
the inevitable dumb mistakes like I make. It is part of the
process, so learn to enjoy it. NV
You can find the source code and supplements forthis
article in Workshop10.zip on the Nuts&Volts and
Smiley Micros websites.
And Now for Another Word from
Our Sponsors ...
Smiley Micros and Nuts & Volts are selling
a special kit — the Arduino Projects Kit providing
components for use with Smiley’s Workshops 9, 10,
and many future Workshops. Over time, we will
learn simple ways to use these components, and
more importantly we will use them to drill down
into the deeper concepts of C programming,
AVR microcontroller architecture, and embedded
With the components in this kit you can:
• Blink eight LEDs (Cylon Eyes).
• Read a pushbutton and eight-bit DIP switch.
• Sense voltage, light, and temperature.
• Make music on a piezo element.
• Sense edges and gray levels.
• Optically isolate voltages.
• Fade LED with PWM.
• Control motor speed.
• Plus more ...
One final note: The USB serial port on the
Arduino uses the FTDI FT232R chip. This was
discussed in detail in the article “The Serial Port is
Dead, Long Live the Serial Port’ by yours truly in
the June 2008 issue of Nuts & Volts. You can also
get the Virtual Serial Programming Cookbook
(also by yours truly) and associated projects kit
from either Nuts & Volts or Smiley Micros.
■ FIGURE 12. PWM Fade.