Turning PWM into a real voltage with a resistor and a capacitor
- 192parts on the bench
- 25boards running now
- 1.00xreal time, on every board
An Uno has no digital-to-analog converter. analogWrite does not produce a voltage, it produces a square wave: pin 9 switches between 0 V and 5 V about 490 times a second and spends a chosen fraction of each cycle high. An LED or a motor is slow enough to average that for you. An op-amp, an analog input or a circuit expecting a steady voltage is not, and sees the square wave for what it is.
The fix is a low-pass filter, and the simplest one is a resistor followed by a capacitor to ground. The capacitor charges while the pin is high and discharges while it is low, and if the time constant is long compared with one PWM cycle, it settles at the average. The circuit above is the Uno’s built-in pwm sketch stepping pin 9 through eight duty cycles, 700 ms each, into 10 k and 10 uF, with the scope on both ends and a meter on the capacitor.
Press Run and watch the meter climb in steps: 0.31 V, 0.78 V, 1.56 V, 2.49 V and on up to 4.95 V, then back to zero and round again. Open the Scope tab to see the square wave on channel 1 getting wider as the voltage on channel 2 rises.
Want the step-by-step version? The lesson "Brightness from a digital pin" walks through this with checkpoints.
Open the lessonThe average, worked out
analogWrite takes a duty from 0 to 255, and the average voltage of the square wave is the supply times duty over 255. The sketch steps through 0, 16, 40, 80, 128, 176, 220 and 255, which is 0 V, 0.31 V, 0.78 V, 1.57 V, 2.51 V, 3.45 V, 4.31 V and 5.00 V.
The capacitor settles about one percent under each of those, 2.49 V at a duty of 128 rather than 2.51 V, and the reason is the same one the voltage divider page is about. The filter’s output is a source with 10 k behind it, and the meter’s 10 M and the scope’s 1 M probe hang on it as a load of about 909 k. 10 k against 909 k costs 1.1 percent. Anything you connect to the filter will take its share the same way.
Picking R and C: ripple against speed
Tau here is 10 k x 10 uF = 100 ms. One PWM cycle is 1 / 490 Hz = 2.04 ms, so the capacitor only moves a little in each cycle, and that little movement is the ripple left on the output. For a square wave into an RC with tau much longer than the period, the peak-to-peak ripple is roughly Vcc x D x (1 - D) x T / tau. At a duty of one half that is 5 V x 0.25 x 2.04 ms / 100 ms, about 25 mV, which is under one percent of the output.
The price is speed. Five tau is 500 ms, so every time the duty changes the output takes half a second to arrive, which is why the sketch waits 700 ms on each step. Make the capacitor ten times smaller and the output follows ten times faster with ten times the ripple. There is no free choice with a single RC stage: you choose where on that line you want to be, or you add a second stage, which cuts the ripple far more than it slows the response.
The same trade looks different in the frequency domain. This filter’s corner is 1 / (2 pi x 0.1 s) = 1.6 Hz, about three hundred times below 490 Hz. A single RC falls by a factor of ten for every factor of ten in frequency, so the 490 Hz square wave comes out about three hundred times smaller than it went in, and the average, which is 0 Hz, comes out untouched.
What the scope and the pins show
At 1 ms per division, channel 1 is about five cycles of square wave, and the Scope tab’s duty measurement reads the fraction that is high. Channel 2 is nearly a flat line. Zoom its volts per division right in and you can see the ripple as a small sawtooth, rising while pin 9 is high and falling while it is low.
The sketch also drives pin 13, the board’s own LED, from the same numbers with digitalWrite: on above half, off below. Pin 13 has no timer behind it on an Uno, so it can only be all the way on or all the way off. That is the whole difference between a PWM pin and any other.
Common mistakes
Using the filtered voltage to power something. The output is a voltage behind 10 k, so the first few milliamps of load will drag it down; if you need current, follow the filter with an op-amp buffer. Forgetting that not every pin is a PWM pin: on an Uno only 3, 5, 6, 9, 10 and 11 are, and 5 and 6 run at about 980 Hz rather than 490. And an electrolytic capacitor fitted backwards, which on a real bench can heat up and vent: the stripe goes to ground.
What to try next
In the editor, change the capacitor to 1 uF and watch the ripple on the scope grow as the steps get quicker. Then look at a board that has a real DAC. The Uno R4 has a 12-bit one on A0, and the classic ESP32 has two 8-bit ones on GPIO 25 and 26, so on those a steady voltage is one function call away with no filter at all.
Questions
Can an Arduino Uno output a real analog voltage?
Not directly: the Uno has no DAC, and analogWrite gives a PWM square wave. An RC low-pass filter turns that into a steady voltage equal to 5 V times duty over 255, with a little ripple. The Uno R4 and the classic ESP32 do have DACs.
What resistor and capacitor should I use to filter PWM?
Pick a time constant ten to a hundred times the PWM period. For the Uno’s 490 Hz, 10 k and 10 uF give 100 ms, about 25 mV of ripple at half duty and half a second to settle. Smaller C is faster with more ripple.
Why does the filtered voltage take time to change?
The capacitor has to charge or discharge through the resistor, which takes about five time constants to finish. With 10 k and 10 uF that is half a second. It is the same property that removes the ripple.
Build this for real
Open the editor, change a value and watch the number move with it. Nothing to install, and no account needed.