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.

Quick reference
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.

A continuous analog signal sampled at regular intervals, then quantized into a staircase of digital values analog input digital output (quantized)
Each sample (dot) freezes the input at one instant; the bar below is that sample rounded to the converter's nearest representable step — the gap between "smooth line" and "staircase" is quantization error.

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

TypeHow it worksTypical 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.
FlashA 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.

A PWM square wave passed through a low-pass filter to produce a smooth analog voltage at the average level PWM, ~62% duty cycle avg Low-pass filter (RC) smooth analog out
A higher duty cycle raises the average (and the filtered output); a lower duty cycle lowers it — the filter's job is just to smooth away the switching, not to change the average.

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

Gotchas

Field notes