D9GND25GNDUNO R3ATmega328P10 kΩESP32DAC 8-bit0.00 V10 µF0.000 V1// Uno R3: PWM on pin 92analogWrite(9, 102);3// ESP32: DAC on GPIO254dacWrite(25, 155);
0123456012345t (ms)VPIN 9 VOLTAGEpin 9, 40% dutyaverage 2.0 V00.10.20.30.40.50.600.511.522.5t (s)VFILTER OUTPUTvout (average)
Back left: Uno chip, pin 9, R, C and a voltmeter (the RC filter)Back right: ESP32 chip and a voltmeter on its DAC outputDots: charge leaving pin 9 only while it is HIGH (slowed down), then a steady outputPlots: pin 9's voltage and the filter output, computed from the modelLeft: an Arduino Uno R3 makes 2.0 V with analogWrite(9, 102): 40% duty at 490 Hz, smoothed by a 10 kΩ, 10 µF low-pass filter (τ = 0.1 s, ripple 24.5 mV). Right: an ESP32's 8-bit DAC makes 2.006 V directly with dacWrite(25, 155). Below: pin 9's square wave and its average, and the filter output settling.

PWM is not a voltage

  1. An Uno pin is digital: 0 V or 5 V, nothing between. Yet a sensor test, a motor driver or an audio circuit may want 2.0 V. We make 2.0 V two ways: filtered PWM on an Uno, then a real DAC on an ESP32.

Filter it

A real DAC

What happens

;; step 1/9 · on-off
pin       digital: LOW = 0 V or HIGH = 5 V
goal      a steady 2.0 V
way 1     PWM + RC filter     (Arduino Uno R3, pin 9)
way 2     a real DAC          (ESP32, GPIO25)

The output plot shows the average voltage. The real trace also carries 24.5 mV of ripple at 490 Hz, about one pixel at this scale. The filter output comes through 10 kΩ, so anything that draws current from it pulls the voltage down: a 10 kΩ load would halve it. Put an op-amp voltage follower after the filter (see the Circuits lesson “Op-amp circuits”), or use a DAC with a buffered output.