Back to all articles
Embedded SystemsNew

PWM to Analog Voltage: STM32 Timer and RC Filter Design

A PWM pin and one RC filter make a cheap DAC. Here is the full trade on an STM32G0 at 64 MHz: counts per period, ripple under one count, the settling time that buys, the load, and when a second stage or a real DAC wins.

December 3, 2026·10 min read·STM32, PWM, RC filter, DAC, embedded firmware, analog design

Run the timer with no prescaler so each PWM period holds as many counts as you need, set the compare register for the duty, and follow the pin with an RC low-pass whose ripple stays under one count. The STM32 Timer Calculator gives the registers. The cost is response time: about 0.2 s for 11.6 bits at 20 kHz.

The numbers below assume an STM32G0 (RM0444, the STM32G0x1 reference manual), whose system clock tops out at 64 MHz. With the APB prescaler at 1 the timer clock equals the bus clock, so the timer counts at 64 MHz. The PWM is edge-aligned, PWM mode 1, on a 3.3 V rail, driven by a pin whose output resistance is far below the filter resistor, into no load until step 4. Other families and clock trees give other numbers; the method is the same.

The worked example: 20 kHz PWM from a 64 MHz timer

Pick 20 kHz, above the audio band, and keep the prescaler at 0. The timer then divides 64 MHz by 3,200, so ARR is 3199 and the compare register has 3,200 steps. One step on a 3.3 V rail is 1.03 mV.

Timer settings for 20 kHz PWM at a 64 MHz timer clock
QuantityValueWhere it comes from
Prescaler (PSC)0counter clock = 64 MHz / (PSC + 1)
Auto-reload (ARR)319964 MHz / 20 kHz = 3,200 counts, ARR = counts - 1
Resolution3,200 steps, 11.6 bitslog2(3,200)
One step1.03 mV3.3 V / 3,200
CCR for 25% duty8000.25 x 3,200, average 0.825 V
CCR for 1.000 V970round(1.000 / 3.3 x 3,200), gives 1.0003 V

Now the filter. Ripple is worst at 50% duty, so size for that and quote the duty with every ripple figure. Four candidates with a 10 kΩ resistor:

One RC stage after 3.3 V, 20 kHz PWM, R = 10 kΩ
CapacitorCornerRipple at 50%Ripple at 25%Settle to 1%
100 nF159.2 Hz41.25 mV (40 steps)30.94 mV4.605 ms
1 µF15.92 Hz4.12 mV (4 steps)3.09 mV46.05 ms
3.3 µF4.823 Hz1.25 mV (1.2 steps)0.94 mV152 ms
4.7 µF3.386 Hz0.88 mV (0.85 steps)0.66 mV216.4 ms

Only 4.7 µF keeps the worst-case ripple under one step. Quoted at 25% duty the 1 µF part shows 3.09 mV, three steps; at the 50% worst case it is 4.12 mV, four steps, which is why a ripple number without its duty cycle means little.

RC Filter Calculator
Cutoff frequency and time constant for a low-pass or high-pass, the nearest standard R or C for a target, ADC settling time and what a single pole does at Nyquist.
Start from these numbers: Low-pass, 10 kΩ, 4.7 µF, frequency of interest 20 kHz
Try the calculator: RC Filter Calculator

What a PWM DAC can and cannot do

The filtered output is the duty cycle times the voltage the pin swings, and that voltage is the supply rail. Resolution comes from the timer; accuracy comes from the rail. A 3.3 V rail that is 1% off moves full scale by 33 mV, which is 32 steps of this example. So 11.6 bits of resolution sit on about 6.6 bits of absolute accuracy unless you measure the rail and correct for it, or drive the filter from a buffer powered by a reference.

That makes a PWM DAC good for slow setpoints: a bias voltage, a comparator threshold, a contrast or brightness control, a trim. It is poor for waveforms, because the corner that removes the ripple sits at a few hertz. On the STM32G0x1 line the G031 and G041 have no DAC peripheral, while the G051 and above do (RM0444, Table 1), so on the small parts PWM is often the only analog output there is.

Step 1: pick the PWM frequency from the resolution you need

With PSC at 0 the number of steps is the timer clock divided by the PWM frequency. A prescaler only throws steps away. The STM32 Timer Calculator also covers the classic trap: the timer clock is twice the bus clock whenever the APB prescaler is not 1.

