Projects / Battery life calculator lab for sensor nodes

Battery life calculator lab for sensor nodes

Beginner2 hGuided buildReviewed

Build it step by step

0 of 6 done
  1. ESP32INA219NANO1.22 A

    Wire the meter

    Put the INA219 in the 5 V supply line to the ESP32 board, with the Nano reading it.

  2. ESP32AHT20NANO26.6°C

    Check the reading

    Upload the Nano sketch and confirm a steady figure with the ESP32 awake.

  3. ESP32

    Upload the test sketch

    Run the ESP32 cycle: awake, Wi-Fi on, deep sleep.

  4. ESP32AHT2026.6°C

    Record each state

    Note the mA and seconds in each state from the Serial CSV.

  5. ESP32++

    Do the sums

    Average mA = Σ(mA × seconds) ÷ total seconds. Battery hours = capacity mAh × 0.8 ÷ average mA.

  6. ESP32AHT20++26.6°C

    Compare options

    Try a 2600 mAh 18650 against 4 AA cells and change the sleep time to see the effect.

In short

How do you build a battery life calculator lab for sensor nodes?

Measure a sensor node's current in each state with an INA219 and work out how long it would run on AA cells or an 18650. You need ESP32 DevKit V1, INA219 Current Monitor, AHT20 temperature and humidity sensor, Battery holder 18650 single, 18650 cell 2600 mAh (branded), Arduino Nano and SSD1306 OLED Display. It takes 6 steps and about 120 minutes.

Level
Beginner
Time
About 120 min
Parts
7

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
Nano 5VINA219 VCC and VIN+Power and measured supply
INA219 VIN-ESP32 VIN (5V) pinLoad under test
Nano GNDINA219 GND and ESP32 GNDCommon ground
INA219 SDA, SCLNano A4, A5I2C
OLED SDA, SCL, VCC, GNDNano A4, A5, 5V, GNDShows live current
AHT20 on the ESP32ESP32 3V3, GND, GPIO21 (SDA), GPIO22 (SCL)The sensor node under test

Code

Arduino IDE, board "Arduino Nano" (processor ATmega328P, or ATmega328P (Old Bootloader) for clones). Install "Adafruit INA219" and "U8g2". The INA219 cannot read the few microamps of a bare ESP32 in deep sleep, and a development board's regulator and USB chip draw several milliamps anyway, so the measured sleep current is for the whole board. Real battery life uses your own average, not published chip figures.

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

// ---- Nano meter sketch: reads current through the INA219 and shows it ----
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <U8x8lib.h>

Adafruit_INA219 ina;
U8X8_SSD1306_128X64_NONAME_HW_I2C oled(U8X8_PIN_NONE);

void setup() {
  Serial.begin(115200);
  ina.begin();
  ina.setCalibration_16V_400mA();      // finer steps (about 0.1 mA) for a small load
  oled.begin();
  oled.setFont(u8x8_font_chroma48medium8_r);
}

void loop() {
  float mA = ina.getCurrent_mA();
  Serial.print(millis()); Serial.print(','); Serial.println(mA, 1);   // save this as a CSV file
  char buf[17];
  dtostrf(mA, 7, 1, buf);
  oled.drawString(0, 2, "Current mA");
  oled.drawString(0, 4, buf);
  delay(50);
}

/* ---- ESP32 test sketch (upload separately to the ESP32 under test) ----
#include <WiFi.h>
const char* WIFI_NAME = "YOUR_WIFI_NAME";
const char* WIFI_PASSWORD = "YOUR_WIFI_PASSWORD";
void setup() {
  delay(5000);                                   // state 1: awake, radio off
  WiFi.begin(WIFI_NAME, WIFI_PASSWORD);
  unsigned long t = millis();
  while (WiFi.status() != WL_CONNECTED && millis() - t < 10000) delay(100);   // state 2: Wi-Fi on
  delay(3000);
  WiFi.disconnect(true);
  esp_sleep_enable_timer_wakeup(20ULL * 1000000ULL);
  esp_deep_sleep_start();                         // state 3: deep sleep for 20 s
}
void loop() {}
*/

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

0,120.1
50,119.6
100,120.0
150,120.4
200,120.4
250,119.8

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
ESP32 awake, no Wi-FiRoughly tens of mA
Wi-Fi connectingShort peaks above 100 mA
Deep sleepFalls to a few mA on a dev board
Sleep time doubledAverage current drops and calculated hours rise

If it does not work

Reads zero

Current must pass through VIN+ to VIN-, not around the module.

ESP32 will not start

The module's shunt drops about 0.3 V at 400 mA. Check the load.

Numbers jump

Wi-Fi bursts are short. Average over a whole cycle.

OLED shows nothing

Check I2C addresses and wiring.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the battery life calculator lab for sensor nodes?

ESP32 DevKit V1, INA219 Current Monitor, AHT20 temperature and humidity sensor, Battery holder 18650 single, 18650 cell 2600 mAh (branded), Arduino Nano and SSD1306 OLED Display.

How long does the battery life calculator lab for sensor nodes take to build?

About 120 minutes for a beginner maker, in 6 steps.

How do I know the battery life calculator lab for sensor nodes works?

ESP32 awake, no Wi-Fi: Roughly tens of mA. Wi-Fi connecting: Short peaks above 100 mA. Deep sleep: Falls to a few mA on a dev board.

Reads zero: what should I check?

Current must pass through VIN+ to VIN-, not around the module.

ESP32 will not start: what should I check?

The module's shunt drops about 0.3 V at 400 mA. Check the load.

Numbers jump: what should I check?

Wi-Fi bursts are short. Average over a whole cycle.

OLED shows nothing: what should I check?

Check I2C addresses and wiring.