Animations are fun, but a voltage readout on a tiny OLED is useful. I use this pattern on robot packs and portable lab boxes, so my students can watch the battery sag under load instead of trusting a "full" LED that lies.
Warning up front, and I mean it: never put a raw Li-ion pack above 5V straight into an Arduino analog pin.
You need a voltage divider, and I'd add a fuse or proper battery protection too. Here I'm assuming a
simple two-resistor divider into A0.
What you'll need
- An Arduino Uno or Nano, whichever you've got
- 0.96" I2C SSD1306 OLED
- Two resistors for your divider. I'd start with 100k + 100k (÷2), which handles packs up to ~10V on a 5V Arduino
- A battery pack with a safe measurement point. I'd pick a protected one
Divider math (keep it honest)
Say you've got R1 (from battery+ to A0) and R2 (from A0 to GND):
V_pin = V_battery × R2 / (R1 + R2)
V_battery = V_pin × (R1 + R2) / R2
Here's an example. R1 = R2 = 100k → divide by 2. An 8.4V 2S pack becomes 4.2V at A0, which a 5V Uno can take. A 12V lead-acid needs a bigger ratio, say 100k / 47k, so you keep the pin under 5V.
Resistors come with ±1% or ±5% tolerance. Your reading will be off until you calibrate against a multimeter. That's normal. Build the calibration into your sketch.
How you wire it
- OLED: VCC→5V, GND→GND, SDA→A4, SCL→A5 (Uno)
- Battery+ → R1 → node → R2 → GND
- Node → Arduino A0
- Battery− → Arduino GND (you need a common ground)
Check polarity twice before you connect your pack. Reverse polarity into the divider can still hurt the pin if you short something else too. Take a breath. Wire it once, and wire it right.
Sketch: OLED voltmeter with calibration
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
Adafruit_SSD1306 display(128, 64, &Wire, -1);
// Divider: R1 (top) and R2 (bottom). Change to match your resistors.
const float R1 = 100000.0;
const float R2 = 100000.0;
const float DIV = (R1 + R2) / R2; // 2.0 for equal resistors
// Measure Arduino 5V rail with a meter if you can; USB is often ~4.9–5.1V
const float VREF = 5.0;
// Tweak after comparing OLED vs multimeter (e.g. 1.02 if you read 2% low)
const float CAL = 1.00;
const int PIN = A0;
float readBatteryVolts() {
// Average a few samples to reduce noise
long sum = 0;
for (int i = 0; i < 16; i++) {
sum += analogRead(PIN);
delay(2);
}
float adc = sum / 16.0;
float vPin = adc * (VREF / 1023.0);
return vPin * DIV * CAL;
}
void setup() {
analogReference(DEFAULT);
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for (;;);
}
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
}
void loop() {
float v = readBatteryVolts();
display.clearDisplay();
display.setTextSize(1);
display.setCursor(0, 0);
display.println(F("Pack voltage"));
display.setTextSize(3);
display.setCursor(0, 22);
display.print(v, 2);
display.setTextSize(2);
display.print(F(" V"));
// Simple bar (0–12V scale — change maxV for your pack)
const float maxV = 9.0;
int bar = (int)constrain(v / maxV * 120, 0, 120);
display.drawRect(0, 54, 124, 8, SSD1306_WHITE);
display.fillRect(2, 56, bar, 4, SSD1306_WHITE);
display.display();
delay(300);
}
Calibrate it in five minutes
- Measure your pack with a decent multimeter.
- Write down what the OLED shows.
- Set
CAL = multimeter / oled(example: 8.20 / 8.00 = 1.025). - Re-upload. Check again under a small load. Some packs droop, and that's real, not a bug.
Measure the Uno's 5V pin too while it runs from USB. If you read 4.85V, put VREF = 4.85.
USB power is rarely a perfect 5.000V.
What you'll learn (beyond the number)
- You learn the analog input limits and why dividers exist
- You see noise averaging at work, since
analogReadis jittery on USB-powered boards - You update an OLED without redrawing the whole UI every microsecond
- You find out why "percentage battery" from voltage alone is rough. Chemistry curves aren't linear
Safety notes I tell students
- Use protected Li-ion packs. Bare cells and hobby wiring are how your bench ends up with scars.
- Don't power motors from the same weak USB port you measure with, then expect stable readings. You won't get them.
- If you monitor a car battery, remember load dumps and spikes. Add protection or use a proper module.
Ideas you can try next
- Add a low-voltage warning icon. You can flash a bitmap when V < threshold.
- Log the min and max since boot, so you can see the worst dip.
- Show mV and a bigger bar if you're demoing in a classroom, so your students can read it from the back.
- Combine it with a weather page. I'd reuse the DHT22 weather station pattern and a button menu.
- If you want long battery sessions, blank the OLED between samples. See OLED power consumption.
If you'd rather not hand-pack bitmaps for icons and bars, draw shapes in the shape exporter or drop a small warning glyph from the animation maker.
Percentage is a fiction (and I'll say it out loud)
Mapping voltage to "78%" looks product-ready, but it can fool you. Lithium curves are flat in the middle and fall off near empty. On a teaching meter I show volts first. If you must show a bar, label it as approximate, or calibrate it against discharge data for your pack's chemistry.
Under motor load the voltage sags, and at rest it recovers. If you sample only while the robot drives, the number will look "jumpy" even when your divider math is perfect. Average more samples, or sample in a state you know is idle.
A 2S pack, checked against a meter
Here's the build I use when my students bring a 2S Li-ion pack that should read 8.40 V full and about 6.0 V empty (3.0 V per cell). I use R1 = 100k from pack+ to the A0 node and R2 = 100k from that node to GND, so the pin sees half. At 8.40 V the node sits at 4.20 V, which a 5 V Uno can read. A 3S pack at 12.6 V would put 6.3 V on A0 with the same pair. That's over the pin limit. Don't reuse equal resistors for anything that can exceed about 10 V on a 5 V Arduino.
The USB "5 V" on the Uno I last measured was 4.95 V, so VREF in your sketch should be 4.95, not a hopeful 5.00. With the pack at a real 8.40 V, the ADC count is about 4.20 / 4.95 × 1023 ≈ 868. If the code still assumes VREF = 5.0, it turns 868 into about 4.24 V at the pin and 8.48 V on the OLED. The divider's fine. The reference isn't. Set CAL = 8.40 / 8.48 ≈ 0.99 only after VREF matches the 5 V pin, then check again with a 100 Ω load or a small motor so you can see sag. Sag that recovers when the load stops is the pack. You haven't got a software bug.
A 100 nF ceramic from the A0 node to GND, close to the Arduino, knocks down the jumpy least-significant bit you'll get on a USB-powered bench. Keep your OLED on A4/A5 (or pins 20/21 on a Mega) and the divider only on A0. Wire the battery node into SDA and you've killed your display.
For a low-battery mark I draw a second line under the volts instead of a fake percentage. With this 2S chemistry I treat under 6.4 V as "stop driving" and under 7.4 V as "getting low," and I print those words only. The bar in the sketch above stays a voltage bar. If your pack is 2S, change maxV to 8.4, not 9.0.
display.setTextSize(1);
display.setCursor(0, 12);
if (v < 6.4) display.print(F("STOP - pack low"));
else if (v < 7.4) display.print(F("Low"));
else display.print(F("OK"));
Sample while the robot is idle for a stable number. Sample again while the motors run to see the sag. If you show both, a half-second apart, you'll teach more than a single filtered average that hides the dip.
Readings you can't trust
- Your OLED shows 0.00 V and never moves. A0 isn't on the resistor node, R1 is open, or the pack negative isn't tied to Arduino GND. Meter the node. If your meter sees half the pack and the OLED sees zero, the jumper into A0 is the break.
- Reading stuck near the top of the scale (about
VREF × DIV). Either R2 is open, so A0 floats or is pulled up, or you've put 4095 in the formula on a 10-bit Uno. Uno and MegaanalogReadtop out at 1023. ESP32 Arduino cores are usually 0-4095 against 3.3 V. Mix those constants and a 7 V pack looks like 16 V. - The number jumps by 0.1-0.3 V every refresh. You're taking too few samples, there's no capacitor on the node, or a motor shares your USB port. Average 16 or 32 samples, add 100 nF, and don't power the motor from the laptop port that feeds the ADC reference.
- Your multimeter and OLED disagree by a fixed ratio. That's
CAL, or a resistor that isn't the value you typed. Parts marked 100k are often ±5%. Measure R1 and R2, put the real ohms in your sketch, then trimCALonce. - The display resets or goes blank when the pack sags. If the Arduino runs from that same pack through a weak boost module, the OLED's 1024-byte update isn't the cause. The rail dipped. Power the Nano from USB while you debug the divider, and combine supplies only after the volts look right.
- Your "78%" label barely changes for an hour, then falls off a cliff. Lithium voltage is flat in the middle, so the meter is behaving. Delete the percentage, or label the bar "approx" once you've logged a real discharge on that exact pack.
Uno, Mega, ESP32 and NodeMCU don't share ADC math
Your OLED wiring changes with the board, and the voltmeter math has to change with it. Run an untouched Uno sketch on an ESP32 and you'll clip the pin and print nonsense.
| Board | OLED I2C | ADC to plan for | Divider note |
|---|---|---|---|
| Uno / Nano | SDA A4, SCL A5, OLED VCC 5V | 10-bit, 0-1023, VREF ≈ USB 5 V pin | Equal 100k is fine for you up to ~10 V packs |
| Mega 2560 | SDA 20, SCL 21 | Same 10-bit ADC as the Uno; A0 still works | Keep your resistor ratio and move only the I2C wires |
| ESP32 | GPIO21 / GPIO22, OLED on 3.3V | Typically 12-bit, 0-4095, about 3.3 V | Keep the pin under 3.3 V. I'd try R1=200k, R2=100k (÷3): 8.4 V → 2.8 V |
| ESP8266 NodeMCU | SDA D2, SCL D1, OLED on 3.3V | A0 is 0-1023 and the board accepts about 0-3.3 V | Don't wire a 4.2 V tap to A0. The bare ESP8266 ADC is 0-1 V; the NodeMCU divider is what makes 3.3 V legal |
On ESP32, an 8.40 V pack through equal 100k resistors gives 4.20 V at the pin. That's above 3.3 V. Use a heavier top resistor, check the node with your meter before you touch the GPIO, and change your sketch to adc * (3.3 / 4095.0) * DIV. Address 0x3C (sometimes 0x3D) is unchanged. A 128×64 frame is still 1024 bytes of buffer. That's comfortable on ESP32, and tight on an Uno if you also keep large logs.
If your OLED is an SH1106, the voltage code stays the same, but the constructor doesn't. A two-column shift on the "Pack voltage" label is the display driver, not the divider. I'd fix that before you chase CAL.
FAQ
Can I measure a 1S Li-ion without a divider?
On a 5 V Uno, a single cell stays under 4.2 V, so A0 can read it directly if pack− shares GND with the board. You should still fuse or protect the pack. And don't try this on an ESP32 or NodeMCU, since 4.2 V is above a 3.3 V pin. Use a divider there.
Why did my "percentage" stay at 80% until my robot died?
Resting voltage on lithium is flat through the middle of the discharge. A straight line from 8.4 V to 6.0 V isn't state of charge. Show volts. I'd add a percent only once you've recorded a discharge curve for your pack and your load.
The OLED meter works on USB and reads high when I power the Nano from a boost converter. What changed?
The ADC reference moved. Measure the 5 V (or 3.3 V) pin under that supply and put what you read in VREF. If a boost module gives 5.2 V and VREF is still 5.0, every pack reads about 4% high.
Does the SSD1306 refresh rate affect my voltage reading?
No. analogRead doesn't care about the 1024-byte frame. Updating the OLED every 300 ms is plenty for a pack meter. Calling display() in a tight loop only wastes bus time and makes the number harder to read. I2C is the right bus for this UI, and you don't need SPI unless the same board also pushes high frame-rate bitmaps.
Related guides you might like
- Stretch OLED battery life
- Bars & gauges UI
- Multi-page menu
- OLED wiring + Hello World
- Blank screen checklist
- GFX shapes for UI chrome
Add a battery icon animation later
Design a charging or empty glyph, export the drawBitmap code and drop it into your meter.
Open the free tool →Free tools: OLED Animation Maker for SSD1306 and SH1106 animations, Assemble Studio for name and logo animations, the Shadow Box Lamp Generator for 3D-printed shadow lamps, Text to STL for 3D-printed names and keychains, and 3MF to Web to show 3D prints on any website. New: the Image to Byte Array converter, the OLED Robot Eyes generator and the Lithophane Maker. See all tools.