Hardware Reference
In-Depth Information
Chapter 9
Pulse-Width Modulation
This chapter explores pulse-width modulation (PWM) using the Raspberry Pi. PWM is
applied in motor control, light dimming, and servo controls, to name a few examples.
To keep the hardware simple and the software small enough to read, we're going to apply
PWM to driving an analog meter in this chapter.
While the CPU percent-busy calculation used here is a bit cheesy, it is simple
and effective for our demonstration. The meter deflection will indicate how busy your
Raspberry Pi's CPU is. We'll demonstrate this using a hardware and software PWM
solution.
Introduction to PWM
The GPIO output signal is a digital signal that may be only on or off. You can program it to
deliver only 3 V or 0 V. Consequently, there is no means for the software to ask the GPIO
to deliver 1 or 2 V. Despite this limitation, an analog meter can be driven from a digital
output using PWM.
PWM is a technique that works on the principle of averaging the signal. If the
signal is on for 10% of the total cycle, then when the signal is averaged out, the result is
an analog 10% of the two digital extremes. If the highest voltage produced by the GPIO
output is 3.3 V, a repeating digital signal that is only on for 75% of that cycle produces an
average voltage that's determined as follows:
V
avg =ยด
=
33 075
2 475
.
.
.
V
If the GPIO output signal was high only 10% of the time, the averaged result is
V avg = 0.33 V. The on time as a percentage of the total cycle time is known as the duty cycle .
Obviously, there is an averaging aspect to all of this. If you applied the 10% signal to
the probes of an oscilloscope, you'd see a choppy digital-looking signal. The duty cycle
may be there, but the averaging effect is not.
The averaging effect is accomplished in several ways. In a lightbulb, the element
is heated up by the on pulses but does not cool immediately, so its brightness reflects
the averaged current flow. A DC motor does not immediately stop when the current is
withdrawn, because the rotational inertia keeps the rotor spinning. A meter's pointer
does not immediately move back to zero when the current is removed. All of these
physical effects have an averaging effect that can be exploited.
 
Search WWH ::




Custom Search