devices so that they produce results functionally similar to
a variable voltage.
Figure 11 illustrates this concept with four pulse
trains (pulses that repeat over a set interval know as the
period) where the vertical represents voltage (zero to + 5)
and the horizontal represents time. In the first pulse train,
the pulse is turned on to + 5 volts 10% of the time and off
to zero volts 90% of the time. In that case, the average
voltage over time is 10% of five volts or 0.5 volts.
The second pulse train is 30% on and 70% off, giving
an average of 1.5 volts. The third pulse is 50% on and
50% off, and averages to 50% of five volts or 2. 5 volts.
The last one is 90% on and 10% off for a 4. 5 volts
average. [Please note this is a simplification of what
happens in real electronic systems and is meant to help
understand what is going on at this point in the
discussion.]
These digital signals are known as PWM (Pulse Width
Modulation). To output PWM signals with an Arduino,
you use the analogWrite(write_value); function. This
function takes the write_value variable which can be from
zero to 255, and uses that number to set the width of the
pulses as shown in Figure 12. There are three terms you
need to describe what is happening: frequency, period,
and pulse width.
The frequency is the number of times a repeating
event occurs per unit time. The period is the duration of
one cycle of that repeating event. We use the term Hz
(Hertz) to indicate how many times the repeating event
occurs in one second. The Arduino analog Write function
outputs a pulse train with a frequency of about 490 Hz
that equates to a period of about 2 ms.
We see in Figure 12 that each of these pulse trains
creates a constant DC (Direct Current) voltage that does
not change over time. Since we can set the pulse to any
of 256 widths, that means we can have 256 discrete DC
voltages. If we use analog Write(0);, we get a voltage of
zero; if we use analog Write(255), we get the maximum
voltage that (in our case) is five volts. If we use
analog Write(127) — half way between the two — then we
get half of five volts, or 2. 5 volts.
You can calculate the write_value to use in
analog Write(write_value) to give a percent of time the
pulse is high by multiplying the percent times 255. Thus,
for a 90% on time, you multiply 0.9x255 = 229.5. Since
we are using integers, you round it up to 230. This is
shown in Figure 12 as analog Write(230).
May 2014 15
; FIGURE 10: Arduino Uno PWM pins.
; FIGURE 11: Pulse
width and period. ; FIGURE 12: Pulses and average voltage.
Lab 1: Controlling LED Brightness.
Parts required:
1 Arduino
1 USB cable
1 Arduino proto shield
1 LED
1 1,000 W resistor
Estimated time for this lab:15 minutes
Check off when complete:
; Connect the LED to pin 11 as shown in Figures 13
and 14.