Battery life measurement lab
Build it step by step
0 of 7 doneCheck the sensor
Run the library's example and see the current.
Measure the idle Nano
Note the milliamps with the LED off.
Measure the LED
Note the current when it is on.
Work out the duty cycle
On time divided by the total time.
Compute the average
Average = on current × duty + off current × (1 − duty).
Calculate the life
Capacity in mAh divided by the average in mA.
Compare
Run on real batteries and note when it stops.
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
- Development BoardsBuyArduino Nano × 1
- SensorsBuy
- DisplaysBuy
- PowerBuy
- BatteriesBuy
- SemiconductorsBuyLED 5 mm red × 1
- PassivesBuyResistor 220 Ω × 1
Tick what you already own. It saves to My Garage, so “Build with what I have” knows.
Wiring
| From | To | Why |
|---|---|---|
| INA219 VCC | Nano 5V | Sensor power |
| INA219 GND | Nano GND | Ground |
| INA219 SDA | Nano A4 | I2C data |
| INA219 SCL | Nano A5 | I2C clock |
| OLED VCC, GND, SDA, SCL | Nano 5V, GND, A4, A5 | Shares the I2C bus |
| Battery pack plus | INA219 VIN+ | Current goes through the sensor |
| INA219 VIN− | Test project's positive supply | Measured load |
| Battery pack minus | Test project's GND and Nano GND | Common ground |
| Red LED and 220 Ω | Nano D6 to GND | Test 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.
| Check | Expected |
|---|---|
| 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 |
| Real run | Within 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 →
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.
