Monitoring a supply rail with the ADC
The ESP32’s ADC reads from about 0.15 V to 3.1 V at its default setting, and is only straight up to about 2.45 V. So a 12 V battery must be scaled down well below 3.3 V: a 100 kΩ / 22 kΩ divider puts 12.6 V at 2.27 V, and a full 14.6 V still only at 2.63 V. The sketch reads calibrated millivolts and multiplies back up. Large resistors barely drain the battery.

Open this lesson in the labFree, in your browser, no account needed.
How it works
Vpin = Vbatt × R2/(R1+R2) = 12.6 × 0.1803 = 2.272 V.
Raw analogRead() ≈ 3127 on this board (a straight line would say 2820): don't scale raw counts by 3.3/4095. analogReadMilliVolts() uses the chip's calibration and is good to about ±60 mV up to 2.45 V.
Highest readable battery voltage: about 3.1 V ÷ 0.180 = 17.2 V. Divider drain: 103.3 µA.
Try it on the bench
- Turn the battery supply down from 12.6 V to 11 V: the sketch tracks it.
- Push it above 17 V: the pin passes 3.1 V and the reading sticks at the top, however high the battery goes.
- Change R2 back to 33 kΩ (and the sketch to match): 12.6 V now lands at 3.13 V, past the top of the ADC.
Parts on the bench
- 2 × Bench power supply
- ESP32 DevKit
- USB charger / port
- 2 × Resistor
- Digital multimeter
In the course
Zen’s Electronics Design Mastery, module “Microcontrollers & Embedded”.