Electronics
Data Converters — ADC and DAC
The physical world — temperature, sound, light, position — is continuous. A microcontroller only understands discrete numbers. Every sensor reading and every generated waveform has to cross that boundary somewhere, and that crossing is exactly what an ADC and DAC do.
- What it is
- ADC turns a continuous voltage into a digital number; DAC does the reverse.
- Mental model
- A ruler with only so many tick marks — the ADC rounds the real, continuous world to the nearest tick it has.
- Key numbers
- digital = (Vin / Vref) × (2bits − 1); 10-bit = 1024 levels, 12-bit = 4096.
- Reach for it when
- Any analog sensor input, or generating a smooth analog output/audio/control voltage from digital logic.
ADC: sampling and quantization
An Analog-to-Digital Converter does two things at every conversion: samples the input voltage at one instant, then quantizes it — rounds it to the nearest digital step the converter's resolution can represent.
Resolution, reference voltage, and sample rate
- Resolution
- How many bits the output has — 10-bit gives 1024 possible values, 12-bit gives 4096. More bits means finer steps, not a wider input range.
- Reference voltage (Vref)
- The voltage that maps to the maximum digital output. An input at Vref reads full-scale; the reference's own accuracy directly limits the whole conversion's accuracy.
- Sample rate
- How many conversions happen per second — has to be fast enough to capture how quickly the signal actually changes (see Nyquist, below).
digital_value = (V_in / V_ref) × (2^bits − 1)
The Nyquist rule
To faithfully represent a signal, you have to sample at at least twice the highest frequency component present in it — the Nyquist rate. Sample slower than that and higher frequencies don't just get lost, they get misrepresented as false lower frequencies that weren't actually there — aliasing. This is why audio ADCs sample at 44.1 kHz+ (to cover the ~20 kHz of human hearing) and why a fast-changing sensor signal needs a correspondingly fast ADC, not just a high-resolution one.
SAR, sigma-delta, and flash ADCs
| Type | How it works | Typical fit |
|---|---|---|
| SAR (Successive Approximation) | Binary search — compares the input against a reference divided in half repeatedly, resolving one bit per comparison. | The default on-chip ADC in most microcontrollers — good balance of speed, resolution, and cost. |
| Sigma-Delta (ΔΣ) | Oversamples at a much higher rate and averages, trading speed for very high effective resolution. | Precision measurement — audio, weighing scales, thermocouple front-ends. |
| Flash | A bank of comparators evaluates every possible level simultaneously — one clock cycle per conversion. | Very high-speed digitizing (RF, oscilloscopes) — expensive and power-hungry at high resolution. |
A SAR ADC's binary-search process is worth holding onto as a mental model: to resolve an 8-bit value it makes exactly 8 comparisons, each one deciding a single bit — first "is it in the top or bottom half," then repeating on whichever half it's in, narrowing down bit by bit until every bit is set.
DAC
A Digital-to-Analog Converter does the reverse: given a digital number, it produces a proportional analog voltage — reading the resolution formula above backward. DAC outputs typically need a smoothing (reconstruction) filter afterward, since the raw output changes in discrete steps rather than a truly continuous curve.
PWM as a poor man's DAC
Many microcontrollers have PWM outputs but no real DAC — and PWM plus a simple low-pass filter approximates one surprisingly well. A PWM signal switches fully on and off at a fixed frequency; its average voltage over time is proportional to the duty cycle. Filter out the fast switching and what's left is that average, as a genuinely smooth analog voltage.
The tradeoff: a heavier filter gives a smoother output but reacts more slowly to changes — the same RC time-constant tradeoff from Passive Components applies directly here.
Real-world applications
- Analog sensor reading — a thermistor voltage divider, a potentiometer, a microphone — anything continuous gets read through an ADC channel.
- Audio playback — a DAC (often a dedicated audio codec chip) turns a stream of digital samples back into the analog signal that drives a speaker.
- Motor current sensing — an ADC reading the voltage across a small shunt resistor is the standard way firmware measures real-time motor current.
- LED dimming and motor speed control — PWM (the poor man's DAC above) is the near-universal technique for both, often without needing a filter at all since the load itself (an LED, a motor's inertia) does the averaging.
- Battery voltage monitoring — a simple resistor-divider into an ADC channel is how most battery-powered devices track remaining charge.
Gotchas
- A noisy or drifting Vref corrupts every single reading proportionally — the ADC has no way to know its own reference moved, so "the sensor is drifting" is sometimes actually "the reference is drifting."
- Switching a multiplexed ADC to a new input channel needs settling time before the first reading is trustworthy — a common bug is reading immediately after switching channels and getting a value contaminated by the previous channel.
- Sampling below the Nyquist rate doesn't just lose detail — it actively creates false, lower-frequency signal that wasn't there, which is often harder to diagnose than an obviously noisy reading.
- On many MCUs, ADC-capable pins are shared with digital GPIO functions — configuring a pin for one silently disables the other, a common source of "why doesn't this pin read analog values anymore."