64 MHz timer clock, PSC = 0, 3.3 V rail: steps, and the filter that holds ripple to one step at 50% duty
PWM frequencyARRStepsOne stepτ for 1-step rippleSettle to 1%
1 kHz6399964,000 (16.0 bits)0.052 mV16 s73.68 s
10 kHz63996,400 (12.6 bits)0.52 mV160 ms736.8 ms
20 kHz31993,200 (11.6 bits)1.03 mV40 ms184.2 ms
50 kHz12791,280 (10.3 bits)2.58 mV6.4 ms29.47 ms
100 kHz639640 (9.3 bits)5.16 mV1.6 ms7.368 ms

Read the last two columns together. At a fixed timer clock, one extra bit halves the PWM frequency and doubles the steps, so the time constant that holds ripple to one step grows four times. That is the trade of a single-stage PWM DAC: each extra bit costs four times the response time.

One way out is a faster timer clock. On G0 parts that integrate the PLL's Q output (RM0444 notes that not all do), TIM1, and TIM15 where fitted, can run from it at up to 128 MHz, if PCLK comes from the PLL's R output and the Q output is a whole multiple of at least 2 of PCLK (RM0444, section 5.2.13). At 128 MHz, 20 kHz PWM has 6,400 steps, 12.6 bits, with the same filter rules.

Step 2: size the filter so the ripple is under one step

Where the formula comes from: when the PWM period is much shorter than τ = R x C, the output barely moves in one period. During the high time the capacitor charges with a current of about Vpp x (1 - D) / R for D / f seconds, so it rises by Vpp x D x (1 - D) / (f x τ). That product peaks at D = 0.5, which gives the worst case, Vpp / (4 x f x τ). The RC Filter Calculator uses the exact expression; at f x τ above 5 the two agree within 0.1%.

For ripple under one step of a timer with N steps, set Vpp / (4 x f x τ) at or below Vpp / N, so τ must be at least N / (4 x f). At 20 kHz and 3,200 steps that is 40 ms: 4 µF with 10 kΩ. That is not a standard value; its E6 neighbours are 3.3 µF and 4.7 µF, so take 4.7 µF (0.88 mV, 3.386 Hz corner), or accept 1.25 mV with 3.3 µF. A 47 kΩ resistor with 1 µF gives the same 47 ms time constant as 10 kΩ and 4.7 µF, with a cost in step 4.

A ceramic capacitor of a few microfarads loses capacitance as its DC voltage rises, so τ, and with it the ripple, changes with the output level. Check the DC bias curve in the part's datasheet, or use a film capacitor.

Step 3: check the settling time you just bought

A single pole settles within 1% of a step in ln(100) = 4.605 time constants. With 10 kΩ and 4.7 µF that is 216.4 ms. Settling within one step of 3,200 takes ln(3,200) = 8.07 time constants, 379.3 ms. If the firmware changes the setpoint and then reads something that depends on it, wait that long, or the reading belongs to the old value.

Step 4: the load is part of the filter

Whatever reads the output sits across the capacitor and forms a divider with R. A 100 kΩ load on the 10 kΩ and 4.7 µF filter gives a DC gain of 0.909: the output reads 9.09% low, and the corner moves from 3.386 Hz to 3.725 Hz. A 1 MΩ load still leaves it 0.99% low. A fixed load is a gain error you can calibrate out; a load that changes with temperature or mode is not.

The fix is an op amp wired as a voltage follower after the capacitor, with inputs and output that reach the rails you need. If you also need a smaller span, a divider after the buffer keeps it out of the filter; the Voltage Divider Calculator gives its ratio and source resistance. The pin's own output resistance adds to R, which is one more reason to keep R well above it.

When one RC stage is not enough

There are three ways forward: raise the PWM frequency and give up steps (the table in step 1), add a second RC stage, or use a real DAC. A second stage changes the trade most, because it attenuates the PWM frequency a second time while each stage stays fast.

Two ways to hold 20 kHz ripple under one step (1.03 mV) on a 3.3 V rail
FilterRipple at 50%Settle to 1%
One stage, 10 kΩ and 4.7 µF0.88 mV216.4 ms
Two stages, 10 kΩ and 100 nF each, bufferedunder 0.33 mV6.638 ms

The two-stage numbers assume a buffer between the stages, so the second does not load the first. The first stage leaves 41.25 mV of ripple; the second has a 159.2 Hz corner and passes 20 kHz at -42.0 dB, so at most 0.33 mV gets through. Two equal poles settle within 1% in 6.638 time constants, 6.638 ms here, about 33 times faster than the single stage. Without that buffer, put the second stage at ten times the impedance (100 kΩ and 10 nF, the same 1 ms): it still loads the first a little, and settling stretches to 7.345 ms. The output then has a 100 kΩ source impedance, so it needs the buffer of step 4.

