Making a voltage: DAC and filtered PWM
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
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.
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)
اقرأ الدرس خطوة بخطوة
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)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() {}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 VA 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 littleTime 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-offClimbing 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 scaleA 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, fasterA 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() {}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
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01On an Uno R3, analogWrite(9, 51) sets the duty cycle to 51/255. What is the average voltage on the pin?
51/255 = 0.2, and the average is duty × 5 V = 0.2 × 5 = 1.0 V. The pin itself is still only ever at 0 V or 5 V.
02A PWM filter uses R = 10 kΩ and C = 10 µF. What is its time constant τ = RC?
10,000 Ω × 0.000 010 F = 0.1 s. The output reaches about 63% of its final value after one τ and about 99% after 5τ = 0.5 s.
03You make the capacitor ten times bigger. What happens?
A bigger RC smooths the pulses better (about ten times less ripple) but takes ten times longer to settle. The average stays at duty × Vcc.
04An ESP32 DAC outputs 3.3 V × value / 255. Which value gives the closest to 2.0 V?
3.3 × 155 / 255 ≈ 2.006 V. Each step is about 12.9 mV, so exactly 2.000 V is not reachable with 8 bits.
05Why does the filter output sag when you connect a 10 kΩ load to it?
The 10 kΩ filter resistor and a 10 kΩ load form a divider, halving the average. An op-amp voltage follower after the filter fixes it.
المسرد
- PWM (pulse-width modulation)
- Switching a pin on and off at a fixed rate and setting the share of time it is on.
- Duty cycle
- The fraction of each PWM cycle the pin is high. 102/255 is 40%.
- Low-pass filter
- A circuit that lets slow changes (and steady voltages) through and blocks fast ones. A resistor and capacitor make the simplest one.
- Time constant (τ)
- τ = RC. After one τ a charging capacitor has covered about 63% of the way to its final voltage.
- Cut-off frequency
- Where a filter starts to block: 1/(2πRC) for an RC low-pass.
- Ripple
- The small up-and-down wobble left on a filtered output, here about 24.5 mV.
- DAC (digital-to-analog converter)
- A circuit that turns a number into a voltage. The ESP32 has two 8-bit DACs.
- Resolution
- The change in output for a change of 1 in the input: 3.3 V / 255 ≈ 12.9 mV for the ESP32 DAC.
الملاحظات والمصادر
- On the Uno R3, pins 3, 9, 10 and 11 run at about 490 Hz with duty = value / 255. Pins 5 and 6 share Timer0 with millis(), run at about 980 Hz in fast PWM and give slightly more than value / 255 (see the lesson “PWM: dimming an LED”).
- The 2.0 V assumes the pin swings exactly between 0 and 5 V. A real Uno on USB may run a little below 5 V, and the output scales with it; a DAC's output likewise follows its reference.
- 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.
- 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.
- Only the classic ESP32 and the ESP32-S2 have these DACs; newer chips such as the ESP32-S3 and C3 do not. The UNO R4 has a true DAC on pin A0, used through analogWrite(). The Signals lessons “Quantization and bit depth” and “DACs and reconstruction” explain how a DAC works inside.
- Arduino language reference: analogWrite() (source)
- Arduino AVR core: wiring.c (Timer1 prescaler 64, 8-bit phase-correct PWM)
- Microchip ATmega328P datasheet (7810D), §15.9.4 Phase Correct PWM Mode
- Texas Instruments SPRAA88A: Using PWM Output as a Digital-to-Analog Converter
- OpenStax University Physics II, §10.6 RC Circuits
- Espressif ESP-IDF: Digital To Analog Converter (DAC), ESP32
- Espressif Arduino-ESP32: DAC API (dacWrite)
- J. W. Valvano, Embedded Systems: Shape the World, ch. 13: DAC and Sound (UT Austin)
اختبار يتاح بعد الخطوة الأخيرة.