From PWM to voltage

The Arduino library has always had an “analog Write()” function, even though the AT mega doesn’t have any way to generate a varying voltage. So why the name?
Well, what most microcontrollers can do is generate a pulse-width modulated signal, also known as PWM. That’s just a fancy way of saying that the microcontroller periodically generates a pulse, of which the width can be varied under software control.
Here’s a diagram from the Arduino PWM tutorial page:
It’s very easy to generate an approximate voltage between 0 and VCC by simply adding a resistor and capacitor to act as low-pass filter:
From PWM to voltage
That image came from an excellent article by Scott Daniels about this same topic on the website. Check it out for much more background information.
So how do these two little components turn PWM into a voltage?
To see what’s going on, we can tie the resistor to a PWM pin, call “analog Write(N,128)” to set a 50% duty cycle, i.e. halfway, and then watch the result on the oscilloscope:

(the above was generated with a 10 kΩ resistor and 0.1 µF capacitor – see this calculator)

What you’d want to see, is a 1.65 V output. As you can see, the real thing is indeed centered around that value, but with a lot of “ripple” (yellow = PWM, blue = Vout).

The reason for this is that the PWM is charging and discharging the capacitor. The average value will be the ratio of on-to-off time against the digital voltage, i.e. 3.3V, so a 50% duty cycle will lead to half VCC, i.e. 1.65V. But it’s still charging & discharging.

Read More: From PWM to voltage

Leave a Comment

Your email address will not be published. Required fields are marked *