DACs (Digital-to-Analog Converters) reverse the ADC process by converting digital numbers back into analog voltages, enabling microcontrollers to control motors, dim LEDs, and generate audio. Most microcontrollers lack true DACs and instead use PWM (Pulse Width Modulation) – rapidly toggling a pin on and off where the duty cycle determines the average output voltage. Choose a true DAC for precision applications like audio, and PWM for LED dimming and motor control where inherent filtering smooths the output.
Key Concepts
DAC (Digital-to-Analog Converter): Converts a digital number to a proportional analog voltage; output = (digital_value / 2^N) x Vref; used for audio output, analog reference generation, and variable voltage control in IoT systems
PWM as a Low-Cost DAC Alternative: A PWM signal filtered through an RC low-pass filter produces a smoothed analog voltage proportional to duty cycle; effectively a 1-bit DAC with the filter providing averaging; low cost but limited bandwidth and added ripple
PWM Ripple: The residual AC component on a PWM-derived analog voltage caused by incomplete filtering; ripple voltage = Vcc x (1 - duty_cycle) x e^(-T/(RC)); reduced by larger RC time constant but at the cost of slower response time
DAC Resolution: The smallest output voltage step = Vref / 2^N; a 12-bit DAC with 3.3 V reference resolves 0.8 mV per step; ESP32 has a built-in 8-bit DAC (GPIO 25, 26) providing 256 steps (12.9 mV per step at 3.3 V)
Voltage Reference: The reference voltage determining the DAC output range; a stable, low-noise voltage reference (LM4040, LM336) is essential for accurate DAC output; using the noisy MCU VCC as reference introduces supply noise into the DAC output
R-2R Ladder DAC: A passive resistor network implementing a DAC using only two resistor values (R and 2R); can be built from standard resistors to create a simple N-bit DAC from N GPIO pins; popular for low-cost audio and control applications
PWM Frequency vs. Ripple Trade-off: Higher PWM frequency with the same RC filter produces less ripple but the filter must still be designed for the application; typical trade-off: use 20-100 kHz PWM with a suitable RC filter to achieve smooth DC output with acceptable response time
Output Impedance: The DAC’s internal resistance seen from its output terminal; a high output impedance causes voltage drop when current is drawn by the load; buffer the DAC output with a unity-gain op-amp for low-impedance drive capability
17.1 Learning Objectives
By the end of this section, you will be able to:
Explain DAC Operation: Describe how digital-to-analog converters translate binary values into proportional voltages
Apply DAC Formulas: Calculate output voltage from digital input using the DAC transfer equation
Implement PWM Output: Generate pulse width modulation signals as pseudo-analog output for actuator control
Design RC Filters: Compute component values for low-pass filters that convert PWM to smooth analog voltage
Interface Actuators: Wire motors, LEDs, and other analog loads to DAC and PWM outputs
Evaluate DAC vs PWM Tradeoffs: Select the appropriate output method based on application requirements and constraints
For Beginners: DAC and PWM Output
While ADCs translate sensor signals into numbers a computer can read, DACs do the reverse – they turn digital numbers back into smooth voltages that can control things like speakers and motors. Most microcontrollers use a shortcut called PWM instead, which rapidly flickers power on and off. To a motor or LED, this flickering feels like a steady, adjustable voltage because it happens too fast for them to notice the individual pulses.
17.2 Prerequisites
Before diving into this chapter, you should be familiar with:
ADC Fundamentals: Understanding ADC operation helps understand DAC as the inverse process
Suitable for LED dimming and motor speed control where high-frequency switching is acceptable
ESP32 offers 1-16 bit PWM resolution; max frequency depends on resolution (e.g., ~312 kHz at 8-bit, ~1.2 kHz at 16-bit)
Cost: $0.05 (RC filter components)
Consumes <0.1mA average for low duty cycles
Decision Factors: Choose true DAC when driving audio amplifiers, generating test signals, providing reference voltages, or interfacing with analog-input devices sensitive to ripple. Choose PWM when controlling inductive loads (motors inherently filter PWM), dimming LEDs (human eye cannot perceive >100Hz flicker), or when all DAC pins are already used. Note: ESP32 has only 2 DAC channels but 16+ PWM-capable GPIOs, making PWM more flexible for multi-channel applications.
17.7 Arduino DAC/PWM Implementation
17.7.1 Arduino Uno (No True DAC)
Solution: Use PWM (Pulse Width Modulation) to simulate analog output
Frequency: ~490 Hz (pins 3,9,10,11) or ~980 Hz (pins 5,6)
Not true analog but works for LEDs, motors
17.7.2 ESP32 (True DAC)
DAC Pins: GPIO25, GPIO26
// True analog output on ESP32void setup(){// No setup needed for DAC}void loop(){// Output ~1.66V on DAC channel 1 (GPIO25) dacWrite(25,128);// 0-255, Vref = 3.3V// 128/255 * 3.3V = 1.66V}
Try It: ESP32 DAC Output Simulation
Experience true analog output with the ESP32 DAC in Wokwi:
Challenge: Generate a smooth voltage sweep from 0V to 3.3V
Steps:
Add code in the loop: dacWrite(25, value) where value sweeps from 0 to 255
Connect GPIO25 to an LED with 220Ω resistor (observe smooth brightness change)
Verify the voltage formula: \(V_{out} = \frac{value}{255} \times 3.3V\)
For value=128: \(V_{out} = \frac{128}{255} \times 3.3V \approx 1.66V\) (measure with multimeter in simulation)
Compare this smooth DAC output to PWM’s rapid on/off switching
What to observe: True DAC provides genuine analog voltage with no high-frequency switching, ideal for audio and precision applications where PWM ripple would cause problems.
17.8 PWM as Pseudo-DAC
Most microcontrollers lack true DACs but use PWM (Pulse Width Modulation) to simulate analog output.
How PWM Works:
Rapidly toggle digital output between 0V and 5V
Duty cycle (% time HIGH) determines average voltage
Low-pass filter smooths PWM to quasi-analog signal
PWM duty cycle controls average voltage and perceived brightness
8-bit resolution gives 256 brightness levels
Human eye perceives brightness logarithmically, so linear PWM steps appear nonlinear
17.11 Common Pitfalls
Pitfall: Confusing DAC Resolution with Output Accuracy
The Mistake: Assuming an 8-bit DAC with a 3.3V reference produces exact voltages at every step (e.g., expecting exactly 1.650V for value 128).
Why It Happens: The formula \(V_{out} = V_{ref} \times D / (2^n - 1)\) gives the ideal voltage, but real DAC output is affected by integral nonlinearity (INL), differential nonlinearity (DNL), offset error, and gain error. An 8-bit DAC has 12.9 mV steps at 3.3V, but typical INL of +/-1 LSB means actual output can deviate by +/-12.9 mV from the ideal.
The Fix: Check the DAC IC datasheet for INL and DNL specifications. For precision applications, use a higher-resolution DAC (12-bit or 16-bit) and calibrate against a known reference voltage. ESP32 built-in DAC has approximately +/-1% accuracy without calibration.
Rule of Thumb: Usable accuracy is typically 2-3 bits fewer than advertised resolution once all error sources are combined.
Pitfall: PWM Frequency Too Low for the Application
The Mistake: Using the default Arduino PWM frequency (490 Hz on most pins) for motor control, resulting in audible motor whine, or for LED dimming causing visible flicker in video recordings.
Why It Happens: The default frequency is a compromise that works for basic demos but is too low for many real applications. Cameras recording at 30-60 fps can capture individual PWM cycles, and human hearing extends to ~20 kHz.
The Fix: Increase PWM frequency to match the application: - LED dimming: >200 Hz for flicker-free to human eye; >1 kHz for flicker-free on camera - DC motor control: >20 kHz (ultrasonic) to eliminate audible whine - Servo motors: Fixed at 50 Hz (industry standard), do not change
On ESP32, use ledcSetup(channel, freq, resolution) to set custom PWM frequency. On Arduino Uno, modify timer prescaler registers.
Rule of Thumb: Use the highest PWM frequency your hardware supports, keeping in mind that higher frequency reduces effective resolution.
For Kids: Meet the Sensor Squad!
DACs are like translators who speak the language of motors and lights!
17.11.1 The Sensor Squad Adventure: The Reverse Translator
Max the Microcontroller had a new challenge. “I know the motor needs to spin at 50% speed, and I can think in numbers like 128 out of 255. But the motor only understands smooth voltage, not step-numbers!”
Lila the LED agreed. “Same for me! I want to glow at half brightness, but I need a smooth amount of power, not a number!”
That is when DAC Danny showed up. “I am the opposite of ADC Andy! Andy translates smooth signals INTO numbers for Max. I translate numbers FROM Max back into smooth signals for motors and LEDs!”
Bella the Battery was impressed. “So you are a reverse translator!”
Danny smiled. “Exactly! When Max says 128, I convert that into 1.65 volts – just the right amount to make Lila glow at half brightness.”
But Sammy the Sensor noticed something. “Wait, Danny, you are not on every microcontroller. What happens when you are not around?”
Danny pointed to his friend PWM Pete. “Pete has a clever trick! He blinks the power ON and OFF really, really fast. If he is ON half the time and OFF half the time, the average power is 50% – and it happens so fast that Lila cannot even tell she is blinking!”
Lila blinked. “I had no idea! I thought I was glowing smoothly this whole time!”
17.11.2 Key Words for Kids
Word
What It Means
DAC
A reverse translator that turns numbers into smooth voltage
PWM
Blinking power ON and OFF so fast it looks smooth
Duty Cycle
How much of the time the power is ON (50% = half the time)
Motor Control
Changing how fast a motor spins using voltage
LED Dimming
Making a light brighter or dimmer
17.11.3 Try This at Home!
The Blinking Brightness Game!
Get a flashlight and a dark room
Turn the flashlight ON for 1 second, then OFF for 1 second. Repeat. (That is 50% duty cycle – but you can SEE the blinking!)
Now try blinking it ON for half a second, OFF for half a second. Faster!
Keep going faster and faster until you cannot see the blinks anymore
What you learned: When blinking is fast enough, your eyes blend it into steady light. That is exactly how PWM works – it blinks so fast (hundreds of times per second) that LEDs and motors respond smoothly!
Matching Quiz: DAC and PWM Concepts
Ordering Quiz: PWM to Smooth Analog Voltage Pipeline
Knowledge Check: DAC and PWM
How It Works: ESP32 DAC Converting Digital 200 to Analog 2.59V
The big picture: A DAC is the reverse of an ADC - it uses an R-2R resistor ladder to convert an 8-bit digital value (0-255) into a proportional analog voltage (0-3.3V).
Step-by-step breakdown:
Digital Input: Code calls dacWrite(25, 200) - 200 in binary is 11001000 - Real example: ESP32 GPIO25 DAC receives this 8-bit value
Resistor Ladder: Each bit switches a 2R resistor to either Vref or ground, creating weighted currents - Real example: Bit 7 (MSB) contributes Vref/2 = 1.65V, bit 6 contributes Vref/4 = 0.825V, and so on with each successive bit halving
Summing Amplifier: Op-amp sums all bit contributions to produce output voltage - Real example: V_out = 3.3V × (200/255) = 2.59V
Why this matters: Understanding that PWM average voltage (3.75V at 75% duty cycle) requires RC filtering to become smooth DC explains why motors work with PWM directly (coil inductance acts as filter) but audio needs a true DAC.
Concept Check: DAC vs PWM for LED Brightness Control
Concept Relationships: DAC/PWM and Actuator Control
Concept
Relates To
Relationship
DAC Resolution (8-bit)
ADC Resolution
Same 2^n formula - 8-bit DAC has 256 output levels, 12-bit ADC has 4,096 input levels
PWM Duty Cycle
Motor Speed Control
Average voltage = V_high × duty_cycle - motors respond to average, not individual pulses
RC Filter Cutoff Frequency
Sampling Theory
Filter fc should be 10× below PWM frequency to smooth ripple (inverse of Nyquist)
Cross-module connection: PWM Actuator Control - Explains how DC motors respond to PWM average voltage via back-EMF and inductance
Key Takeaway
DACs and PWM are the two main ways IoT microcontrollers produce analog-like output signals for actuator control. True DACs provide smooth voltage output ideal for audio and precision applications, while PWM offers a flexible, cost-free alternative for LED dimming and motor speed control available on every GPIO pin. Understanding the tradeoffs between these approaches is essential for designing effective IoT actuator interfaces.
Decision Framework: When to Use True DAC vs PWM for Actuator Control
Use this decision table to choose the right output method based on your application requirements.
Application Requirement
True DAC
PWM
Recommended Choice
Audio output (music, voice)
✓ Smooth analog, >90dB SNR possible
✗ High-frequency ripple audible as hiss
DAC — Audio requires clean signals
LED brightness control
✓ Flicker-free at any frequency
✓ Flicker-free above 100 Hz
PWM — Saves DAC pins, perceptually identical
DC motor speed control
✓ Smooth speed, no torque ripple
✓ Motor inductance filters PWM naturally
PWM — Motor coils act as low-pass filter
Servo motor positioning
~ Acceptable but not standard
✓ Industry standard 50 Hz PWM signal
PWM — Servos expect PWM input (1-2 ms pulse width)
Precision voltage reference
✓ Stable DC output, <1 mV noise
✗ Ripple voltage 2-10% even with RC filter
DAC — Reference voltages must be clean
Analog sensor simulation (testing)
✓ Can mimic smooth sensor output
~ Only for slowly-changing signals
DAC — Faster signals need true analog
Multi-channel output (8+ channels)
✗ Most MCUs have only 1-2 DAC pins
✓ Every GPIO can generate PWM
PWM — Scalability advantage
Battery-powered application
~ Consumes 0.5-5 mA continuous
✓ <0.1 mA average at low duty cycles
PWM — Lower average current
Key Decision Factors:
Choose True DAC When:
Signal purity matters: Audio, instrumentation, reference voltages
High-frequency content: Signals faster than 1 kHz with harmonics
Interfacing with analog circuits: Op-amps, comparators expecting DC levels
Savings: $36 by using PWM where perceptually/functionally equivalent
Rule of Thumb: Start with PWM for actuators (motors, LEDs, heaters). Only use true DAC when you observe problems (audible noise, instability, ripple voltage) or need to interface with analog circuitry expecting clean DC.
17.12 See Also
ADC Fundamentals — DAC is the inverse process - understanding ADC helps grasp DAC operation
PWM Actuator Control — Hands-on lab demonstrating PWM for DC motor speed control
Power Budget Calculator — Factor in DAC continuous current (0.5-5mA) vs PWM average current for battery life
Label the Diagram
💻 Code Challenge
17.13 Summary
This chapter covered Digital-to-Analog Converters (DACs) and PWM output for actuator control:
DAC Operation: Converts digital values to proportional analog voltage