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

ADC is useful for post-optocoupler levels, uncertain GPIO levels, 24/12/5/3.3 V supply monitoring, shunt current, input degradation, RC filters and HIL analog levels. GPIO/comparator remains the fast path; ADC provides slow diagnostics.

3. Theory

SAR ADC:

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

Influences include Vref, source impedance, sampling time, noise, layout, RC, temperature, calibration and clock. 12 bits gives codes 0..4095. At 3.3 V, an LSB is about 0.805 mV, but that does not imply 0.805 mV accuracy. Accuracy, noise, linearity and offset/gain errors still matter. A high-impedance divider and short sampling time introduce error because the sampling capacitor cannot settle. Increase sampling time, reduce resistance, add a buffer op-amp or add a capacitor for slow signals. 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.