32 DAC and PWM: Output Foundations
32.1 Start With the Decision
A digital pin cannot produce every analog voltage on its own. The circuit must choose a DAC code or a PWM duty cycle that the load can use.
32.2 Route Overview
This is part 1 of 3. Continue with DAC and PWM: Loads, Filters, and Code.
32.3 Part Objectives
- Compute DAC resolution and quantization error.
- Compare true DAC output with PWM duty-cycle output.
-
Which output method fits each physical job?
32.4 Start Simple
Trace One Number Into a Real Output
Picture a plant lamp that should grow brighter as a control value rises. The computer holds a number, but the lamp needs voltage, current, and safe timing. The path between them decides whether the physical result matches the command.
An analog-to-digital converter means a circuit that turns a voltage into a number; it is called an ADC. A digital-to-analog converter (DAC) means a circuit that turns a number into a voltage or current. Pulse-width modulation means control made by changing how long a signal stays on; it is called PWM. A duty cycle means the share of each cycle spent on. Modulation means changing a signal to carry or control information. An actuator means a part that creates a physical action.
Command minimum, middle, and maximum output. Then lower the supply, change the load, restart the controller, and hold the command at a limit. Measure the real output and keep the actuator in a safe range.
This runway does not size a driver or prove smooth motion. The deeper sections compare conversion, timing, filtering, loads, and the evidence needed to choose an output method.
Imagine a device that must dim a light, set a reference voltage, or drive a small actuator. A DAC produces a stepped analog voltage directly, while PWM makes a fast digital pulse train that a load or filter averages. Start with what the load actually responds to: voltage level, average power, ripple, timing, and acceptable noise.
- Start Simple
- In 60 Seconds
- Phoebe’s Field Notes: Why PWM Can Behave Like a DAC
- Key Concepts
- For Beginners: DAC and PWM Output
- Prerequisites
- Why This Chapter Matters
- How To Use This Chapter
- Digital-to-Analog Converters (DAC)
- Putting Numbers to It
- DAC Circuit Architecture
- Quantization Error in DAC
- Checkpoint: DAC Codes and Resolution
- True DAC vs PWM Comparison
- DAC vs PWM Output Tradeoff
32.5 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
32.6 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
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.
32.7 Prerequisites
Before diving into this chapter, you should be familiar with:
- ADC Fundamentals: Understanding ADC operation helps understand DAC as the inverse process
- Binary Fundamentals: Binary number systems and resolution
- Electricity Fundamentals: Understanding voltage, current, and basic circuits
32.8 Why This Chapter Matters
Follow these connections in order: Reading sensors is only half of an IoT system. Devices also need to create outputs: dim lights, drive motors, generate analog control voltages, and shape behavior in the physical world. In real hardware, this chapter explains why PWM is used so often, when a true DAC is worth the extra complexity, and what tradeoff you make when you smooth pulses into an analog-like signal. A beginner who understands this chapter can move from passive sensing projects to systems that actually control something.
32.9 How To Use This Chapter
- Read the DAC basics first so you understand the ideal case, then read PWM as the practical microcontroller shortcut.
- Tie each concept to a real job: LED dimming, fan speed control, audio output, or voltage reference generation.
- If the filtering discussion feels abstract, imagine what the load experiences: a motor averages pulses differently from a speaker or measurement input.
32.10 Digital-to-Analog Converters (DAC)
Start with the clean case: a digital code becomes a proportional voltage.
DAC = Device that converts digital numbers into analog voltage
Inspect Figure 32.1 with Digital N-bit and code k in mind. Look at how it presents dAC conversion contract from bounded digital code through reference and transfer function to measured analog output before applying digital-to-analog converters (dac).
Read DAC conversion: code, reference, and load first in Figure 32.1, locate Digital N-bit next, and use code k to interpret their connection. The drawing communicates dAC conversion contract from bounded digital code through reference and transfer function to measured analog output. For digital-to-analog converters (dac), these labels identify the boundary where an assumption must become a calculation or measurement.
32.10.1 DAC Formula
Example (8-bit DAC, Vref = 5V):
| Digital Input | Calculation | Voltage Output |
|---|---|---|
| 0 | 5 x (0/255) | 0.00 V |
| 64 | 5 x (64/255) | 1.25 V |
| 128 | 5 x (128/255) | 2.51 V |
| 192 | 5 x (192/255) | 3.76 V |
| 255 | 5 x (255/255) | 5.00 V |
For an 8-bit DAC with V, the step size (resolution) is mV. To output 3.0V, you need digital value . Actual output is V exactly. With a 12-bit DAC (), step size shrinks to mV — 16× finer than 8-bit, enabling smooth audio waveforms where 8-bit would sound grainy.
32.10.2 DAC Applications in IoT
| Application | Example | DAC Type |
|---|---|---|
| Audio Output | Music playback | 12-16 bit, 44.1 kHz |
| Motor Speed Control | PWM alternative | 8-10 bit |
| LED Brightness | Smooth dimming | 8-12 bit |
| Analog Sensor Simulation | Testing | 12 bit |
| Waveform Generation | Signal generator | 12-16 bit |
32.11 DAC Circuit Architecture
The running circuit argument reaches Figure 32.2, where digital-to-Analog Converter circuit diagram can be inspected directly before dac circuit architecture proceeds.
The visual sequence in Figure 32.2 begins at DAC Circuit, passes through Digital, and reaches Vref. That ordering expresses digital-to-Analog Converter circuit diagram. The dac circuit architecture discussion builds on it by following energy, signal, or material behaviour across the same labelled boundary.
R-2R Ladder Network:
- Uses only two resistor values (R and 2R)
- Each bit contributes proportional current
- Binary-weighted output voltage
- Low cost, moderate accuracy
32.12 Quantization Error in DAC
Quantization appears because an N-bit DAC can produce only one of code levels. Compare the requested smooth waveform with the nearest supported code, and use the step size to bound the ideal amplitude error. Increasing bit depth reduces that vertical step; increasing update rate reduces the time between code changes but does not create new voltage levels. A low-pass filter can attenuate step edges and PWM carrier energy, yet it also changes bandwidth, settling time, and load response. The design therefore selects resolution, update timing, and filtering together, then measures the actual output into the intended load rather than claiming that any one remedy makes the signal perfectly analogue.
Problem: DAC output is not truly analog - it’s discrete steps
Quantization Error = Difference between ideal smooth output and actual stepped output
Reducing Error:
- Increase bit depth (8-bit → 12-bit → 16-bit)
- Increase update rate (faster DAC updates)
- Add filtering (low-pass filter smooths steps)
Checkpoint: DAC Codes and Resolution
You now know:
- A DAC maps digital input to a fraction of Vref using ADC-style bit-depth logic.
- More bits mean smaller steps: 8-bit output has 256 levels, while 12-bit output has 4096 levels.
- Quantization error is the gap between the desired smooth value and the nearest stepped output value.
32.13 True DAC vs PWM Comparison
Now move from the ideal component to the practical choice: smooth DAC channel, or PWM plus averaging.
-
Start with how much ripple and delay the real load can take.
-
Compare a true smooth output with a filtered switch signal.
-
Test the chosen output on the real load before keeping it.
Option A (True DAC - ESP32 GPIO25/26, MCP4725):
- Produces genuine smooth analog voltage with no ripple
- Output impedance varies: <1 ohm for external DACs with op-amp output (e.g., MCP4725), ~10 kohm for ESP32 built-in DAC
- External DACs can drive resistive loads directly; ESP32 built-in DAC needs a buffer op-amp for low-impedance loads
- Ideal for audio (SNR >90 dB achievable with external 16-bit+ DACs; ESP32 8-bit DAC limited to ~50 dB theoretical max) and precision voltage reference
- ESP32 DAC resolution 8-bit (256 levels), MCP4725 offers 12-bit (4096 levels)
- Cost: $0 (ESP32 built-in) to $3-8 (external DAC)
- Consumes 0.5-5mA continuous
Option B (PWM Pseudo-Analog - Any GPIO):
- Digital square wave filtered to approximate DC
- Requires external RC filter (adds latency = 5x RC time constant)
- Output ripple typically 2-10% of amplitude
- 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.
32.14 Continue to the Next Part
Carry this evidence into DAC and PWM: Loads, Filters, and Code, which begins with PWM, DAC, and RC Simulator.
