3 Generating Analog Signals with Pulse Width Modulation (PWM)

In most microcontrollers, the number of internal DACs in the device are limited. An alternative way to generate analog signals in the microcontroller is to use an RC filter with a PWM output. Most microcontrollers have some form of dedicated hardware that can generate PWM. The output voltage of the RC filter is proportional to the duty cycle of the PWM. Figure 3-1 shows the RC filter connections from the I/O pin:
Figure 3-1. RC Low Pass Filter
Note: This assumes the load on the output of the filter is negligible. If loaded, then a more complex analysis is required. For a sharper response, cascading multiple stages of the filter will improve performance.

To generate an analog output, there are four variables to consider:

  • Frequency (of PWM)
  • Duty Cycle (of PWM)
  • Resistance
  • Capacitance

Each of these variables influences the response and performance of the analog output. Frequency, for instance, influences ripple. If the frequency is too low, then the capacitor will have a high output ripple due to over-charging and -discharging. But, if the frequency is too high, then the resolution of the PWM is degraded.

To start, select a resistor and capacitor for the filter. The capacitor used in the filter should be kept small for two reasons. Firstly, larger capacitors take up more PCB area than smaller capacitors. Secondly, a large, charged capacitor can power-up a microcontroller through the ESD diodes in the I/O pins, causing unusual reset behavior or damage. We recommend using a high stability dielectric, such as X7R or C0G, to ensure the filter capacitance is stable with changes in output voltage.

This example uses R = 10kΩ and C = 0.1µF to implement the filter:

f3dB=12ΠRC

To reduce the amount of output ripple, the frequency of the PWM should be much higher than the -3 dB point. As a general guideline, this means at least 10x higher in frequency. With the example values (R = 10 kΩ and C = 0.1 µF), the -3 dB point works out to be about 159 Hz. Creating a PWM output at 1.6 kHz would fulfill the basic requirements, but the ripple reduces significantly with frequency. If a higher frequency output is available, or more optimal for generation, then the higher frequency option should be used instead.

The final variable to consider is the duty cycle (DC), which proportionally relates the desired output voltage to the power supply to the microcontroller, as shown below.

VOUT=DC*VDD

Where VOUT is the output of the filter, DC is the duty cycle (in %), and VDD is the power supply to the microcontroller.