Why We Need ADC and DAC
Microcontrollers operate digitally:
Digital domain → 0 or 1
Voltage levels → 0V or 3.3V (for example)
But the real world is analog:
- Temperature sensor → 0.52V
- Microphone → varying waveform
- Battery voltage → 2.8V
- Motor control signal → analog waveform
So we need converters.
Physical World
(Analog Signal)
│
▼
+----------+
| ADC |
+----------+
│
▼
Digital MCU
And the opposite:
Digital MCU
│
▼
+----------+
| DAC |
+----------+
│
▼
Analog Output (Voltage/Current)
Analog-to-Digital Converter (ADC)
An ADC converts a continuous voltage into a digital number.
ADC is acronym for Analog to Digital Converter, which is used to convert analog signals into digital signals, allowing analog data (like voltage) to be processed in digital systems. ADCs are widely used in electronics, especially in microcontrollers and digital signal processing systems, to interpret real-world analog signals, such as temperature, sound, and light, into a format that digital systems can handle.
To convert Analog signal to Digital signals you have to make 3 steps:
- Sample and Hold
- Quantization
- Encoding
- Vref = 3.3V
- Resolution = 12-bit
Example
Suppose:
That means:
2^12 = 4096 levels
So each step = 3.3V / 4096 ≈ 0.8mV
If input = 1.65V: 1.65 / 3.3 * 4095 ≈ 2047
Analog Input
│
▼
+----------------+
| Sample & Hold |
+----------------+
│
▼
+----------------+
| Comparator |
+----------------+
│
▼
+----------------+
| SAR Logic |
+----------------+
│
▼
Digital Register
Sample and Hold
The ADC first captures the analog voltage.
Why?
Because conversion takes time. If the input changes during conversion → wrong result.
it is making a sample every a specific time (Sampling rate: number of samples / second), and hold and read this signal (volt) with minimum frequency (Nyquist Criteria => Fs >= 2Fm)
So it:
- Samples
- Holds voltage constant
- Converts
SAR ADC (Successive Approximation)
Most MCUs use SAR ADC.
How it works (12-bit example):
Step 1: Try 2048 (1000 0000 0000)
Step 2: Check comparator
Step 3: Adjust next bit
Step 4: Repeat
Binary search in hardware.
Conversion time:
Tconv = sampling time + 12 clock cycles
Sample and Hold
it is making a sample every a specific time (Sampling rate: number of samples / second), and hold and read this signal (volt) with minimum frequency (Nyquist Criteria => Fs >= 2Fm)
Quantization
Each sampled value is rounded to the nearest digital level, which are a number of levels depend on the percision that we assign the sampled value to the nearest one, to prepare it to the next step which is the Encoding.
There is a tolerance produced due to assign the desecrate signal to nearest quatization level which can be increased by reducing the precision, or decreased by increasing the precision.
The quality of an ADC is determined by its resolution (number of bits), sampling rate, and accuracy.
Encoding
The quantized value is converted into a binary code (a series of digital bits) depend on the nearest quantization level.
ADC Key Parameters
Resolution
8-bit → 256 levels
10-bit → 1024
12-bit → 4096
16-bit → 65536
Higher resolution = finer measurement
But slower and noisier in low-cost MCUs
Sampling Rate
How many samples per second.
Example:
- 1 kHz → slow sensor
- 44.1 kHz → audio
- 1 MHz → fast signals
Nyquist rule:
Sampling frequency ≥ 2 × signal frequency
Reference Voltage (Vref)
Defines maximum measurable voltage.
Digital value = Vin / Vref × (2^n - 1)
If Vref unstable → readings unstable.
Security note:
In secure products, reference integrity is critical (fault injection attacks sometimes manipulate Vref).
Quantization Error
Because we approximate continuous signal: Error ≤ 1 LSB
Analog Signal
/\
/ \ Digital Steps
/ \ ____
/ \ | |
_/ \_____|____|___
This step behavior = quantization.
Types of ADC
- Flash ADC (Parallel ADC):
- Operation: Uses a bank of comparators, each with a different reference voltage, to simultaneously compare the input voltage to the references.
- Pros: Very fast, ideal for high-speed applications.
- Cons: High power consumption, complex circuitry, and expensive, especially for high resolutions.
- Use cases: Video processing, radar systems.
The flash ADC uses a bank of comparators to simultaneously compare the input voltage to multiple reference levels
Analog Input
|
v
+-------------+
| Comparators |
+-------------+
| | | | |
| | | | |
Ref Ref Ref Ref
Voltages |
v
+-------------+
| Priority |
| Encoder |
+-------------+
|
v
Digital Output
- Successive Approximation Register (SAR) ADC:
- Operation: Uses a binary search algorithm to approximate the input signal step-by-step, finding the digital equivalent one bit at a time.
- Pros: Good balance of speed, resolution, and power efficiency.
- Cons: Moderate speed compared to flash ADC.
- Use cases: General-purpose data acquisition, audio applications.
The SAR ADC uses a binary search algorithm to converge to the correct digital output one bit at a time.
Analog Input
|
v
+-------------+
| Sample & |
| Hold Circuit|
+-------------+
|
v
+-------------+
| Comparator |
+-------------+
|
v
+-------------+
| SAR Logic |
| (Binary |
| Search) |
+-------------+
|
v
Digital Output
- Sigma-Delta (ΔΣ) ADC:
- Operation: Oversamples the input signal with a high-frequency clock, then uses digital filtering and decimation to convert it to a lower sampling rate.
- Pros: High resolution and excellent noise performance, suitable for low-frequency signals.
- Cons: Slow compared to SAR and flash ADCs.
- Use cases: High-precision applications, like audio and instrumentation.
The sigma-delta ADC oversamples the input signal and uses digital filtering for high resolution.
Analog Input
|
v
+-------------+
| Integrator |
+-------------+
|
v
+-------------+
| Comparator |
+-------------+
|
v
+-------------+
| Digital |
| Filter |
+-------------+
|
v
+-------------+
| Decimator |
+-------------+
|
v
Digital Output
- Dual Slope (Integrating) ADC:
- Operation: Measures the time it takes for an integrator’s output to reach a reference level, providing a digital value proportional to the input.
- Pros: High noise immunity and accuracy.
- Cons: Very slow, making it unsuitable for high-speed applications.
- Use cases: Digital voltmeters, industrial control.
The dual-slope ADC charges a capacitor and then discharges it, measuring the time to get a digital value.
Analog Input
|
v
+-------------+
| Integrator |
+-------------+
|
v
+-------------+
| Comparator |
+-------------+
|
v
+-------------+
| Timer / |
| Counter |
+-------------+
|
v
Digital Output
- Pipeline ADC:
- Operation: Combines the principles of flash and SAR ADCs, dividing the conversion process into multiple stages. Each stage converts part of the signal before passing it to the next.
- Pros: Good speed-to-power ratio, moderate resolution.
- Cons: Complexity in design.
- Use cases: Intermediate-speed applications like digital video and communication systems.
The pipeline ADC has multiple stages, each converting part of the input signal, similar to a pipeline.
Analog Input
|
v
+-------------+
| Stage 1 |
| (Partial |
| Conversion) |
+-------------+
|
v
+-------------+
| Stage 2 |
| (Partial |
| Conversion) |
+-------------+
|
v
+-------------+
| Stage N |
| (Final |
| Conversion) |
+-------------+
|
v
Digital Output
ADC Modes in MCU
Single Conversion Mode
CPU starts conversion manually.
ADC_START = 1
wait until ADC_DONE
read ADC_DR
Continuous Mode
ADC keeps converting.
Scan Mode (Multi-channel)
Channel 0 → Channel 1 → Channel 2 → repeat
Used for:
- Battery
- Temperature
- Sensor arrays
DMA Mode
Instead of CPU reading:
ADC → DMA → RAM buffer
+------+
ADC ---->| DMA |----> Memory Buffer
+------+
Critical for:
- Audio
- High-speed sampling
- Low CPU overhead
Interrupt Mode
ADC conversion done → interrupt → ISR
Example ISR:
void ADC_IRQHandler(void)
{
if (ADC_SR & EOC)
{
uint16_t value = ADC_DR;
process(value);
}
}
ADC Noise and Signal Integrity
In real hardware:
- Switching noise from MCU
- Power supply ripple
- Ground bounce
- EMI from motors
You must:
- Separate analog and digital ground
- Use decoupling capacitors
- Use proper filtering
Digital-to-Analog Converter (DAC)
DAC converts digital number into analog voltage.
Example:
12-bit DAC
Value written = 2048
Vout = 2048 / 4095 × Vref
If Vref = 3.3V: ≈ 1.65V
DAC Internal Architecture
Common types:
- R-2R Ladder
- Current Steering
- Sigma-Delta (audio)
R-2R Ladder
Bit3 ──R──┐
├─R──┐
Bit2 ──R──┘ ├─R──┐
Bit1 ──R────────┘ ├─R── Vout
Bit0 ──R───────────────┘
Each bit contributes weighted voltage.
Simple and common in MCUs.
DAC Applications
- Audio generation
- Motor control waveform
- Bias voltage control
- Analog test signals
- Control loops (PID output)
Security Perspective (Very Important)
As a hardware security engineer, you should know:
- ADC can leak side-channel information (power analysis)
- DAC can be used for fault injection
- Analog glitching can manipulate ADC results
- Timing of ADC conversion can leak secrets
Example:
Attacker injects voltage glitch: Vref ↓ → measured value altered
This can bypass threshold checks.
In secure systems:
- Use monitoring circuits
- Validate readings multiple times
- Detect abnormal voltage transitions
Practical Example (STM32-like MCU)
Configuration steps:
- Enable ADC clock
- Configure GPIO as analog
- Select channel
- Set sampling time
- Enable ADC
- Start conversion
Example:
ADC1->CR |= ADC_CR_ADEN;
ADC1->CR |= ADC_CR_ADSTART;
while(!(ADC1->ISR & ADC_ISR_EOC));
uint16_t value = ADC1->DR;
Performance Trade-offs
| Parameter | Higher Value Means | Tradeoff |
|---|---|---|
| Resolution | Better precision | Slower |
| Sampling rate | Faster signal tracking | More noise |
| Vref | Larger range | Lower resolution per volt |
| Sampling time | Better accuracy | Lower speed |
Full System View
+-----------------------+
| MCU |
| |
Analog →| ADC CPU |→ DAC → Analog
Input | │ │ |
| │ │ |
| DMA ISR |
+-----------------------+