Sensors and I/O

ADC analog sample

Battery voltage and analog sensors. Attenuation and calibration matter.

Sensors and I/O Analog / battery ESP32 family

Official path: examples/peripherals/adc

01 Overview

The ESP32 built-in ADC reads analog voltages, commonly used for battery monitoring and analog sensors like LDRs or potentiometers.

The official driver offers oneshot and continuous modes; the API changes across IDF versions, so check the matching docs.

Use for: resistor-divided battery voltage, general analog inputs where moderate accuracy is fine.

Avoid for: metrology-grade measurements (e.g., energy meters) – use an external ADC like ADS1115.

Input range depends on attenuation; default is 0-1.1V, so choose attenuation based on your signal amplitude.

Calibration compensates for manufacturing variance, but not all chips support it; detect at runtime.

02 Hardware

  • GPIO voltage limit: Use a resistor divider to keep input below the GPIO maximum (about 3.3V) to avoid damage.
  • Divider resistors: Battery voltage must be divided into the ADC range; resistor tolerance affects accuracy.
  • Ground layout: Separate analog and digital grounds to prevent digital noise coupling, which shows up as jitter.
  • Strapping pins: Avoid using strapping pins (e.g., GPIO0, GPIO12) as the only sample input because boot strapping can interfere.
  • Filter capacitor: A small cap (e.g., 100nF) on the ADC pin filters high-frequency noise but increases settling time.
  • Reference voltage: The internal reference drifts with temperature; calibration partially compensates.

03 Software flow

1. Select ADC channel and attenuation (e.g., ADC_ATTEN_DB_11 for 0-3.3V).

2. If supported, call the calibration API (e.g., esp_adc_cal) to get calibration coefficients.

3. Configure oneshot or continuous mode; continuous uses DMA for high-rate sampling.

4. Take multiple samples and average to reduce noise.

5. Convert raw values to voltage (accounting for divider ratio) and apply calibration curve.

6. Filter the voltage (e.g., moving average) for stable battery percentage display.

7. Low-battery detection: trigger shutdown or alarm when voltage drops below a threshold.

04 Core points

  • The on-chip ADC is not a meter: accuracy is limited; don't use for high-precision measurements.
  • Attenuation matters: different attenuation gives different ranges; choose to avoid clipping or poor resolution.
  • Calibration is essential: without it, error can be large; enable in production.
  • Sampling time: balance sample rate and filtering in continuous mode.
  • Custom work: divider values, calibration curve, and low-battery shutdown thresholds must be tuned per product.

中文

传感器与外设

ADC 模拟采集

电池电压、模拟传感器。注意衰减和校准。

传感器与外设 模拟量 / 电池 ESP32 family

官方路径: examples/peripherals/adc

01 项目概述

ESP32 内置 ADC 用于读取模拟电压,常见场景是电池电压监测和模拟传感器(如光敏、电位器)。

官方驱动分 oneshot 和 continuous 两种模式,API 随 IDF 版本变化,需查阅对应版本文档。

适用:分压测电池、模拟量读取,对精度要求不高的场合。

不适用:高精度计量(如电能表),应使用外部 ADC(如 ADS1115)。

ADC 输入范围受衰减设置影响,默认 0-1.1V,需根据信号幅度选择衰减。

校准功能可补偿出厂偏差,但并非所有芯片都支持,需在运行时检测。

02 项目硬件描述

  • GPIO 电压限制: 分压电阻确保输入电压不超过 GPIO 最大电压(约 3.3V),否则可能损坏芯片。
  • 分压电阻: 电池电压需通过分压电阻降到 ADC 量程内,注意电阻精度影响测量结果。
  • 模拟地布局: 模拟地和数字地分开,避免数字噪声耦合到 ADC 输入,否则采样值会跳动。
  • Strapping 脚: 避免使用 strapping 引脚(如 GPIO0、GPIO12)作为唯一采样口,因为上电时序可能影响采样。
  • 滤波电容: 在 ADC 输入引脚加小电容(如 100nF)可滤除高频噪声,但会增加采样稳定时间。
  • 参考电压: 内部参考电压可能随温度漂移,校准可部分补偿。

03 项目软件流程描述

1. 选择 ADC 通道和衰减(如 ADC_ATTEN_DB_11 对应 0-3.3V)。

2. 若芯片支持,调用校准 API(如 esp_adc_cal)获取校准系数。

3. 配置 oneshot 或 continuous 模式,continuous 使用 DMA 适合连续采样。

4. 多次采样取平均,减少噪声影响。

5. 将原始值转换为电压(考虑分压比),并应用校准曲线。

6. 对电压进行软件滤波(如移动平均),用于电池电量显示等。

7. 低电检测:当电压低于阈值时,触发关机或报警。

04 项目核心点

  • ESP32 内置 ADC 不是电表: 精度有限,不适合高精度测量。
  • 衰减选择: 不同衰减对应不同量程,需根据信号幅度选择,避免削波或分辨率不足。
  • 校准必要性: 未校准时误差可能较大,产品中应启用校准。
  • 采样时间: 连续采样时注意采样率与滤波的平衡。
  • 可定制: 分压电阻值、校准曲线、低电关机阈值需根据具体产品调整。