Chapters

32 DAC and PWM: Output Foundations

electronics-controller-design
analog
digital
dac
pwm
actuators

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.
  1. Voltage Vera compares a dim lamp, a reference-voltage dial, and a small actuator while two unlabeled output paths remain undecided.

    Which output method fits each physical job?

CP-0135 pre-concept hook: 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.

In 60 Seconds

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.

The mathematical gist. An 8-bit endpoint DAC on 5 V has step q=5/255=19.6q=5/255=19.6 mV and code 153 gives 3.00 V. PWM instead gives Vavg=DVhighV_{avg}=DV_{high}. At 75% duty that is 3.75 V; a 10 kohm, 0.22 uF filter has 2.20 ms time constant, 72.3 Hz cutoff, and about 0.162 V first-harmonic peak at 1 kHz.

Math Bridge · guided foundationsWhen does a switching waveform behave like an analogue voltage?Let Eddie connect DAC codes, PWM duty, RC smoothing, ripple, and settling.
Chapter Roadmap
  • 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:

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).

DAC conversion contract showing a bounded N-bit code, reference and transfer function, expected output, measured output, and acceptance checks for range, monotonicity, saturation, settling, and load effects.
Figure 32.1: DAC conversion contract from bounded digital code through reference and transfer function to measured analog output

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

Vout=Vref×Digital Input2n1V_{out} = V_{ref} \times \frac{Digital\ Input}{2^n - 1}

Example (8-bit DAC, Vref = 5V):

Digital InputCalculationVoltage Output
05 x (0/255)0.00 V
645 x (64/255)1.25 V
1285 x (128/255)2.51 V
1925 x (192/255)3.76 V
2555 x (255/255)5.00 V

For an 8-bit DAC with Vref=5V_{ref} = 5 V, the step size (resolution) is Vstep=Vref/(2n1)=5/255=19.6V_{step} = V_{ref} / (2^n - 1) = 5 / 255 = 19.6 mV. To output 3.0V, you need digital value D=Vout×(2n1)/Vref=3.0×255/5=153D = \lfloor V_{out} \times (2^n - 1) / V_{ref} \rfloor = \lfloor 3.0 \times 255 / 5 \rfloor = 153. Actual output is Vactual=153×5/255=3.0V_{actual} = 153 \times 5 / 255 = 3.0 V exactly. With a 12-bit DAC (2121=40952^{12} - 1 = 4095), step size shrinks to 5/4095=1.225 / 4095 = 1.22 mV — 16× finer than 8-bit, enabling smooth audio waveforms where 8-bit would sound grainy.

32.10.2 DAC Applications in IoT

ApplicationExampleDAC Type
Audio OutputMusic playback12-16 bit, 44.1 kHz
Motor Speed ControlPWM alternative8-10 bit
LED BrightnessSmooth dimming8-12 bit
Analog Sensor SimulationTesting12 bit
Waveform GenerationSignal generator12-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.

A 4-bit digital-to-analog converter maps input 0000–1111 into 16 levels of analogue Vout using 5V Vref. Step size is 5V/16.
Figure 32.2: Digital-to-Analog Converter circuit diagram

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 2N2^N 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:

  1. Increase bit depth (8-bit → 12-bit → 16-bit)
  2. Increase update rate (faster DAC updates)
  3. Add filtering (low-pass filter smooths steps)

Voltage VeraCheckpoint: 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.

DAC vs PWM Output Tradeoff
  1. Voltage Vera inspects a real load beside ripple and delay limit cards.

    Start with how much ripple and delay the real load can take.

  2. Vera compares a smooth converter trace against a switching trace passed through a filter, without declaring a universal winner.

    Compare a true smooth output with a filtered switch signal.

  3. Vera tests one chosen output on that load and keeps it only after the measured trace fits its limits.

    Test the chosen output on the real load before keeping it.

Choose a true smooth voltage output or a filtered switching output from the load's ripple, channel, cost, and response needs.

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.