# Calculating battery percentage using an Arduino

I'm doing a little project using a Lipoly charger and a 2500mAh 3.7V battery, both from Adafruit. I'm trying to display the percentage left on the battery. For that I'm using this little sketch :

long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}


I saw on that page that the minimum voltage is 3.0V so I use that to get my values :

  float volt = (float)readVcc()/1000;
float percent = ((volt-3)/0.7)*100;


When I display my values, it shows me that the percentage left is -4%. Should I worry or am I doing something wrong while calculating?

• If your calculation ((volt-3)/0.7)*100 gives you negative number - that means your "volt" variable is less than 3V. May 12, 2014 at 11:41
• Wait wait... You measuring constant 1.1V reference connected to ADC input and calculating Avcc? ADC backwards, interesting! May 12, 2014 at 11:53
• Kamil, could you explain what you just said ? Because the function readVCC isn't from me, I got it on this page : code.google.com/p/tinkerit/wiki/SecretVoltmeter May 12, 2014 at 12:07
• I explained in answer. May 12, 2014 at 12:10

I think your calculation is OK.

Proof:

If your ADC conversion results is 1024 (full scale 10 bit) - when AVcc = reference

result = 1126400L / 1024 = 1100;


If your ADC conversion results is 512 (half scale 10 bit) - when AVcc = reference / 2

result = 1126400L / 512 = 2200;


Your percent result is -4% because voltage is below 3V.

percent = ((volt-3)/0.7)*100
-4 = ((volt-3)/0.7)*100
-4/100 = (volt-3)/0.7
-0.04*0.7 = volt - 3
-0.28 + 3 = volt
2.972 = volt

• And is it safe to have a voltage below 3V with a 3.7V battery ? May 12, 2014 at 12:15
• No. It's not good for Li-Ion battery. It will loose capacity pretty fast. You should disconnect battery when it happends or reduce current to microamperes (microcontroller in sleep mode, leds off, everything off) until battery will be recharged. May 12, 2014 at 12:19
• Ok thanks :). Do you have any idea how to shut off the Arduino when battery is below 3V ? I saw that there is a sleeping mode in the arduino, should that be enough ? May 12, 2014 at 12:22
• It depends on what you have connected to that battery. And how long this circuit will stay without charging. If you leave it in sleep mode at 3V for few weeks - it will kill battery anyway. It's really hard to tell. Im not arduino guy, I don't know how sleeping mode does. If it turns on Power-down Mode: (0.1µA in Atmega328P) - it should be enough, but it's hard to tell without schematic. If you have I2C with pullups - they can kill battery no matter if you have power-down mode on. May 12, 2014 at 12:26
• All right, thanks for all the answers ! I'll check that out ! May 12, 2014 at 12:29