Every home has a drawer full of batteries, and nobody knows which ones still work. In this capstone you build a battery tester with the UNO R3 Starter Kit. Touch the red and black wires to a battery and the tester works out by itself whether it is a 1.5 V cell (AA, AAA, C or D), a 3 V coin cell or a 9 V block. The LCD shows its voltage and a charge bar, and a green, yellow or red LED says "good", "weak" or "replace". The UNO can only measure up to 5 V, so you use two resistors as a voltage divider to measure the 9 V battery safely. And because the UNO's own 5 V isn't exactly 5 V, the sketch measures it against a reference inside the chip first, so the readings are accurate.
What you'll learn
- How a voltage divider makes a higher voltage small enough for an analog input
- How analogRead() turns a voltage into a number, and why its reference voltage matters
- How the UNO can measure its own supply with the internal 1.1 V reference
- How to recognise a battery type from its voltage and turn the voltage into a charge percentage
- How to draw a bar on the 1602 LCD with your own character
What you need
- UNO R3 board and USB cable (UNO R3 Starter Kit)
- 1602 LCD display and a 10 kΩ potentiometer (UNO R3 Starter Kit)
- Resistors: 10 kΩ (brown, black, orange), 4.7 kΩ (yellow, violet, red) and 4 × 220 Ω (UNO R3 Starter Kit)
- 3 LEDs: green, yellow and red (UNO R3 Starter Kit)
- A red and a black jumper wire as test probes
- Batteries to test, for example AA cells, a CR2032 coin cell and the 9 V battery from the kit
- Breadboard and jumper wires (UNO R3 Starter Kit)
Safety: only test single batteries of up to 9 V: AA, AAA, C, D, coin cells and 9 V blocks. Never connect the probes to a phone charger, a car battery, a power supply or the mains. Don't short a battery by touching the red and black probes together on it, and never test a battery that is swollen, leaking or warm.
Before you start
The LCD has 16 connections in a row. If yours has no pins, solder 16 pins from the 40-pin male header in the kit first, as in the LCD lesson.
Wiring

Build it step by step
Plug in all the parts first, then add the wires in this order. In each picture the new wires are bright and the ones from earlier steps are faded. Click a picture to see it larger.





