Embedded Systems
ADC, DAC, and Analog Interfaces
The human body runs on analog signals: heart rhythm at 1 Hz, neural spikes at 1 kHz, muscle EMG at 500 Hz. Medical devices must digitize these with enough precision to detect a 50 µV ST-segment elevation (heart attack indicator) in the presence of 1 mV of 50 Hz power line interference. An ambulatory ECG monitor does this on a coin cell battery, sampling at 500 Hz with 16-bit resolution, for 14 days. The difference between a life-saving diagnosis and a missed alert is ADC resolution, sampling rate, and signal conditioning quality. This is what analog interfaces mean in practice.
- **Automotive engine control**: crank and cam position sensors use ADC input capture to measure ignition timing to within 0.1 crankshaft degrees; fuel injector timing depends on 12-bit ADC readings of manifold pressure, coolant temperature, and oxygen sensor voltage
- **Industrial IoT vibration monitoring**: Emerson AMS 9420 wireless transmitter samples vibration at 25.6 kHz with 24-bit ADC to detect bearing faults by frequency analysis; detects changes in vibration spectrum 6-8 weeks before mechanical failure
- **Medical pulse oximetry (SpO2)**: reads two wavelengths of LED light through a fingertip with 16-bit ADC at 1 kHz, extracting 0.5% SpO2 changes; requires < 0.1% ADC linearity to distinguish critical saturation levels (94% vs 95%)
ADC Resolution and Quantization
A temperature sensor outputs 0-3.3V proportional to -40°C to 125°C. The microcontroller needs to read this as a number. The **Analog-to-Digital Converter** (ADC) maps a continuous voltage range to discrete integer values. Resolution is the number of bits: a 12-bit ADC produces integers from 0 to 4095. The **LSB** (Least Significant Bit) voltage is V_ref / 2^n. For a 12-bit ADC with 3.3V reference: LSB = 3.3 / 4096 = 0.806 mV. This is the minimum voltage change the ADC can detect.
**Quantization error** is inherent to all ADCs: the true analog value is rounded to the nearest integer multiple of LSB. Worst-case error is ±0.5 LSB. For a temperature sensor mapping 165°C range to 4096 steps: LSB = 165°C / 4096 = 40 millidegrees. Temperature measurement error from quantization alone: ±20 millidegrees - negligible for most applications. A 10-bit ADC: ±80 millidegrees - still acceptable. An 8-bit ADC: ±320 millidegrees - may matter for precision applications.
**Oversampling** improves effective resolution: averaging 4 samples gives 1 extra bit, 16 samples gives 2 extra bits, 256 samples gives 4 extra bits. A 12-bit ADC with 256-sample averaging achieves approximately 16-bit effective resolution. STM32 HAL includes oversampling hardware that performs this automatically without CPU overhead.
A 12-bit ADC with 3.3V reference reads a value of 2048. What voltage does this represent?
Sampling Rate and the Nyquist Theorem
Resolution determines accuracy; **sampling rate** determines what frequencies can be captured. The Nyquist-Shannon theorem states: to accurately reconstruct a signal with maximum frequency f_max, the sampling rate must be at least **2 × f_max**. Sample too slowly, and high-frequency content folds back into the measurement as false low-frequency components - **aliasing**. A 50 Hz power line interference sampled at 60 Hz appears as a false 10 Hz signal in the data.
**Nyquist theorem in practice:** - Audio (telephone quality): f_max = 4 kHz → sample at 8 kHz (ITU-T G.711) - Audio (CD quality): f_max = 20 kHz → sample at 44.1 kHz - ECG (medical): f_max = 150 Hz → sample at 500+ Hz - Vibration analysis (industrial): f_max = 10 kHz → sample at 25.6 kHz - Power quality monitoring: f_max = 2.5 kHz (50th harmonic) → sample at 6.4 kHz **Anti-aliasing filter** (low-pass analog filter before the ADC) must attenuate frequencies above f_sample/2 before sampling. Without it, aliasing is inevitable regardless of ADC resolution.
**Always use DMA for continuous ADC sampling**. CPU-polled ADC has jitter: interrupt latency varies by ±1-2 µs depending on what else the CPU is doing. At 25.6 kHz, ±2 µs jitter = ±5% timing error per sample. DMA transfers happen in hardware with deterministic timing. STM32 ADC + DMA + Timer trigger achieves < 10 ns sample timing jitter.
An ECG monitor samples at 250 Hz but the patient has a pacemaker artifact at 150 Hz. The digitized ECG shows a 100 Hz oscillation that was not present in the analog signal. What is happening?
Digital-to-Analog Conversion
The **DAC** is the inverse of the ADC: it converts a digital integer to an output voltage. Applications include audio output, waveform generation for signal generators, reference voltages for sensor excitation, and analog control signals to actuators. STM32 includes 12-bit DAC(s) capable of producing waveforms up to ~1 MHz. For higher-frequency output, an external DAC with SPI interface is used (e.g., MCP4922 at 2.4 Msps, or AD5791 at 1 Msps with 20-bit resolution).
**DAC key specifications:** - **Resolution**: bits (12-bit = 4096 levels, 16-bit = 65536 levels) - **Sample rate / update rate**: maximum samples per second - **Settling time**: time for output to reach final value after update (20 ns - 10 µs) - **Output impedance**: low impedance needed to drive loads without voltage sag - **Glitch energy**: unwanted output spikes during code transitions (minimized by deglitch circuits) - **INL/DNL**: Integral/Differential Non-Linearity - actual vs ideal transfer curve
A 12-bit DAC with 3.3V reference outputs voltage for code 1024. What is the output voltage?
Signal Conditioning
Real-world sensors rarely output 0-3.3V perfectly matched to the ADC input range. A thermocouple outputs 0-80 mV. A pressure sensor outputs 4-20 mA current loop. A piezoelectric vibration sensor outputs ±10V at high impedance. **Signal conditioning** adapts these diverse signals to the ADC's requirements through amplification, level shifting, filtering, and impedance transformation. Proper conditioning is as important as ADC selection.
| Sensor Type | Output | Conditioning Needed | Common Circuit |
|---|---|---|---|
| Thermocouple | 0-80 mV, ±µV accuracy | 50-100x amplification, cold junction comp. | MAX31855, INA128 |
| Current loop (4-20mA) | 4-20 mA current | Shunt resistor (249Ω → 1-5V) | R + RC filter |
| Strain gauge | 0.1-10 mV bridge differential | Instrumentation amplifier, 100-1000x gain | INA326, AD8220 |
| MEMS microphone | Differential, 1V p-p | Differential-to-single-ended conversion | Low-noise op-amp |
| Piezo vibration | ±10V, high impedance | Voltage divider + clamp to ADC range | Resistor divider + Zener |
**Input protection** is critical: ADC inputs must not exceed V_ref (typically 3.3V) or go below 0V. Even 100 mV overvoltage can permanently damage the ADC input stage. Protection: Schottky diode clamps to VCC and GND, with a series resistor (1-10 kΩ) to limit current. Industrial sensors often have transients of ±50V from motor switching - protection networks are non-negotiable in industrial designs.
Higher ADC resolution always improves measurement accuracy
ADC resolution improves only the quantization floor; noise, offset errors, gain errors, and signal conditioning quality determine actual accuracy; a 12-bit ADC with good signal conditioning often outperforms a 24-bit ADC with poor conditioning
Real ADCs have multiple error sources: quantization noise (resolution-dependent), thermal noise (analog circuit property), integral non-linearity (INL, up to ±2 LSB even in 12-bit ADCs), offset and gain errors (up to ±1% without calibration). A 12-bit ADC with 0.1% total error gives 10-bit effective resolution (ENOB). Adding bits to the converter does not help if the analog front-end has 1% noise. Industrial measurements always start with signal conditioning quality, then choose resolution.
An exponential moving average filter with alpha=0.1 is used on a noisy ADC reading. The current filtered value is 100. A new noisy sample of 200 arrives. What is the new filtered value?
Key ideas
- **ADC resolution** determines quantization granularity: LSB = V_ref / 2^n; oversampling (averaging 4N samples) adds 1 effective bit; 12-bit STM32 ADC with 3.3V ref: 0.8 mV/LSB
- **Nyquist theorem**: sample rate must exceed 2 × f_max; frequencies above f_sample/2 alias into false lower-frequency components; always apply an analog anti-aliasing filter before the ADC
- **DMA + Timer trigger**: the correct architecture for continuous ADC sampling; timer provides precise timing (< 10 ns jitter); DMA transfers to memory without CPU; CPU processes complete buffers via callbacks
- **Signal conditioning** adapts real-world sensor signals to ADC input range via amplification (INA128 for thermocouples), level shifting, anti-aliasing filtering, and input protection (Schottky clamps + series resistor)
Related topics
ADC and DAC connect to signal processing and the sensor ecosystem:
- Timers and Interrupts — Timer output compare triggers ADC conversions at precise intervals; DMA completion interrupts notify the CPU when buffers are ready
- Digital Signal Processing (FIR Filters) — ADC output is the input to digital filters; sampling rate and anti-aliasing determine what the DSP algorithm receives
- Communication Protocols (SPI, I2C) — External high-resolution ADCs (ADS1256, AD7779) and DACs communicate via SPI; understanding the protocol is needed for sensor integration
Вопросы для размышления
- An audio recorder samples at 44.1 kHz. A guitar string produces harmonics up to 8 kHz. The anti-aliasing filter cutoff is 20 kHz. What is the minimum filter order needed to sufficiently attenuate a 25 kHz overtone before it aliases to 19.1 kHz (44.1 - 25)?
- A pressure sensor has a 4-20 mA current loop output. The engineer uses a 100Ω shunt resistor instead of 249Ω to create 0.4-2V output, saving headroom. What problem does this create for the ADC dynamic range - and what is the minimum usable resolution now?
- An industrial temperature controller needs ±0.1°C accuracy over a -40 to 125°C range (165°C span). What is the minimum ADC resolution required just for quantization to meet this spec - and what other error sources would make this harder to achieve in practice?