# How does PWM generate an analog signal?

AFAIU, PWM works as follows:

According to the value of the counter in the microcontroller, a square wave is generated at a designated output pin of the $\mu C$. The duty cycle of this square wave is determined by the previously set compare value of the counter and the current value of the counter.

So, at a given time, if the counter is, say, higher than the compare value, the square wave at the designated output will be at high voltage ($V_H$). Similarly, if counter is lower than the compare value, the square wave at the designated output will be at low voltage ($V_L$).

I have heard that we are able to simulate analog output using PWM. For example, if the duty cycle of the output square wave is, say 60%, then AFAIK, we treat the output signal as if it is $(V_H - V_L) * duty cycle$ for the whole duration of that PWM period.

But how is this possible? In reality, a 60% duty cycle means that the output signal is fully at $V_H$ for 60% of the PWM period and it is fully at $V_L$ at the remaining 40% of the PWM period.

How are we able to treat the output signal as if it is $(V_H - V_L) * duty cycle$ for the whole of the period?

• low pass filter Apr 14, 2016 at 19:24
• @JImDearden Could you elaborate with an answer if you have time to do so?
– Utku
Apr 14, 2016 at 19:24
• @Utku, try it for yourself. In simulation or real life, apply a low pass filter (with cut-off frequency at least, say, 1 decade below the PWM signal's frequency) to a PWM signal. What do you see? Apr 14, 2016 at 19:26
• The PWM is a square wave with a DC component, the higher the duty cycle, the higher the DC component. With an LPF, you can leave the DC component alone. Apr 14, 2016 at 19:26

## 1 Answer

I'll answer, qualitatively, that a low pass filter (i.e. an RC filter or something more sophisticated), acts to smooth out variations over time (more or less aggressively based on its designed time constant). A step function (i.e. a theoretically pure square wave is a composition of these) will be transformed into a more gradual "ramp" (not being precise here on purpose) by a low pass filter. If instead of a step function, you imagine a PWM signal that changes more rapidly than the time it takes that "ramp" to reach it's final value in the case of a step response, it's plain to see that the output of the low pass filter will be somewhere between the high and low value of the square wave. Where exactly that voltage settles is an direct function of the duty cycle, and you can prove that to yourself mathematically or through simulation. There's also obviously some distorting relationship between the rise-time of the filter's step response and the period of the PWM signal, but that's a second-ordery consideration. I might get myself into trouble here, I'm a bit rusty, but I think an important point here is that practical low pass filter are not memoryless.

• ..acts to smooth out variations over time.. in other words: Averging A well chosen low-pass filter will output the average of the PWM signal. I fully agree with the answer, just wanted to add the term averaging to it for clarification. Apr 14, 2016 at 20:17