How to Get a DC Voltage from PWM.

Every beginner electronics enthusiast knows how to get a DC voltage from PWM. It's simple: pass the PWM through a low-pass filter (in the simplest case an RC network), and you get a DC voltage at the filter output, right?

In fact, I think it's all much more interesting: when trying to get a DC voltage from PWM, the following questions arise:

How do you choose the values of the filter components?

Will the PWM be smoothed out completely, or will ripple remain?

And how does it work at all, since the capacitor charges and discharges through the same resistor, and in theory, if the duty cycle is less than half, the voltage on the capacitor would be zero. For example, if the duty cycle is 30%, then for 30% of the period the capacitor charges and for 70% it discharges through the same resistor, and in the end nothing would be left on it; at least that's what you might think.

Let's check this in practice: to do this, we'll build the circuit shown below and connect the oscilloscope probes to points 1 and 2; note that the PWM period is an order of magnitude longer than the time constant of this network.
How to Get a DC Voltage from PWM.

How to Get a DC Voltage from PWM.


The oscillogram shows that this is indeed what happens: the capacitor discharged as quickly as it charged. So how do people get a DC voltage from PWM at all?

The only idea that suggests itself is to change the RC filter values; let's increase the resistor value by an order of magnitude, thereby increasing the RC time constant (now it will be equal to the PWM period), or in other words lowering the filter cutoff frequency.
How to Get a DC Voltage from PWM.

How to Get a DC Voltage from PWM.


Wow, something is starting to become clear: a DC component has appeared. So an error crept into our reasoning: the capacitor charges from 0 to 63% in a time equal to R*C (T), but it discharges from 63% to 5% in a time longer than 2T; below are graphs explaining this.
How to Get a DC Voltage from PWM.


The graphs show that the rate at which the capacitor charges and discharges is not constant and depends on the capacitor's charge; this property is what makes it possible to get a DC voltage from PWM.

Now that we've found the error in our reasoning, let's analyze what happened in the first experiment. It's known that a capacitor fully charges or discharges in a time equal to 5T, and charges to 95% or discharges to 5% in about 3T. Since the time constant of the RC network (which we used as a LPF) was small, the capacitor managed to almost fully charge and discharge within one PWM period.

After we increased the time constant of the network, its charge and discharge rates became different. For example, if the capacitor discharged to 63% in time x, it needs more than 2x to discharge completely. You can look at the graphs above to understand this.

So the conclusion: the time constant of the RC network must be equal to or greater than the PWM period; then the capacitor won't fully charge and discharge within one period. If you increase the RC time constant by another order of magnitude, the settling time will increase and the ripple will decrease. The settling time is the time it takes for the voltage on the capacitor to change from 0 to some steady value. This conclusion is given for general understanding.

Now, roughly understanding how a DC voltage is obtained from PWM, let's move on to a real task.
We need to generate a reference voltage on one of the op-amp inputs using PWM and a LPF; the PWM logic high is 3 volts, the PWM frequency is 10KHz, and the acceptable ripple level is 30 millivolts. We assume that the op-amp inputs draw no current, and as the LPF we'll use a first-order filter implemented with an RC network.

The simplest way is to take an RC network whose T is two orders of magnitude greater than the PWM period, see what the ripple is, and then adjust the filter values, but that's nothing other than trial and error, and I'd like to calculate everything properly.
How to Get a DC Voltage from PWM.

So, to calculate it properly, let's work out how many times the signal needs to be attenuated: 3000/30 = 100, and convert this to decibels: we get -40dB.
How to Get a DC Voltage from PWM.


It's known that the roll-off of a first-order filter is 20dB/decade, and attenuating the signal by 40dB corresponds to increasing the frequency by two decades. (20dB/decade means the amplitude decreases 10 times (20dB) when the frequency increases 10 times (a decade).)
How to Get a DC Voltage from PWM.

Knowing that the filter cutoff frequency must be two decades (100 times) below the PWM frequency, we can calculate it: 10KHz/100 = 100Hz.

The filter values can be chosen using the well-known formula.
How to Get a DC Voltage from PWM.

Let's take a resistance of 16K and a 100nF capacitor.
Let's check what we get in practice: build the circuit shown below and connect to points one and two.
How to Get a DC Voltage from PWM.

How to Get a DC Voltage from PWM.

And let's draw the Bode magnitude plot of our circuit.
How to Get a DC Voltage from PWM.


This generator has a switching power supply that is very noisy; you can see this in the second channel, but if you look closely, you can see that the ripple amplitude on the oscillogram is about 40 millivolts, i.e. slightly different from the calculated value. That's normal, since the PWM contains higher harmonics that contribute, and the roll-off isn't exactly 20dB/decade everywhere, as the Bode plot shows. Despite
some assumptions, this calculation seemed very simple and clear to me: with simple logical reasoning and school formulas, we solved such an interesting problem. When solving this problem, it's important to understand the physical meaning: essentially, on the frequency response of an abstract filter we find the point corresponding to the required signal attenuation; the second coordinate of the point is the frequency, which must equal the PWM frequency. This way we find one point of the filter's frequency response, use it to find the cutoff frequency, and knowing that, we find the filter values; that's all.