1. Topic

ADC sampling time, source impedance, calibration, oversampling, DMA scans, min/max/avg and diagnostics. Main idea: the ADC measures the result of a sampling circuit, not an ideal voltage.

2. Why this matters in a project

An ADC is useful for post-optocoupler levels, supply diagnostics and shunt-current measurements. Do not apply 24/12/5 V supplies directly to its input: design scaling, protection and input-range checks. In this architecture, GPIO/comparator handles the fast path and ADC provides separate diagnostics.

3. Theory

SAR ADC:

text
input -> sample capacitor -> comparison with Vref -> digital code

Results depend on Vref, source impedance, sampling time, noise, layout, RC networks, temperature, calibration and clocking. An ideal 12-bit ADC has 4096 codes; for a hypothetical 3.3 V range, 3.3/4096 ≈ 0.806 mV per step. This is not total accuracy and does not claim that the ESP32 Vref is 3.3 V. A high-impedance divider with short acquisition can prevent the sampling capacitor from settling. Check settling time, resistance, buffer amplifiers and RC networks against the ADC documentation. ESP32:

text
oneshot - CLI/diagnostics/slow measurements
continuous - stream of samples
ADC2 + Wi-Fi - caution, prefer ADC1 for critical channels

STM32:

text
Polling - individual debug measurements
Interrupt - occasional events / analog watchdog
DMA - multichannel scan, min/max/avg

Oversampling reduces random noise but does not correct Vref, offset, gain error or inadequate sampling time. Averaging post-optocoupler AC can hide pulsation, so retain min/max/avg/last:

c
typedef struct {
    uint16_t last_raw;
    uint16_t min_raw;
    uint16_t max_raw;
    uint32_t avg_raw;
    uint32_t sample_count;
    uint32_t error_count;
    int64_t last_sample_us;
} adc_channel_stats_t;

4. Common mistakes

  1. Trusting one ADC sample.
  2. Combining a high-impedance divider with a short sampling time.
  3. Using ESP32 ADC2 with Wi-Fi for a critical measurement.
  4. Using oversampling instead of calibration.
  5. Delaying phase_event for an ADC scan.
  6. Confusing channel order in a DMA scan.
  7. Leaving the ADC pin unprotected from external signals.

5. Practical task

Create ADC_DIAGNOSTICS_POLICY.md:

markdown
# ADC diagnostics policy
Role:
ADC is used for slow diagnostics, not for fast phase timestamp.
Fast path:
opto/comparator/Schmitt -> GPIO -> phase_detector
Slow path:
analog node -> ADC -> min/max/avg -> CLI/telemetry
Rules:
1. No phase event waits for ADC.
2. ADC channels must have documented scaling.
3. Every ADC channel has min/max/avg/last stats.
4. Calibrate when voltage matters.
5. Source impedance and sampling time are checked together.
6. ESP32 critical channels prefer ADC1 with Wi-Fi.
7. STM32 ADC scan order must match channel table.

6. Further reading and experiments

ESP-IDF ADC oneshot/continuous/calibration, STM32 HAL ADC and ST AN2834 on ADC errors.

A 12-bit ADC with a nominal 3.3 V range has an LSB around 0.805 mV. What does that mean?

Exercise

More averaging reduces noise, but a high-impedance divider still reads systematically low. What should you check, and why will still more averaging not remove the cause?

Self-check criteria: Distinguish random noise from systematic error; identify sampling time/source impedance and calibration.

Show the supplied answer

Check source impedance and sampling time: the sampling capacitor may not settle. Then check scaling, Vref/calibration and circuit errors. Averaging reduces some random noise but does not remove systematic settling or calibration errors.