3 Generating Analog Signals with Pulse Width Modulation (PWM)
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:
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.
Where VOUT is the output of the filter, DC is the duty cycle (in %), and VDD is the power supply to the microcontroller.