Battery voltage via divider

Measures the voltage of a Li-ion / LiFePO4 / 12 V battery through a resistor divider and sleeps to save power.

Power & batteries Board: ESP32-C3 SuperMini Level: ●○○ ⏱ 1 h

⚠ Caution

Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.

The divider that drains the battery you are measuring

It looks trivial: two resistors and an ADC pin. The trap is the current through the divider. A 12 V pack across 100 kΩ + 22 kΩ pulls about 100 µA all day, which on a 3000 mAh cell is a few months of life before the ESP32 has even woken up once.

Doing it properly

Choose the ratio so that the highest voltage stays below about 3.1 V at the pin: for a 4S LiFePO4 pack charged to 14.6 V, 100 kΩ over 22 kΩ gives roughly 2.6 V. Add 100 nF at the pin to steady the reading. The ESP32 ADC is not linear near the ends, so calibrate against a multimeter at two voltages, or use the factory-calibrated millivolt function.

  • To save power, feed the divider through a small MOSFET that is switched on only for the measurement.
  • A non-isolated divider shares ground with the battery. If the ESP32 is powered from a different source, connect the grounds deliberately.
  • Never let the pin see more than 3.3 V, even for a moment. A zener or a series resistor protects against surprises.

Components

  • ESP32-C3 SuperMini
  • Resistor kit 1/4W (1k, 2k, 4.7k, 10k, 22k, 100k)
  • Electrolytic capacitors 100-470 uF + ceramic 100 nF
  • Dupont jumper wires (M-F, M-M)

Wiring

Module Module pin ESP32 pin Note
Battery + to R1 (100 kΩ) divider top R1 to the ADC node for a 4S LiFePO4 / 12 V lead-acid battery up to 15 V
ADC node analog GPIO3 (ADC1) R2 22 kΩ to GND and 100 nF to GND at this node
Battery - GND GND common ground with the ESP32

Code

#include <WiFi.h>

const char* WIFI_SSID = "YOUR_WIFI";
const char* WIFI_PASS = "YOUR_PASSWORD";

bool connectWiFi(uint32_t timeoutMs = 20000) {
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
  uint32_t t0 = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - t0 < timeoutMs) {
    delay(250);
    Serial.print(".");
  }
  Serial.println();
  if (WiFi.status() == WL_CONNECTED) {
    Serial.print("IP: ");
    Serial.println(WiFi.localIP());
    return true;
  }
  return false;
}
#include <WiFiClientSecure.h>
#include <HTTPClient.h>

const char* TG_TOKEN = "123456:ABC-YOUR-BOT-TOKEN";   // from @BotFather
const char* TG_CHAT  = "YOUR_CHAT_ID";

// Keep the text free of double quotes and line breaks (it is inserted into JSON).
bool telegramSend(const String& text) {
  if (WiFi.status() != WL_CONNECTED) return false;
  WiFiClientSecure client;
  client.setInsecure();   // simple; pin the certificate for production
  HTTPClient http;
  String url = String("https://api.telegram.org/bot") + TG_TOKEN + "/sendMessage";
  if (!http.begin(client, url)) return false;
  http.addHeader("Content-Type", "application/json");
  String body = String("{"chat_id":"") + TG_CHAT + "","text":"" + text + ""}";
  int code = http.POST(body);
  http.end();
  return code == 200;
}
const int PIN_ADC = 3;
const float R1 = 100000.0, R2 = 22000.0;      // divider; max 15 V gives 2.7 V at the pin
float CAL = 1.00;                              // multiply to match a multimeter reading
const float LOW_V = 12.0;                      // low battery warning (4S LiFePO4: about 12.0 V)
const uint64_t SLEEP_US = 10ULL * 60ULL * 1000000ULL;
RTC_DATA_ATTR int lowSent = 0;

float readBatteryV() {
  analogReadResolution(12); analogSetPinAttenuation(PIN_ADC, ADC_11db);
  long s = 0; for (int i = 0; i < 32; i++) { s += analogReadMilliVolts(PIN_ADC); delay(3); }
  float mv = s / 32.0;                         // calibrated millivolts at the pin
  return mv / 1000.0 * (R1 + R2) / R2 * CAL;
}
void setup() {
  Serial.begin(115200);
  float v = readBatteryV(); Serial.printf("Battery: %.2f Vn", v);
  if (v < LOW_V && lowSent < 3 && connectWiFi()) { telegramSend(String("Battery low: ") + String(v, 2) + " V"); lowSent++; }
  if (v >= LOW_V + 0.3) lowSent = 0;
  esp_sleep_enable_timer_wakeup(SLEEP_US); esp_deep_sleep_start();
}
void loop() {}

Build tips

  • Calibrate CAL with a multimeter; the divider draws about 0.12 mA continuously.
  • Use 1 MΩ / 220 kΩ and a 100 nF capacitor on the ADC pin for an even lower leakage.

What you need

Buy the parts kit · Estimated total: $7.00

ESP32-C3 SuperMini × 1
$2.50
Buy at AliExpress Buy at Amazon.com
Resistor kit 1/4W (1k, 2k, 4.7k, 10k, 22k, 100k) × 1
$1.50
Buy at AliExpress Buy at Amazon.com
Electrolytic capacitors 100-470 uF + ceramic 100 nF × 1
$1.50
Buy at AliExpress Buy at Amazon.com
Dupont jumper wires (M-F, M-M) × 1
$1.50
Buy at AliExpress Buy at Amazon.com
🇺🇦 Ukraine🌍 Other countries

Other countries: order these parts on AliExpress with the links below.

Some links are affiliate links: we may earn a commission at no extra cost to you.

Related ideas

⚠ Caution

Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.

Car and generator starter battery watch

Wakes rarely, measures the 12 V battery at rest and warns before it is too flat to start the engine, with almost no drain of its own.

  • Board ESP32-C3 SuperMini
  • Level ●●○ Intermediate
  • Build time 1–2 h
Pick parts / kit
⚠ Caution

Batteries: watch polarity, use protected cells and a charger with protection, never charge damaged cells unattended.

Mini-UPS monitor for the router

Watches the battery of a 12 V mini-UPS that powers the router, shows its voltage, load and estimated runtime, and warns before it runs flat.

  • Board ESP32-C3 SuperMini
  • Level ●○○ Beginner
  • Build time 1–2 h
Pick parts / kit

Quiet cooling for the inverter cabinet

Runs the cabinet fan only as fast as the temperature needs, reports overheating and warns if the fan stops, so the inverter and the battery stay cool and quiet.

  • Board ESP32-C3 SuperMini
  • Level ●○○ Beginner
  • Build time 1–2 h
Pick parts / kit

← All module ideas →

Comments

No comments yet. Share your experience or ask a question.

Comments are moderated.