Measuring battery voltage using M5stack
I often need to test batterys voltage before throwing them away. I am using a multimeter to test, but it is cumbersome.
I thought it is not too difficult to make battery voltage measurement tool using a microcontroller, so I did it as a little electronics project.
Microcontroller I used - M5stack Core
I used M5stack Core. It is an ESP32-based microcontroller and it is handy that it comes with a LCD screen. It has two AD converter pins (ADCs).
I’ve had a M5stack Core at hand, but it is a bit old and is discontinued (EOL). I would use ESP32 Basic Core IoT Development Kit V2.6($39.90) or M5Stack Core2 ($46.90) if I am buying a new one.
Circuit
I’ve created a simple circuit to connect a battery. I just put a 1k ohm resister between a AA battery holder terminals on a breadboard.
The positive terminal is connected to pin 35 of M5Stack, and the negative terminal is connected to GND.
Program
I’ve written a simple program to read ADC reading and display it on M5Stack’s LCD.
#include <M5Stack.h>
int cnt = 0;
void setup(){
M5.begin();
M5.Power.begin();
M5.Lcd.setTextSize(3);
pinMode(35, INPUT);
}
float voltage(int reading) {
return (float)reading*0.000841 + 0.131;
}
void loop() {
int value;
value = analogRead(35);
M5.Lcd.setCursor(0,0);
M5.Lcd.printf("%d:value:%4d\n", cnt++, value);
M5.Lcd.printf("voltage:%02.3fV\n", voltage(value));
delay(1000);
}
Adjusting ADC readings
M5Stack’s ADC is the one built-in to ESP32. It is measuring 0-3.3V with value 0 to 4095.
There are articles like ESP32 ADC – Read Analog Values with Arduino IDE saying the ADC reading is not linear, and a graph like this.
However, even this non-linear values were little off (0.1-0.2V) for me.
I had a lot of batteries around, and I’ve collected a bunch of measurements. ADC readings and measurements by voltage reading by Fluke 116 multimeter.
I’ve collected 43 readings ranging from 1.0 to 1.6V (and a few 0.1V ones).
Using least squares and I got V = 0.000841 * reading + 0.131. It never gives 0V, but it is okay for my battery measurement purpose.
Result
With a new formula, it is usable for my purpose.
A reading of an Amazon basic rechargable battery with my Fluke’s reading 1.337V. The M5Stack’s ADC reading is 1430(decimal) and the derived voltage is 1.334V (-0.03V).