32 DAC and PWM: Output Foundations
More
Depth: Practitioner
Progress saves as you read. Mark sections complete as you go; your progress appears in the sidebar.
32.1 Start With the Decision
This section: Start With the Decision — A digital pin cannot make every analog voltage on its own.
A digital pin cannot make every analog voltage on its own. A DAC turns a digital code into a voltage. PWM switches a pin on and off; its duty cycle is the share of time spent on. Choose the DAC code or PWM duty cycle that the load, the device using the output, can accept.
32.2 Route Overview
This section: Route Overview — This is part 1 of 3.
This is part 1 of 3. Continue with DAC and PWM: Loads, Filters, and Code.
32.3 Learning Objectives
This section: Learning Objectives — Inspect the story panel and ask which output method fits the physical load rather than the software command alone.
- Compute DAC resolution and quantization error.
- Compare true DAC output with PWM duty-cycle output.
Inspect the story panel and ask which output method fits the physical load rather than the software command alone.
-
Which output method fits each physical job?
32.4 Start Simple
This section: Start Simple — The panel leaves the two output paths unresolved.
The panel leaves the two output paths unresolved. The lamp, reference dial, and actuator do not all respond to the same electrical property: one may tolerate switched average power, another needs a stable voltage, and another brings current, inertia, and timing constraints. The two open paths therefore cannot be labelled DAC or PWM until the load’s ripple, delay, drive, and safety limits are known. This visual introduces the chapter’s comparison without declaring a universal winner. It asks the learner to trace a numeric command into voltage, current, and physical response, then choose the conversion method whose measured output satisfies that particular job. 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 How to Use the 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; for f_PWM RC much greater than one, peak-to-peak ripple is approximately Vcc x D x (1 - D) / (f_PWM 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
This section: Learning Objectives
- 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
This section: 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 Learning Objectives and Motivation
This section: Learning Objectives and Motivation — Follow these connections in order: Reading sensors is only half of an IoT system.
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
This section: 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)
This section: Digital-to-Analog Converters (DAC) — Start with the clean case: a digital code becomes a proportional voltage.
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
This section: DAC Circuit Architecture — Inspect Figure 32.2 and ask how many distinct output levels a four-bit code can select from a 5 V reference.
Inspect Figure 32.2 and ask how many distinct output levels a four-bit code can select from a 5 V reference.
Read Figure 32.2 from the four-bit input through the converter and reference to the stepped output. Four bits encode possible codes, spanning 0000 through 1111. With the illustrated 5 V reference, dividing the reference range by the code count gives per step, or 312.5 mV. Each code therefore selects one discrete output level rather than an arbitrary continuous voltage. This is the architectural boundary the chapter uses to compare a DAC with PWM: a DAC directly presents a code-dependent level, while filtered PWM synthesizes an average and brings ripple, load, and settling-time questions.
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
This section: 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)
This section:
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
This section: True DAC vs PWM Comparison — Now move from the ideal component to the practical choice: smooth DAC channel, or PWM plus averaging.
Now move from the ideal component to the practical choice: smooth DAC channel, or PWM plus averaging.
Inspect the three story panels and ask whether the real load tolerates the ripple and delay of a filtered switching signal.
-
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.
Read the story panels from load limits to measured acceptance. The first panel establishes ripple and response time before a technology is chosen. The second compares a true converter’s stepped voltage with a PWM waveform whose filter and load create an average; neither is automatically superior because channel count, drive, noise, cost, and delay differ. The third panel places the selected path on the real load and keeps it only when the trace fits those limits. This sequence is the decision rule for the detailed comparison below: calculate resolution or filter behavior, then verify the actual output under supply and load changes before committing the circuit.
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 depends on PWM frequency, duty cycle, filter, and load.
- 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
This section: Continue to the Next Part — Carry this evidence into DAC and PWM: Loads, Filters, and Code, which begins with PWM, DAC, and RC Simulator.
Carry this evidence into DAC and PWM: Loads, Filters, and Code, which begins with PWM, DAC, and RC Simulator.
Find help
3 reviewed answers.
Where should I start in a module?
Open the module page from the sidebar to see its chapters in order. Use the module map to return to a topic you need.
Modules and pathsReviewed
What can I try when a project does not work?
Check the symptom, then work through the diagnostic steps. Keep the error message and the smallest example that reproduces it.
When you are stuckReviewed
How do interactive examples work?
Use the interactive guide to check how examples and simulations work before changing your project.
Interactive examples guideReviewed
