Built-in project · Arduino Uno R3

Battery level indicator on an Arduino Uno

An 18650 cell straight into A0: its voltage and charge in percent on a 16x2 I2C LCD, and a green, yellow or blinking red light as it runs down. The whole build, an Arduino Uno and 3 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Beginner Runs in your browser. Free, and no account needed.

The parts retail for about $37. On Mokxi they're free.

Battery level indicator · Arduino Uno R3live0.000 s 0.00x
Click to open it in the editor
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Battery level indicator: an 18650 lithium-ion cell's voltage on A0, its
// charge on a 16x2 I2C LCD and a traffic light module on an Arduino Uno.
//
//   A0      the cell's plus; the cell's minus on GND
//   2, 3, 4 the traffic light module's G, Y and R
//   LCD     SDA -> A4    SCL -> A5    VCC -> 5 V    GND -> GND
//
// One 18650 is 4.2 V full and 3.0 V empty, under the Uno's 5 V reference, so
// it goes straight into A0: no divider. analogRead() counts 0 to 1023 for 0 to
// 5 V, so the voltage is the count times 5 / 1023. A cell above 5 V (two
// 18650s, a 9 V battery) needs a divider in front of A0 and the sum scaled up.
//
// The charge is a straight line from 3.0 V (0 %) to 4.2 V (100 %), which is
// how the simulated cell behaves. A real cell's voltage is flatter in the
// middle and falls fast at the ends, so on the bench a straight line reads a
// little low at first and drops quickly near empty; a real gauge measures
// the current in and out instead.
//
//   green   over 50 %
//   yellow  20 % to 50 %
//   red     under 20 %, blinking: charge it
//
// Press Run, then drag the charge slider on the battery.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const int BATTERY_PIN = A0;
const int GREEN_PIN = 2;
const int YELLOW_PIN = 3;
const int RED_PIN = 4;

const float EMPTY_V = 3.0;
const float FULL_V = 4.2;

LiquidCrystal_I2C lcd(0x27, 16, 2);
bool blink = false;

void setup() {
  pinMode(GREEN_PIN, OUTPUT);
  pinMode(YELLOW_PIN, OUTPUT);
  pinMode(RED_PIN, OUTPUT);
  Serial.begin(9600);
  Serial.println("Battery level indicator");
  lcd.init();
  lcd.backlight();
  lcd.print("18650 battery");
}

void loop() {
  // Average 16 readings to steady the last digit.
  long sum = 0;
  for (int i = 0; i < 16; i++) sum += analogRead(BATTERY_PIN);
  float volts = sum / 16.0 * 5.0 / 1023.0;

  int percent = (int)((volts - EMPTY_V) / (FULL_V - EMPTY_V) * 100.0 + 0.5);
  percent = constrain(percent, 0, 100);

  blink = !blink;
  digitalWrite(GREEN_PIN, percent > 50 ? HIGH : LOW);
  digitalWrite(YELLOW_PIN, percent >= 20 && percent <= 50 ? HIGH : LOW);
  digitalWrite(RED_PIN, percent < 20 && blink ? HIGH : LOW);

  lcd.setCursor(0, 1);
  lcd.print(volts, 2);
  lcd.print(" V  ");
  lcd.print(percent);
  lcd.print(" %    ");

  Serial.print(volts, 2);
  Serial.print(" V  ");
  Serial.print(percent);
  Serial.println(" %");
  delay(500);
}

Parts list

5 parts, plus the jumper wires. They retail for about $37; on Mokxi they're free.

How it is wired

9 connections, pin by pin, read from the circuit itself. Each line is one set of pins joined together, by a jumper wire or a breadboard strip.

  • Ground: Arduino Uno R3 pin GND; Traffic light module pin GND
  • Arduino Uno R3 pin 4; Traffic light module pin R
  • Arduino Uno R3 pin 3; Traffic light module pin Y
  • Arduino Uno R3 pin 2; Traffic light module pin G
  • Arduino Uno R3 pin 5V; Character LCD with I2C backpack pin VCC
  • Ground: Arduino Uno R3 pin GND; Battery: AA, coin cell, LiPo pin V-; Character LCD with I2C backpack pin GND
  • Arduino Uno R3 pin A0; Battery: AA, coin cell, LiPo pin V+
  • Arduino Uno R3 pin A4; Character LCD with I2C backpack pin SDA
  • Arduino Uno R3 pin A5; Character LCD with I2C backpack pin SCL

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.