# Checking Battery Life/Percentage

Is there any way to write a program that shows the Arduboy’s battery percentage using the Arduboy.h library? I am always wondering if my Arduboy is about to die.

I’m not sure if the Arduino Leonardo supports checking the battery.

The short version:

It’s possible to get a vague reading, but each unit has slightly different voltage values so there’s a certain amount of error.
The error could be reduced by testing the unit and calibrating the values.

This is the code I dig out from the original Arduboy library that does this.

``````#define ADC_TEMP (_BV(REFS0) | _BV(REFS1) | _BV(MUX2) | _BV(MUX1) | _BV(MUX0))
#define ADC_VOLTAGE (_BV(REFS0) | _BV(MUX4) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1))
{
// we also need MUX5 for temperature check
}
delay(2); // Wait for ADMUX setting to settle
return variable;
}

//somewhere else
if(arduboy.everyXFrames(30))/*check every second or so, to save ram*/
``````

This is no longer avilable in any Arduboy2 library (because this was thought to be obsolete)
If you use Arduboy(standard) library, you have the above code. But if you want to use Arduboy2 (for either the button function or the “newer” code itself), include the above ones.
Remember, I will not recommend use the rawADC every single frame (because it look quite resource hungry) so to save CPU usage, I decided to ru it every second (and a variable to store the voltage.) Basically feel free to do whatever you want to it.

• the 1.1 * 1024 * 1000 work this way:
expected difference between one voltage (real voltage input) and another (assume the temp bit one) was 1.1V, hence the 1.1. 1024 was 2^10(IDK either), and 1000 was for unit conversion. (so output will be 1234 for 1.234V)
I might make the 1126400 to a smaller number (so you don’t need the `L` behind it to make it “long” type (like 11264, but output will be 12 for 1.2V)
sounds good enough, though.

Some values:
4.2V(some) supposed to be full charged.
might go up to 4.3V(some) when full battery (and is plugged in)
3.7V(some) for like, 10% batt remaining
3.6V is 5%. (the voltage-remaining power ratio) was NOT a constant. but you just need to warn them at 3.7V.