PWM

Using a DAC may seem the obvious way to generate an analog output voltage. But there is another way, using nothing more than a digital I/O line configured as an output. This technique is known as Pulse Width Modulation, or PWM.

Consider the average, garden-variety square wave shown in Figure 12-25.

A ubiquitous square wave

Figure 12-25. A ubiquitous square wave

The width of the high is equal to the width of the low, so this wave is said to have a 50% duty cycle. In other words, it is high for exactly half the cycle. Now, if the amplitude of this square wave is 5V, for example, the average voltage over the cycle is 2.5V. It is as though we had a constant voltage of 2.5V.

Now consider the square wave in Figure 12-26.

10% duty cycle

Figure 12-26. 10% duty cycle

This wave has a 10% duty cycle, which means that the average voltage over the cycle is 0.5V.

A low-pass (averaging) filter on the PWM output will convert the pulses to an analog voltage, proportional to the duty cycle of the PWM signal. By varying the duty cycle, we can vary the analog voltage. Hey, presto!—we have digital-to-analog conversion without a DAC. That’s the basic idea behind PWM.

Tip

PWM can also be used to drive a LED and thereby get varying light intensities from a signal that is essentially either on or off. PWM can also be used to generate audio. Early ...

Get Designing Embedded Hardware now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.