Analog I/O (ADC/DAC)
How analog-to-digital and digital-to-analog converters bridge the continuous physical world and discrete firmware, and the sampling and quantization limits that govern accuracy.
UART, SPI, and I2C move discrete digital bytes. But the physical world — temperature, light, sound, voltage from a sensor — is continuous. Analog-to-digital converters (ADCs) and digital-to-analog converters (DACs) are the bridge between the two.
How an ADC works
An ADC samples a continuous voltage at discrete points in time, then quantizes each sample into one of a fixed number of digital levels determined by its resolution in bits.
voltage resolution = V_ref / 2^nWorked example: a 12-bit ADC with a 3.3 V reference has:
voltage resolution = 3.3 / 4096 ≈ 0.8 mV per stepAny real-world voltage gets rounded to the nearest 0.8 mV step — this rounding error is called quantization error, and it's an inherent limit of digitizing an analog signal, not a flaw in any particular ADC.
Most microcontroller ADCs use a successive approximation (SAR) architecture: it compares the input against a reference voltage one bit at a time (like a binary search), converging on the closest digital value in a fixed number of clock cycles.
Sampling rate matters too
Resolution determines how precisely each sample is measured; sampling rate determines how often you measure. To reconstruct a signal without ambiguity, you must sample at more than twice its highest frequency component (the Nyquist rate) — sampling slower than that aliases higher frequencies down into your measurement as false low-frequency noise.
DACs: the reverse process
A DAC takes a digital value and produces a corresponding analog voltage, typically by weighting a reference voltage across a resistor network or switched capacitor array. The main practical spec besides resolution is settling time — how long the output takes to stabilize after the input code changes, which matters when generating fast-changing waveforms.
Practical considerations
- Reference voltage stability sets the ceiling on accuracy — a noisy or drifting
V_refcorrupts every conversion regardless of the ADC's nominal resolution. - Input impedance of the ADC's sampling capacitor interacts with the source impedance of whatever you're measuring; a high-impedance sensor may need a buffer amplifier to drive the ADC accurately.
- Oversampling — taking many more samples than strictly needed and averaging them — trades sample rate for effective resolution, a common way to squeeze extra precision out of a cheap ADC.
Why this matters in practice
Nearly every sensor interface in embedded systems — a thermistor, a microphone, a current-sense amplifier — ultimately comes down to correctly configuring and reading an ADC channel, and understanding why the reading has the noise and resolution limits it does.
With digital I/O, serial communication, and analog conversion covered, the last building block is timing: the next sub-lesson, Timers & Interrupts, covers how firmware reacts to events precisely and on schedule.