| From | To |
|---|---|
| LCD VSS, RW and K | GND |
| LCD VDD | 5V |
| LCD V0 | Middle pin of the potentiometer (outer pins to 5V and GND) |
| LCD RS, E | UNO pins 9, 8 |
| LCD D4, D5, D6, D7 | UNO pins 5, 4, 3, 2 |
| LCD A (backlight +) | 220 Ω resistor, then 5V |
| Red probe wire | 10 kΩ resistor (column 34), then on to column 38 |
| Column 38 (the middle of the divider) | UNO A0, and 4.7 kΩ resistor to GND |
| Black probe wire | GND |
| UNO pins 11, 12, 13 | 220 Ω resistor → green, yellow, red LED long leg |
| All LED short legs | GND |
The two loose probe wires are what you touch to the battery: red to + (the end with the bump on an AA, the flat side marked + on a coin cell, the smaller snap on a 9 V), black to −. The 4.7 kΩ resistor also pulls A0 to GND when nothing is connected, so the tester reads 0 V instead of random numbers.
Build this circuit in EEMENTOR Lab
Wire it yourself in your browser before you touch a real wire. No sign-up, nothing to install.
Build it yourself
- The UNO and this lesson’s parts are already placed. Wire them using the wiring table above. Pins with no wire show red.
- Missing a part? Add it from the parts list on the left.
- Press Run, then try the inputs: click, hold or slide them under Selected.
- Then try this lesson’s sketch: paste it into the Code tab and press Compile & Run.
EEMENTOR Lab is free and in beta. Results are a guide; a real build can differ slightly. About the Lab
The sketch
The LiquidCrystal library is built into the Arduino IDE, so there is nothing to install.
#include <LiquidCrystal.h>
LiquidCrystal lcd(9, 8, 5, 4, 3, 2); // RS, E, D4, D5, D6, D7
// ---------- settings ----------
const int PROBE = A0; // the middle of the voltage divider
const float R_TOP = 10000.0; // 10 kΩ from the red probe to A0
const float R_BOTTOM = 4700.0; // 4.7 kΩ from A0 to GND
const int GREEN = 11, YELLOW = 12, RED = 13; // the verdict LEDs
// The batteries it knows: name, the voltage range it recognises, full and empty voltage
struct Battery { const char* name; float low, high, full, empty; };
const Battery TYPES[] = {
{"1.5V cell", 0.3, 2.0, 1.55, 1.00}, // AA, AAA, C, D (alkaline)
{"3V coin", 2.0, 3.6, 3.10, 2.50}, // CR2032 and friends
{"9V block", 6.0, 10.5, 9.50, 7.00},
};
byte block[8] = {31, 31, 31, 31, 31, 31, 31, 31}; // a full square for the charge bar
// The UNO's real supply voltage, measured against its own 1.1 V reference inside the chip.
float readVcc() {
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1); // measure 1.1 V with 5V as the reference
delay(2);
ADCSRA |= _BV(ADSC);
while (bit_is_set(ADCSRA, ADSC));
return 1125.3 / ADC; // 1.1 V × 1023 = 1125.3, so Vcc = 1125.3 / reading
}
float readBattery() {
float vcc = readVcc();
analogRead(PROBE); // the first reading after switching is often off: throw it away
long sum = 0;
for (int i = 0; i < 32; i++) sum += analogRead(PROBE);
float atPin = sum / 32.0 * vcc / 1023.0; // the voltage at A0
return atPin * (R_TOP + R_BOTTOM) / R_BOTTOM; // undo the divider: the battery's voltage
}
void showLeds(int g, int y, int r) {
digitalWrite(GREEN, g);
digitalWrite(YELLOW, y);
digitalWrite(RED, r);
}
void setup() {
lcd.begin(16, 2);
lcd.createChar(0, block);
pinMode(GREEN, OUTPUT);
pinMode(YELLOW, OUTPUT);
pinMode(RED, OUTPUT);
Serial.begin(9600);
lcd.print("Battery tester");
delay(1000);
}
void loop() {
float v = readBattery();
lcd.clear();
if (v < 0.3) { // nothing connected
lcd.print("Connect battery");
lcd.setCursor(0, 1);
lcd.print("red + / black -");
showLeds(LOW, LOW, LOW);
} else if (v > 10.5) {
lcd.print("Too high! Remove");
lcd.setCursor(0, 1);
lcd.print(v, 1);
lcd.print(" V");
showLeds(LOW, LOW, HIGH);
} else {
int t = -1;
for (int i = 0; i < 3; i++) if (v >= TYPES[i].low && v < TYPES[i].high) t = i;
if (t < 0) { // between the known ranges
lcd.print("Unknown battery");
lcd.setCursor(0, 1);
lcd.print(v, 2);
lcd.print(" V");
showLeds(LOW, LOW, LOW);
} else {
const Battery& b = TYPES[t];
int charge = constrain((int)((v - b.empty) / (b.full - b.empty) * 100), 0, 100);
lcd.print(b.name);
lcd.setCursor(11, 0);
lcd.print(v, v < 10 ? 2 : 1);
lcd.setCursor(0, 1);
for (int i = 0; i < charge / 10; i++) lcd.write(byte(0)); // one square per 10 %
lcd.setCursor(11, 1);
lcd.print(charge);
lcd.print("%");
if (charge >= 60) showLeds(HIGH, LOW, LOW); // good
else if (charge >= 25) showLeds(LOW, HIGH, LOW); // weak
else showLeds(LOW, LOW, HIGH); // replace
Serial.print(b.name);
Serial.print(": ");
Serial.print(v, 2);
Serial.print(" V, ");
Serial.print(charge);
Serial.println(" %");
}
}
delay(500);
}
Try it
- Upload the sketch. The LCD says "Battery tester", then "Connect battery". If you see only blocks or nothing, turn the potentiometer until the text is sharp.
- Touch the probes to a new AA cell: red to +, black to −. The LCD shows "1.5V cell 1.58" and a full bar with "100%", and the green LED lights.
- Try an old AA from a remote control: maybe "1.5V cell 1.35", six squares, "63%", still green, or yellow when it is lower.
- Test the 9 V battery from the kit: "9V block 9.21" and its charge. Test a coin cell: "3V coin 3.05".
- Swap the probes on purpose: the tester shows "Connect battery", because the voltage is below zero, which the UNO reads as 0. Nothing is harmed, because the 10 kΩ resistor keeps the current into A0 tiny. Never connect anything backwards straight to a pin.
- Open the Serial Monitor at 9600 baud to see every reading, and write down your batteries' voltages before you throw any away.
An open-circuit voltage only tells part of the story: a nearly empty battery can still show a good voltage when nothing is drawing current from it, and drop as soon as a motor or a torch bulb pulls on it. Real battery testers put a small load on the battery while they measure. For finding the dead ones in a drawer, the voltage works well.
What you should see

