# Battery Monitor

By ripper121, 6 years ago

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.

```//importe the Gamebuino library and the gb object
#include
#define ADC_PIN_OFFSET 165 //in mV (measured ADC from µC)-(Measured voltage on Pin with Multimeter)
#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();
}
int adcPinVolt = 0, batteryVolt = 0;
byte batteryPercent = 0;
void loop() {
// put your main code here, to run repeatedly:
while (!gb.update());
gb.display.clear();
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);
}```