When the output must track a waveform or settle in microseconds, use the on-chip DAC where the part has one, or an external DAC. A DAC does not remove the rail error by itself: its full scale is its reference. On the G0 that is VREF+, which an external reference or, where fitted, the internal reference buffer can supply, but on packages without a VREF+ pin it is tied to VDD (RM0444, section 4.1). For accuracy without calibrating the rail, use a reference other than the supply.

Firmware checklist

  1. Use the timer clock, not the bus clock. On the G0 it equals PCLK when the APB prescaler is 1 and is twice PCLK otherwise (RM0444, section 5.2.13).
  2. PSC = 0 for full resolution; the counter clock is the timer clock / (PSC + 1). ARR = steps - 1.
  3. PWM mode 1 (OCxM = 0110): the output is high while CNT < CCR. CCR = 0 holds it low; a CCR above ARR holds it high, so CCR = 3200 gives the full rail with ARR = 3199.
  4. Set OCxPE and ARPE so a new CCR or ARR takes effect at the next update event, never mid-period, and set the UG bit once before starting the counter so the preloaded values are in place (RM0444, section 21.3.11).
  5. Compute CCR = round(V_target / V_rail x steps). If you measure the rail, use the measured value here: that wins back the accuracy the rail takes away.
  6. After changing CCR, wait the settling time of step 3 before trusting anything downstream.

If the analog output is one part of a larger product, see firmware development for the STM32 side and hardware design for the board around it.

Sources

  • RM0444 Rev 6, STM32G0x1 reference manual, STMicroelectronics, December 2024: Table 1 (DAC by device), section 4.1 (VREF+), section 5.2.7 (64 MHz system clock), section 5.2.13 (timer clock), sections 21.3.11 and 22.3.9 (PWM mode, preload), section 21.4.13 (prescaler).
  • Ripple, corner and settling figures: computed with the formulas behind the RC Filter Calculator; timer figures with the STM32 Timer Calculator.

Want the timer setup and the analog stage checked before layout? Book a call for a design review.

Frequently asked questions

What PWM frequency should I use for a PWM DAC?

Start from the resolution you need. With no prescaler, the steps per period are the timer clock divided by the PWM frequency: 3,200 steps, 11.6 bits, at 20 kHz from 64 MHz. Each extra bit halves the frequency and makes the filter four times slower, so use the highest frequency that still gives enough steps.

How much ripple will a PWM DAC have?

For one RC stage, the peak-to-peak ripple is about Vpp x D x (1 - D) / (f x τ), worst at 50% duty where it is Vpp / (4 x f x τ). With 3.3 V, 20 kHz, 10 kΩ and 1 µF that is 4.12 mV at 50% and 3.09 mV at 25%. Always state the duty cycle.

How many bits of resolution can a PWM DAC really deliver?

The timer sets the steps: 3,200 at 20 kHz from a 64 MHz clock, 11.6 bits, real only once ripple is under one step and the output has settled. Absolute accuracy depends on the rail and the load: into a buffer, a 1% error on 3.3 V is 32 steps, about 6.6 bits, unless firmware measures the rail.

Do I need an op amp buffer after the RC filter?

Usually yes. The load forms a divider with the filter resistor: 100 kΩ on a 10 kΩ filter reads 9.09% low, and even 1 MΩ reads 0.99% low. A rail-to-rail op amp wired as a follower removes that error and lets you keep the filter resistor high without the reading depending on what is connected.

When should I use the microcontroller's built-in DAC instead of PWM?

Use a DAC when the output must follow a waveform or settle in microseconds: a single-stage PWM DAC with 11.6 bits at 20 kHz needs about 0.2 s. Both follow supply error unless the DAC has its own reference. In the STM32G0x1 line the G031 and G041 have no DAC, while the G051 and above have one.

STM32 Timer Calculator
PSC and ARR for any timer frequency or period, with the real frequency, ppm error, PWM resolution in bits and the CCR for your duty cycle.
Try the calculator: STM32 Timer Calculator

Building something hardware-shaped?

We help hardware founders go from sketch to certified product. Bring the spec, or just the idea, to a free 30-minute call with a senior engineer. Prefer email? A senior engineer reads every one.