A simple app to measure the battery level (%) and voltage (mV).

You need to add 2 Resistors to the Backpack.

GND -[330kOhm]- A1 -[100kOhm]- VBAT

I protected the PCB with some tape, because if you touch the Voltage Divider or A1 it influences the readout.


Download: Battery.zip


//importe the Gamebuino library and the gb object
#include <Gamebuino-Meta.h>
#define ADC_PIN A1
#define ADC_PIN_OFFSET 165 //in mV (measured ADC from µC)-(Measured voltage on Pin with Multimeter)
#define ADC_PIN_MAXVOLT 3201
#define BATTERY_FULL 4190 //in mV measured when Battery Full on VBAT
#define BATTERY_EMPTY 3000 //in mV on LiPo dont go Lower!

/*
   Schematic:
   GND -[330kOhm]- A1 -[100kOhm]- VBAT
   When the Battery Voltage is 4.200V the Voltage on A1 is 3.223V
*/
void setup() {
  // put your setup code here, to run once:
  gb.begin();
  pinMode(ADC_PIN, INPUT);
}

int adcPinVolt = 0, batteryVolt = 0;
byte batteryPercent = 0;
void loop() {
  // put your main code here, to run repeatedly:
  while (!gb.update());
  gb.display.clear();

  adcPinVolt = ((3300 / 1023) * analogRead(ADC_PIN)) + ADC_PIN_OFFSET;
  batteryVolt = map(adcPinVolt, 0, ADC_PIN_MAXVOLT, 0, BATTERY_FULL);
  batteryPercent = map(batteryVolt, BATTERY_EMPTY, BATTERY_FULL, 0, 100);
  if (batteryPercent > 100)
    batteryPercent = 100;

  gb.display.drawRect(10, 20, 60, 34);
  gb.display.drawLine(70, 31, 70, 42);
  gb.display.drawLine(71, 32, 71, 41);
  if (batteryPercent >= 60)
    gb.display.setColor(GREEN);
  else if (batteryPercent >= 30 && batteryPercent <= 59)
    gb.display.setColor(YELLOW);
  else
    gb.display.setColor(RED);
  gb.display.fillRect(11, 21, map(batteryPercent, 0 , 100, 0, 58), 32);

  gb.display.setColor(WHITE);
  gb.display.println("Battery");
  gb.display.setCursor(30, 30);
  gb.display.print(batteryVolt);
  gb.display.println("mV");
  gb.display.setCursor(32, 36);
  gb.display.print(batteryPercent);
  gb.display.println("%");
  delay(500);
}

Last comments

Aurélien Rodot

NEW 4 months ago

ripper121 ripper121

Also the battery has a protection circuit which will turn off when the voltage is to low.

...theoretically. Experience proved otherwise ;)

ripper121

NEW 4 months ago

I dont think that it was a problem of the current draw of 8.6uA, because that will need years, the self discharge of the Battery is higher.

Also the battery has a protection circuit which will turn off when the voltage is to low.

Aurélien Rodot

4 months ago

Also the battery has a protection circuit which will turn off when the voltage is to low.

...theoretically. Experience proved otherwise ;)

DFX2KX

NEW 4 months ago

the self-dischage issue can be a serious issue if you don't know about it.

On that note, let me go plug my classic in before it burns out it's new battery... That explains a lot, actually.