Battery voltage through a resistor divider (deep sleep)

Measures a 12-15 V battery with a divider and an ESP32-C3 ADC, and warns in Telegram when it gets low.

Level: Beginner Board: ESP32-C3 SuperMini Power & blackouts

⚠ Safety

Li-ion cells: watch polarity, use protected cells and a charger with protection, never leave a damaged cell charging unattended.

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.

Wiring

ModuleModule pinESP32 pinNote
Battery + to R1 (100 kΩ)divider topR1 to the ADC nodefor a 4S LiFePO4 / 12 V lead-acid battery up to 15 V
ADC nodeanalogGPIO3 (ADC1)R2 22 kΩ to GND and 100 nF to GND at this node
Battery -GNDGNDcommon ground with the ESP32

Code

battery-divider.ino
#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 V\n", 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() {}

Flash a ready firmware from the browser Paste your error into the AI Error Doctor

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.

← ← All projects

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

Comments

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

Comments are moderated.