Paraglider
Well-known member
Here is a simple but reliable circuit and code for lipo battery monitoring. The battery powers a Teensy 3.1 and is connected to the Vin pin. A resistive voltage devider is used to bring down the voltage to be measured below 3.3V. You can use 4.7k and 10k for the resistors. When using 1% resistors then the input voltage at Vin can be measured with an accuracy of +/-20-30mV. In order to prevent a too low discharge of the lipo battery this is precise enough.
Code:
/////////////////////////////////////////////////////////////////////
// Used for LiPo battery voltage measurement
// Battery powers Teensy 3.1 at Vin
// Resistor Divider with 10k and 4.7k
// 4.7k connected to Vbat
// 4.7k / 10k connected to measure Pin
// 10k connected to GND
/////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////
int vBat; //Battery voltage in mV
const int vBatMin = 3300; //Minimum battery voltage in mV
const int measurePin = 20; //Pin connected to resistor divider
unsigned long voltageTimer = 0;
/////////////////////////////////////////////////////////////////////
void setup() {
analogReference(EXTERNAL);
analogReadResolution(12);
analogReadAveraging(8);
}
void loop() {
//Measure Voltage in mV, 1200 is internal voltage reference, 1.47 is from resistor divider with 10k and 4.7k. When vBat is lower than vBatMin the device should be turned off
if (millis() - voltageTimer >= 60000){ // measure voltage every 60s
voltageTimer = millis();
vBat = 1195 * 1.47 * analogRead(measurePin) // analogRead(39);
Serial.println(vBat);
if (vBat <= vBatMin){
Serial.println("Turn Device Off");
// Do something useful here to protect your lipo battery
}
}
}