GETTING STARTED WITHPICs
THE LATEST IN PROGRAMMING MICROCONTROLLERS
■ BY CHUCK HELLEBUYCK
PULSE WIDTH MODULATION
PULSE WIDTH MODULATION (PWM) is a common term in today’s microcontroller
(MCU) world. What PWM is and how you use it are the subjects of this column.
As I’ve covered in previous columns, a MCU typically
senses digital signals such as switch inputs that are
either on or off. A MCU can also sense analog signals by
using an Analog-to-Digital Converter (ADC) to change the
analog signal to digital. PWM is a way to take a digital output and vary the on — or high time — to create a variable
output. If the PWM signal runs at a fixed frequency, then
changing the high time of the signal will change the low
time of the signal, as well. The amount of time the signal is
high is called the pulse width. The period of the signal is
defined as the time from one rising edge to the next rising
edge of the square wave signal and is inversely proportional
to the PWM frequency. The period can easily be calculated
by using the formula period
■ FIGURE 1. Low pass filter. = 1/frequency. A 1 kHz
frequency will give us a
1 millisecond period.
NO TE: The Microchip name and
logo, MPLAB, and PIC are registered
trademarks of Microchip Technology,
Inc., in the USA and other countries.
PICkit is a trademark of Microchip
Technology, Inc., in the USA and
other countries. All other trademarks
mentioned herein are property of
their respective companies.
Duty cycle is what we are really interested in when
using PWM to control a circuit. Duty cycle is the amount
of time the signal is high, relative to the period of the
PWM signal. Duty cycle is measured in percentage of the
whole period. For example, a 50% duty cycle will be high
for half the period and low for half the period. If we
wanted to create a different duty cycle — such as a 20%
duty cycle — then we would make the pulse width last
only 20% of the period.
Using PWM gets very interesting because there are
many applications for it. Simplest of all and a great place
to start is driving some kind of low-pass filter. By driving a
simple resistor-capacitor low-pass filter circuit (see Figure
1) directly from the MCU’s PWM port, the variable pulse
width will be averaged out by the capacitor’s charge and
discharge rate through the resistor. Figure 2 shows this in
detail, using a 50% duty cycle.
The top part of Figure 2 shows the square wave
generated by the MCU. In a digital world, the high pulse
can be considered on and the low pulse considered off.
The circuit diagrams below the pulses show what the
low-pass filter is doing. The arrow shows the direction of
the current. The bottom portion of the figure shows the
charging and discharging voltage of the capacitor. This
depends upon the frequency of the signal, and the values
you select for the resistor and
■ FIGURE 2. PWM signal. capacitor. The important
thing to notice is the dashed
line running through the
charge/discharge signal. That
dashed line is the average
voltage across the capacitor.
Even though the MCU is
outputting five- or zero-volt
signals only, you would see
about 2.5 volts across the
capacitor if you measured it
with a voltmeter.
APPLICATIONS
PWM is used in many
16
September 2008