How to get an analog voltage from a PWM output?
Introduction
In many situations a designer may need to obtain an analog voltage from a microcontroller. This is typically done with built-in DACs, but sometimes (and this is quite common with cheap microcontrollers) the number of DACs is not enough or even zero. If the microcontroller has a spare PWM output, it is possible to obtain an analog voltage.
Obviouly, this won’t be as fast as a DAC, but it works for slow analog control voltages.
Typically, the output voltage of a PWM port is 0 V for the “low” state and 3.3 V for the “high” state. In order to get an arbitrary voltage between \(V_{low}\) and \(V_{high}\) is to vary the PWM duty cycle and apply some filtering (RC)
The project files are available here
Note
As a rule of thumb:
Transient simulation of a PWM-to-analog converter