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. :smiley:

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.


See also:

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))
//uint16_t battery; I added this
uint16_t rawADC(uint8_t adc_bits)
{
  power_adc_enable(); // ADC on
  ADMUX = adc_bits;
  // we also need MUX5 for temperature check
  if (adc_bits == ADC_TEMP) {
    ADCSRB = _BV(MUX5);
  }
  delay(2); // Wait for ADMUX setting to settle
  ADCSRA |= _BV(ADSC); // Start conversion
  while (bit_is_set(ADCSRA, ADSC)); // measuring
  const uint16_t variable = ADC;
  power_adc_disable();
  return variable;
}

//somewhere else
if(arduboy.everyXFrames(30))/*check every second or so, to save ram*/
battery = 1126400L/*1.1 * 1024 * 1000*// rawADC(ADC_VOLTAGE);
//arduboy.print(battery);I added this

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.

  • Just don’t ask me about others.
1 Like

It was this “1.1” that will change slightly from board to board, and was supposed to be around 1 ~ 1.2 (according to some name-forgotten specifecations)
But ignoring the last two digit of the output, it was OK enough.
Enough to stop evening the pickiest person (me) from complaining not knowing the voltage.