ES: ADC and DAC

Analog-to-Digital Converters (ADC) and Digital-to-Analog Converters (DAC) are the bridge between the physical world and the digital MCU world.

Why We Need ADC and DAC

Microcontrollers operate digitally:

md
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.

md
  Physical World
  (Analog Signal)
		 │
		 ▼
	+----------+
	|   ADC    |
	+----------+
		 │
		 ▼
	Digital MCU

And the opposite:

md
	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
  • Example

    Suppose:

  • Vref = 3.3V
  • Resolution = 12-bit

That means:

2^12 = 4096 levels

So each step = 3.3V / 4096 ≈ 0.8mV

If input = 1.65V: 1.65 / 3.3 * 4095 ≈ 2047

md
         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:

  1. Samples
  2. Holds voltage constant
  3. Converts

SAR ADC (Successive Approximation)

Most MCUs use SAR ADC.

How it works (12-bit example):

md
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:

md
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

md
Analog Signal
     /\
    /  \         Digital Steps
   /    \        ____
  /      \      |    |
_/        \_____|____|___

This step behavior = quantization.


Types of ADC

  1. 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

    md
           Analog Input
                 |
                 v
           +-------------+
           | Comparators |
           +-------------+
            |  |  |  |  |
            |  |  |  |  |
           Ref Ref Ref Ref
           Voltages |
                   v
            +-------------+
            | Priority    |
            | Encoder     |
            +-------------+
                 |
                 v
           Digital Output
    

  1. 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.

    md
           Analog Input
                 |
                 v
           +-------------+
           | Sample &    |
           | Hold Circuit|
           +-------------+
                 |
                 v
           +-------------+
           | Comparator  |
           +-------------+
                 |
                 v
           +-------------+
           | SAR Logic   |
           | (Binary     |
           | Search)     |
           +-------------+
                 |
                 v
           Digital Output
    

  1. 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.

    md
           Analog Input
                 |
                 v
           +-------------+
           | Integrator  |
           +-------------+
                 |
                 v
           +-------------+
           | Comparator  |
           +-------------+
                 |
                 v
           +-------------+
           | Digital     |
           | Filter      |
           +-------------+
                 |
                 v
           +-------------+
           | Decimator   |
           +-------------+
                 |
                 v
           Digital Output

  1. 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.

    md
           Analog Input
                 |
                 v
           +-------------+
           | Integrator  |
           +-------------+
                 |
                 v
           +-------------+
           | Comparator  |
           +-------------+
                 |
                 v
           +-------------+
           | Timer /     |
           | Counter     |
           +-------------+
                 |
                 v
           Digital Output
    

  1. 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.

    md
           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

md
         +------+
ADC ---->| DMA  |----> Memory Buffer
         +------+

Critical for:

  • Audio
  • High-speed sampling
  • Low CPU overhead

Interrupt Mode

ADC conversion done → interrupt → ISR

Example ISR:

cpp
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:

  1. R-2R Ladder
  2. Current Steering
  3. Sigma-Delta (audio)

R-2R Ladder

md
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:

  1. Enable ADC clock
  2. Configure GPIO as analog
  3. Select channel
  4. Set sampling time
  5. Enable ADC
  6. Start conversion

Example:

cpp
ADC1->CR |= ADC_CR_ADEN;
ADC1->CR |= ADC_CR_ADSTART;

while(!(ADC1->ISR & ADC_ISR_EOC));

uint16_t value = ADC1->DR;


Performance Trade-offs

ParameterHigher Value MeansTradeoff
ResolutionBetter precisionSlower
Sampling rateFaster signal trackingMore noise
VrefLarger rangeLower resolution per volt
Sampling timeBetter accuracyLower speed

Full System View

md
        +-----------------------+
        |        MCU            |
        |                       |
Analog →|  ADC        CPU       |→ DAC → Analog
Input   |   │           │       |
        |   │           │       |
        |  DMA        ISR       |
        +-----------------------+