LiFePO4 / battery monitor

Shows voltage, current and power of a battery bank (INA219) so you know the real charge and load during outages.

Power & batteries Board: ESP32 DevKit V1 (WROOM-32) Level: ●●○ ⏱ 1–2 h

⛔ High risk

High-current battery: a short circuit can start a fire. Put a fuse next to the battery plus, use thick enough wires and respect the BMS limits.

🛒 Ready kit in the shop

Know what is really left in the battery

During an outage the question is always the same: how long can the fridge, router and lights run? The inverter display shows a voltage that jumps with the load. Here the INA219 measures both the battery voltage and the current, so the ESP32 can integrate the energy and show a real figure.

Three things that decide whether it works

The INA219 reads the voltage across a shunt of at most ±320 mV. The board shunt of 0.1 Ω is fine for a few amps. For an inverter that pulls tens of amps use an external 75 mV shunt (for example 100 A / 75 mV) and recalibrate the current scale in code. The bus input of the chip is specified up to 26 V on the common mode, so a 24 V LiFePO4 bank that charges to about 29 V is at or over the limit; take an INA226 or a Hall-effect module in that case.

On LiFePO4 the voltage curve is almost flat from 20 % to 90 %, so voltage alone gives a poor state of charge. Count ampere-hours instead and correct the counter when the BMS reports full. Put a fuse on the positive wire right next to the battery.

Components

  • ESP32 DevKit V1 (30 pin, WROOM-32)
  • INA219 current/voltage sensor module
  • Inline fuse holder + fuses
  • Lever connectors (WAGO type) 2/3-pin

Wiring

Module Module pin ESP32 pin Note
INA219 VCC 3V3 3V3
INA219 GND GND GND
INA219 SDA I2C GPIO21
INA219 SCL I2C GPIO22
INA219 Vin+ / Vin- shunt battery + → load + max 26 V, 3.2 A on the module

Libraries

  • Adafruit INA219

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 <Wire.h>
#include <Adafruit_INA219.h>
Adafruit_INA219 ina;
#include <WebServer.h>

WebServer server(80);
String buildJson();   // implemented in the project below

String num(float v, int d = 1) { return isnan(v) ? String("null") : String(v, d); }

const char PAGE_HTML[] PROGMEM = R"rawliteral(
<!doctype html><html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
<title>ESP32</title>
<style>body{font-family:system-ui;background:#0b0f14;color:#e6edf3;margin:20px}table{border-collapse:collapse}td{padding:6px 14px;border-bottom:1px solid #223}button,input{font-size:16px;padding:8px 14px;margin:4px;border-radius:6px;border:1px solid #345;background:#16202b;color:#e6edf3}</style>
</head><body><h2>ESP32</h2><table id="t"></table>
<script>
function load(){fetch('/json').then(r=>r.json()).then(d=>{let h='';for(const k in d){h+='<tr><td>'+k+'</td><td>'+d[k]+'</td></tr>';}document.getElementById('t').innerHTML=h;});}
load();setInterval(load,2000);
</script></body></html>
)rawliteral";

void startWeb() {
  server.on("/", []() { server.send_P(200, "text/html", PAGE_HTML); });
  server.on("/json", []() { server.send(200, "application/json", buildJson()); });
  server.begin();
}
// 4S LiFePO4 (12.8 V): rough resting-voltage → % table. Under load the figure is only an estimate.
int socFromVoltage(float v) {
  const float tv[] = {10.0, 12.0, 12.8, 13.0, 13.2, 13.3, 13.4, 14.4};
  const int   ts[] = {0, 9, 14, 17, 30, 40, 99, 100};
  if (v <= tv[0]) return 0;
  for (int i = 1; i < 8; i++) if (v <= tv[i]) return ts[i-1] + (ts[i]-ts[i-1]) * (v-tv[i-1]) / (tv[i]-tv[i-1]);
  return 100;
}
float busV = 0, curA = 0;
String buildJson() {
  return String("{"V":") + num(busV, 2) + ","A":" + num(curA, 2) + ","W":" + num(busV * curA, 1) + ","SoC_est_%":" + socFromVoltage(busV) + "}";
}
void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22);
  if (!ina.begin()) { Serial.println("INA219 not found"); }
  connectWiFi();
  startWeb();
}
void loop() {
  server.handleClient();
  static uint32_t t = 0;
  if (millis() - t > 1000) { t = millis(); busV = ina.getBusVoltage_V(); curA = ina.getCurrent_mA() / 1000.0; }
}

Build tips

  • Calibrate DIV_RATIO with a multimeter; LiFePO4 voltage is flat, so the percentage is only an estimate.
  • Add a 100 nF capacitor on the ADC pin and measure at rest for the best accuracy.

What you need

Buy the parts kit · Estimated total: $10.00

ESP32 DevKit V1 (30 pin, WROOM-32) × 1
$4.00
Buy at AliExpress Buy at Amazon.com
INA219 current/voltage sensor module × 1
$2.00
Buy at AliExpress Buy at Amazon.com
Inline fuse holder + fuses × 1
$1.50
Buy at AliExpress Buy at Amazon.com
Lever connectors (WAGO type) 2/3-pin × 1
$2.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

⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Inverter load manager

Watches the inverter / battery current and switches off non-critical loads with a relay when the battery runs low.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●● Advanced
  • Build time 3–5 h
Pick parts / kit
⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Mains voltage monitor

Measures 220 V mains through an isolated ZMPT101B module and warns about sags, surges and blackouts in Telegram or Home Assistant.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 2–3 h
Pick parts / kit
⛔ High risk

Mains 220–230 V can kill. Work with the power off, use certified isolated modules in an enclosure, fuse the line, and ask a qualified electrician if you are not sure.

Smart energy meter

Reads voltage, current, power and kWh with PZEM-004T and publishes them to a web page, MQTT or Home Assistant.

  • Board ESP32 DevKit V1 (WROOM-32)
  • Level ●●○ Intermediate
  • Build time 2–4 h
Pick parts / kit

← All module ideas →

Comments

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

Comments are moderated.