Projects / Battery life measurement lab

Battery life measurement lab

Intermediate1 hGuided buildReviewed

Build it step by step

0 of 7 done
  1. NANOINA2190.30 A

    Check the sensor

    Run the library's example and see the current.

  2. NANO9.4 mA

    Measure the idle Nano

    Note the milliamps with the LED off.

  3. NANOINA2190.31 A

    Measure the LED

    Note the current when it is on.

  4. NANO

    Work out the duty cycle

    On time divided by the total time.

  5. NANOINA2190.31 A

    Compute the average

    Average = on current × duty + off current × (1 − duty).

  6. NANO

    Calculate the life

    Capacity in mAh divided by the average in mA.

  7. NANO

    Compare

    Run on real batteries and note when it stops.

In short

How do you build a battery life measurement lab?

Measure a project's current in active and sleep modes and work out how long a battery will last. You need Arduino Nano, INA219 Current Monitor, SSD1306 OLED Display, Battery holder 4 × AA with switch, AA alkaline battery × 4, LED 5 mm red and Resistor 220 Ω. It takes 7 steps and about 60 minutes.

Level
Intermediate
Time
About 60 min
Parts
7

Parts list

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

Wiring

FromToWhy
INA219 VCCNano 5VSensor power
INA219 GNDNano GNDGround
INA219 SDANano A4I2C data
INA219 SCLNano A5I2C clock
OLED VCC, GND, SDA, SCLNano 5V, GND, A4, A5Shares the I2C bus
Battery pack plusINA219 VIN+Current goes through the sensor
INA219 VIN−Test project's positive supplyMeasured load
Battery pack minusTest project's GND and Nano GNDCommon ground
Red LED and 220 ΩNano D6 to GNDTest load that blinks

Code

The INA219 measures the current that flows from VIN+ to VIN−, so the test project must be on the VIN− side. The sensor handles up to about 3.2 A with the default setting and 26 V. The Nano and OLED draw their own current from the same pack, so power the Nano from USB while you measure only the project, or subtract their share. The estimate is capacity divided by the average current; real batteries give less at high current and in the cold, so treat the answer as an upper bound and compare it with a real run. For a project that sleeps, run the sketch for a few minutes so that bursts and sleep are both counted. Arduino IDE, board "Arduino Nano" (try the Old Bootloader option if the upload fails).

Runs on a virtual Arduino Nano 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).

// Battery life lab: log current, average it over a duty cycle, estimate runtime. Library: Adafruit INA219 and SSD1306.
#include <Wire.h>
#include <Adafruit_INA219.h>
#include <Adafruit_SSD1306.h>

Adafruit_INA219 ina;
Adafruit_SSD1306 oled(128, 64, &Wire, -1);
const float CAPACITY_MAH = 2000.0;       // AA pack capacity (alkaline gives less at high current)
const int LED = 6;

float sumMa = 0; unsigned long samples = 0;

void setup() {
  pinMode(LED, OUTPUT);
  Serial.begin(115200);
  ina.begin();
  oled.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  oled.setTextColor(SSD1306_WHITE);
}

void loop() {
  // The test load: 100 ms on, 900 ms off = a 10 percent duty cycle.
  digitalWrite(LED, HIGH);
  for (int i = 0; i < 10; i++) { float ma = ina.getCurrent_mA(); sumMa += ma; samples++; delay(10); }
  digitalWrite(LED, LOW);
  for (int i = 0; i < 90; i++) { float ma = ina.getCurrent_mA(); sumMa += ma; samples++; delay(10); }

  float avg = sumMa / samples;
  float hours = (avg > 0.01) ? CAPACITY_MAH / avg : 0;
  oled.clearDisplay(); oled.setTextSize(1);
  oled.setCursor(0, 0);  oled.print("Average current");
  oled.setTextSize(2); oled.setCursor(0, 12); oled.print(avg, 1); oled.print(" mA");
  oled.setTextSize(1);
  oled.setCursor(0, 40); oled.print("Battery life");
  oled.setCursor(0, 52); oled.print(hours, 0); oled.print(" h  "); oled.print(hours / 24.0, 1); oled.print(" d");
  oled.display();
  Serial.print(avg); Serial.print(" mA average, "); Serial.print(hours); Serial.println(" h estimate");
}

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 Arduino Nano with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

119.99 mA average, 16.67 h estimate
119.98 mA average, 16.67 h estimate
119.99 mA average, 16.67 h estimate
119.98 mA average, 16.67 h estimate
119.99 mA average, 16.67 h estimate

Test checklist

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

CheckExpected
LED onReading jumps by about 10 mA
AverageMatches your hand calculation within 10 percent
Change the duty cycleEstimated life changes in proportion
Real runWithin a factor of two of the estimate

If it does not work

Reads zero

Check VIN+ and VIN− are in series with the load.

Negative values

Swap VIN+ and VIN−.

Noisy readings

Average more samples.

Estimate far too long

Include the Nano and OLED current, and use a realistic capacity.

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 measurement lab?

Arduino Nano, INA219 Current Monitor, SSD1306 OLED Display, Battery holder 4 × AA with switch, AA alkaline battery × 4, LED 5 mm red and Resistor 220 Ω.

How long does the battery life measurement lab take to build?

About 60 minutes for a intermediate maker, in 7 steps.

How do I know the battery life measurement lab works?

LED on: Reading jumps by about 10 mA. Average: Matches your hand calculation within 10 percent. Change the duty cycle: Estimated life changes in proportion.

Reads zero: what should I check?

Check VIN+ and VIN− are in series with the load.

Negative values: what should I check?

Swap VIN+ and VIN−.

Noisy readings: what should I check?

Average more samples.

Estimate far too long: what should I check?

Include the Nano and OLED current, and use a realistic capacity.