This example demonstrates how to estimate the charge level of a lithium-ion battery using an Arduino UNO. The code calculates the battery charge level based on the voltage reading and estimates the remaining discharge time based on a given battery capacity in Watt-hours (Wh) and a discharge load.
/*
* Arduino UNO Lithium Battery Charge Indicator
* Measures battery voltage and estimates charge level and discharge time.
* Capacity parameter in Watt-hours (Wh) and discharge load based on 10% of Wh rating.
* © 2024 Copyright Peter I. Dunne, all rights reserved
* Prepared for educational use
* Released under the Mozilla Public License
*/
const int batteryPin = A0; // Battery voltage connected to A0
const float adcMaxVoltage = 5.0; // Maximum ADC voltage (5V)
const float maxADCValue = 1023.0; // Maximum ADC value (10-bit resolution)
// Battery parameters
const float batteryCapacityWh = 2.0; // Battery capacity in Watt-hours (example: 2.0 Wh)
const float dischargeLoadFactor = 0.20; // Discharge load is 10% of battery capacity
void setup() {
Serial.begin(115200); // Start serial communication
}
void loop() {
// Read battery voltage from ADC
float batteryVoltage = analogRead(batteryPin) * (adcMaxVoltage / maxADCValue);
// Convert voltage to charge level percentage (assuming linear relationship)
// Example calibration factors may be needed for accurate readings
float minVoltage = 3.0; // Minimum voltage of battery (fully discharged)
float maxVoltage = 4.2; // Maximum voltage of battery (fully charged)
float chargeLevel = ((batteryVoltage - minVoltage) / (maxVoltage - minVoltage)) * 100.0;
if (chargeLevel<0){
chargeLevel=0;
}
if (chargeLevel>100){
chargeLevel=100;
}
// Calculate discharge load in Watts
float dischargeLoadWatts = batteryCapacityWh * dischargeLoadFactor;
// Estimate remaining discharge time in hours
// Assuming a linear discharge rate
float totalHours = (batteryCapacityWh * (chargeLevel / 100.0)) / dischargeLoadWatts;
int hours = (int)totalHours;
int minutes = (totalHours - hours) * 60;
// Output readings
Serial.print("Battery Voltage: ");
Serial.print(batteryVoltage, 2);
Serial.print(" V, Charge Level: ");
Serial.print(chargeLevel, 2);
Serial.print(" %, Discharge Load: ");
Serial.print(dischargeLoadWatts, 2);
Serial.print(" W, Estimated Remaining Time: ");
// Print result in hh:mm format
if (hours < 10) Serial.print("0");
Serial.print(hours);
Serial.print(":");
if (minutes < 10) Serial.print("0");
Serial.println(minutes);
delay(1000); // Delay between readings
}