ENع
←DAC and filtered PWM
Lesson 24 · Analog: ADC, DAC and sensors

Making a voltage: DAC and filtered PWM

12 min9 steps5 questionsIntermediate
About this lesson

A microcontroller pin can only be at 0 V or the supply, so how do you make 2.0 V? Two ways. On an Arduino Uno R3, analogWrite(9, 102) switches pin 9 on and off 490 times a second with a 40% duty cycle, and a 10 kΩ resistor with a 10 µF capacitor smooths that into 2.0 V, with a little ripple and a wait of about half a second. On an ESP32, a real 8-bit DAC does it in one line: dacWrite(25, 155) gives 2.006 V. The lesson “PWM: dimming an LED” covers the timer behind analogWrite, and the Signals lessons “Quantization and bit depth” and “DACs and reconstruction” explain the converter inside.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig 24 · Interactive
01 / 09

A pin is on or off

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.

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)
Loading figure…
or ← →
Read the lesson step by step
  1. A pin is on or off

    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.

    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)
  2. analogWrite switches, fast

    analogWrite(9, 102) does not make a voltage. A hardware timer switches pin 9 on and off 490 times a second. In each 2.04 ms cycle it is high 102/255 = 40% of the time: the duty cycle.

    line 2    analogWrite(9, 102);
    Timer1    8-bit phase-correct PWM, prescaler 64 (Arduino core)
    frequency 16 MHz / (2 * 64 * 255) = 490.2 Hz
    period    2.04 ms, high for 0.816 ms
    duty      102 / 255 = 0.40 = 40 %
    
    // Arduino Uno R3: 2.0 V from pin 9 through 10 kOhm + 10 uF
    void setup() {
      analogWrite(9, 102);   // 102 / 255 = 40 % duty
    }
    void loop() {}
  3. The average is 2.0 V

    Averaged over a cycle the pin gives duty × 5 V = 0.4 × 5 = 2.0 V. An LED or a motor averages by itself. A circuit that needs a steady voltage does not: at any instant it sees 0 V or 5 V.

    average   duty * Vcc = 0.40 * 5 V = 2.0 V
    LED, motor   slow to react: they feel the average
    ADC sample   catches 0 V or 5 V, not 2.0 V
  4. A resistor and a capacitor

    Add a 10 kΩ resistor and a 10 µF capacitor to ground: a low-pass filter. While the pin is high, C charges a little through R; while it is low, C gives a little back. C cannot follow fast pulses, so it holds the average.

    pin 9 --[ R 10 kOhm ]--+--> out
                         |
                       [ C 10 uF ]
                         |
                        GND
    pin high  C charges a little
    pin low   C discharges a little
  5. Time constant and cut-off

    The filter's time constant is τ = RC = 10 kΩ × 10 µF = 0.1 s. Its cut-off is 1/(2πRC) ≈ 1.6 Hz. The 490 Hz pulses are about 310 times faster, so most of them are filtered out; the steady average passes.

    tau       R * C = 10 000 * 0.000 010 = 0.1 s
    cut-off   1 / (2 * pi * R * C) = 1.59 Hz
    PWM       490.2 Hz  =  about 310 x the cut-off
    average   0 Hz: passes
    one RC    falls off 20 dB per decade above the cut-off
  6. Climbing to 2.0 V

    After analogWrite, the output climbs like a charging capacitor: 1.26 V (63%) after one τ, 1.99 V after 5τ = 500 ms. So filtered PWM cannot change faster than about half a second. A smaller RC is faster.

    vout(t)   2.0 * (1 - e^(-t / 0.1))
    t = 0.1 s   1.26 V   (63 %)
    t = 0.5 s   1.99 V   (99.3 %)
    the plot shows the average; the ripple is too small to see at this scale
  7. A little ripple remains

    Each pulse still nudges C up and down: about 24.5 mV peak to peak, 1.988 to 2.012 V. A bigger RC means less ripple but a slower output; a smaller one, the reverse. That trade-off is the price of filtered PWM.

    ripple    1.988 V .. 2.012 V  =  24.5 mV peak to peak
    approx    Vcc * D * (1 - D) * T / tau = 24.5 mV
    bigger RC   less ripple, slower
    smaller RC  more ripple, faster
  8. A real DAC on the ESP32

    A DAC, or digital-to-analog converter, turns a number straight into a voltage. The ESP32 has two 8-bit DACs, on GPIO25 and GPIO26. dacWrite(25, 155) gives 3.3 V × 155/255 ≈ 2.006 V: no pulses, so no filter to wait for.

    line 4    dacWrite(25, 155);
    ESP-IDF   out = Vref * value / 255,  Vref ideally = VDD = 3.3 V
              3.3 * 155 / 255 = 2.006 V
    channels  GPIO25 and GPIO26, 8 bits each
    
    // ESP32 (Arduino core): 2.0 V straight from the DAC on GPIO25
    void setup() {
      dacWrite(25, 155);    // 3.3 V * 155 / 255
    }
    void loop() {}
  9. 256 levels, either way

    8 bits give 256 levels, 0 to 255. One step is 3.3/255 ≈ 12.9 mV on the ESP32 and 5/255 ≈ 19.6 mV for Uno PWM. So exactly 2.000 V is out of reach: 154 gives 1.993 V, 155 gives 2.006 V.

    levels    2^8 = 256  (0 .. 255)
    ESP32     3.3 V / 255 = 12.9 mV per step
    Uno PWM   5 V / 255 = 19.6 mV per step
    154  ->  1.993 V
    155  ->  2.006 V
    156  ->  2.019 V

Quiz Unlocks after the last step.