How it works

analogRead() compares the voltage on A0 with its reference, normally the UNO's 5V, and gives a number from 0 (0 V) to 1023 (the reference). Anything above 5 V on a pin could damage the chip, so a 9 V battery must never go straight to A0. The voltage divider solves this: two resistors in series share the battery voltage in proportion to their size. A0 sits in between and gets the share of the 4.7 kΩ resistor: 4.7 / (10 + 4.7) = 0.32 of the battery voltage. 9.5 V becomes 3.04 V, safe for the pin. The sketch multiplies back by 14.7 / 4.7 = 3.13. The highest voltage the tester could ever read is 5 V × 3.13 = 15.6 V, which is why the sketch warns above 10.5 V.
There is a catch: USB ports don't give exactly 5 V. Some give 4.8 V, some 5.1 V, and every reading would be off by the same few percent. So before each measurement, readVcc() sets the UNO's analog-to-digital converter up by hand to measure its own internal reference, a voltage of about 1.1 V made inside the chip that doesn't change with the USB supply. From the result it works out the real supply: Vcc = 1.1 × 1023 / reading. The battery reading is then scaled with this real Vcc. Each chip's reference is a little different (1.0 to 1.2 V), so for lab precision you would measure it once with a multimeter and put your value into the sketch. The sketch also averages 32 readings to smooth out noise.
To recognise the battery, the sketch checks which range the voltage falls into: 0.3 to 2 V is a 1.5 V cell, 2 to 3.6 V a coin cell, 6 to 10.5 V a 9 V block. The charge is where the voltage lies between "empty" and "full" for that type: for an alkaline AA, 1.55 V is full and 1.0 V is empty, so 1.30 V is about 55 %. The bar is made of a custom character, a full 5 × 8 block, drawn once with createChar() and printed once per 10 %.
Try this next
- Add a load test: a push button that connects a 220 Ω resistor across the battery while you hold it, and show how far the voltage drops. A good battery hardly drops; a tired one sinks.
- Add rechargeable NiMH cells as a fourth type: they are full at about 1.4 V and empty at 1.1 V, so they need their own range. (Hint: you will need a button to choose, because their range overlaps with alkaline cells.)
- Keep a list of the last 5 batteries tested and scroll through them with a button.
- Calibrate it: measure a battery with a multimeter, then change 1125.3 in
readVcc()until the tester agrees. - Add a buzzer that beeps three times for "replace", so you can test without looking.
Troubleshooting
- The LCD shows blocks or nothing: turn the potentiometer slowly from end to end. Check VSS, RW and K on GND and VDD on 5V.
- It always says "Connect battery": check that A0 goes to column 38, the point between the two resistors, and that the black probe is on GND. Press the probes firmly onto the battery's ends.
- The voltage jumps around: hold the probes steady; a loose touch makes the reading change. The 32-reading average helps once the contact is good.
- Readings are about twice too high (an AA shows up as a coin cell, the 9 V says Too high): the two resistors are swapped. The 10 kΩ goes between the red probe and A0, the 4.7 kΩ between A0 and GND.
- Readings are a few percent off a multimeter: your chip's internal reference isn't exactly 1.1 V. Calibrate it as described in Try this next.
- An LED never lights: check that LED's direction (long leg to the resistor) and its wire to pin 11, 12 or 13.