Home / Circuit lab / PWM to analogue voltage

PWM and RC smoothing filter simulator

A microcontroller pin can only be on or off, but switch it fast enough and smooth it, and you get any voltage in between. Change the duty cycle and the filter and watch the ripple.

Intermediate

Vout

Challenges

  1. Make 2.5 V (within 1%) with under 50 mV of ripple.
  2. At 490 Hz and 50% duty, keep the ripple under 100 mV and settle in under 200 ms.
  3. At 20 kHz and 50% duty, get under 10 mV of ripple while settling in under 50 ms.
  4. Make 1 V (within 1%) with less than 20 mV of ripple.

Controls

Readings

What PWM is

Pulse width modulation switches a pin fully on and fully off many times a second. The fraction of each cycle spent on is the duty cycle. The average voltage is the duty cycle times the high voltage:

Vaverage = duty cycle × Vhigh

So 50% duty on a 5 V pin averages 2.5 V. On an Arduino Uno, analogWrite(pin, value) sets the duty cycle to value ÷ 255, at about 490 Hz on most PWM pins and about 980 Hz on pins 5 and 6. Motors and LEDs do the averaging themselves: a motor's inertia and your eye's persistence smooth out the pulses. Our guide to PWM covers those uses.

Making a real analogue voltage

Some things need a genuinely steady voltage: a reference for another chip, a control input that expects 0 to 5 V, or a simple audio output. A resistor and capacitor make a low-pass filter (see the RC filter lab). When the pin is high, the capacitor charges through the resistor. When it's low, it discharges. If the time constant τ = R × C is much longer than the PWM period, the capacitor voltage hardly moves in one cycle and settles at the average.

The trade-off: ripple against speed

What's left is a small sawtooth called ripple. For a time constant much longer than the period, it's about:

ripple ≈ Vhigh × D × (1 − D) ÷ (f × R × C)

It's worst at 50% duty. A bigger R × C reduces ripple, but the output then takes longer to respond when you change the duty cycle: about five time constants to settle. You can't have both with one RC stage. The ways out are a higher PWM frequency (the ripple falls in proportion, which is why the 20 kHz option is so much easier), or a two-stage or active filter.

Practical points

  • Draw very little current from the output. A load in parallel with the capacitor forms a divider with R and lowers the voltage. Buffer it with an op-amp if you need current.
  • The output is only as accurate as the pin's high voltage, which sags slightly under load and follows the supply.
  • Resolution is limited by the PWM: 8 bits on an Arduino gives 256 steps of about 20 mV.
  • For a cleaner analogue output, a DAC chip, or a microcontroller with a built-in DAC such as the ESP32, may be simpler.

How this simulation works

The pin is an ideal 0 V to 5 V square wave with no output resistance, and the filter has nothing connected to its output. The ripple and waveforms come from the exact steady-state solution of the RC circuit, not the approximation above, so they're right even when the filter is far too small. The settling time is five time constants, when the output is within 1% of its final value.

Keep exploring