Instantly share code, notes, and snippets.

Embed
What would you like to do?
/*
* Battery Capacity Checker
*
* Uses 1 Ohm power resister as shunt - Load can be any suitable resister or lamp
*
* YouTube Video: https://www.youtube.com/embed/qtws6VSIoYk
*
* Based on adamwelch.co.uk/2016/01/lithium-ion-18650-battery-capacity-checker/
*
* LCD bits removed. Logging to serial only.
*
* Crudiely modified to deal with 4 batteries
*/
#define highPin1 A0
#define lowPin1 A1
#define gatePin1 11
#define highPin2 A2
#define lowPin2 A3
#define gatePin2 7
#define highPin3 A6
#define lowPin3 A7
#define gatePin3 3
#define highPin4 A8
#define lowPin4 A9
#define gatePin4 2
boolean finished1 = false;
boolean finished2 = false;
boolean finished3 = false;
boolean finished4 = false;
float mAh1 = 0.0;
float mAh2 = 0.0;
float mAh3 = 0.0;
float mAh4 = 0.0;
float battVolt1 = 0.0;
float battVolt2 = 0.0;
float battVolt3 = 0.0;
float battVolt4 = 0.0;
float shuntVolt1 = 0.0;
float shuntVolt2 = 0.0;
float shuntVolt3 = 0.0;
float shuntVolt4 = 0.0;
float shuntRes = 0.22; // In Ohms - Shunt resistor resistance
float voltRef = 4.90; // Reference voltage (probe your 5V pin)
float current = 0.0;
float battLow = 3.0;
int printStart = 0;
int interval = 5000; //Interval (ms) between measurements
unsigned long previousMillis = 0;
unsigned long millisPassed = 0;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial1.begin(115200);
Serial1.println("Battery Capacity Checker v1.1");
Serial1.println("battVolt current mAh");
digitalWrite(gatePin1, LOW);
digitalWrite(gatePin2, LOW);
digitalWrite(gatePin3, LOW);
digitalWrite(gatePin4, LOW);
}
void loop() {
battVolt1 = analogRead(highPin1) * voltRef / 1024.0;
battVolt2 = analogRead(highPin2) * voltRef / 1024.0;
battVolt3 = analogRead(highPin3) * voltRef / 1024.0;
battVolt4 = analogRead(highPin4) * voltRef / 1024.0;
shuntVolt1 = analogRead(lowPin1) * voltRef / 1024.0;
shuntVolt2 = analogRead(lowPin2) * voltRef / 1024.0;
shuntVolt3 = analogRead(lowPin3) * voltRef / 1024.0;
shuntVolt4 = analogRead(lowPin4) * voltRef / 1024.0;
//BATTERY 1
if(battVolt1 >= battLow && finished1 == false)
{
digitalWrite(gatePin1, HIGH);
millisPassed = millis() - previousMillis;
current = (battVolt1 - shuntVolt1) / shuntRes;
mAh1 = mAh1 + (current * 1000.0) * (millisPassed / 3600000.0);
previousMillis = millis();
Serial1.print("B1");
Serial1.print("\t");
Serial1.print(battVolt1);
Serial1.print("\t");
Serial1.print(current);
Serial1.print("\t");
Serial1.println(mAh1);
delay(interval);
}
if(battVolt1 < battLow)
{
digitalWrite(gatePin1, LOW);
finished1 = true;
delay(interval);
}
// BATTERY 2
if(battVolt2 >= battLow && finished2 == false)
{
digitalWrite(gatePin2, HIGH);
millisPassed = millis() - previousMillis;
current = (battVolt2 - shuntVolt2) / shuntRes;
mAh2 = mAh2 + (current * 1000.0) * (millisPassed / 3600000.0);
previousMillis = millis();
Serial1.print("B2");
Serial1.print("\t");
Serial1.print(battVolt2);
Serial1.print("\t");
Serial1.print(current);
Serial1.print("\t");
Serial1.println(mAh2);
delay(interval);
}
if(battVolt2 < battLow)
{
digitalWrite(gatePin2, LOW);
finished2 = true;
delay(interval);
}
// BATTERY 3
if(battVolt3 >= battLow && finished3 == false)
{
digitalWrite(gatePin3, HIGH);
millisPassed = millis() - previousMillis;
current = (battVolt3 - shuntVolt3) / shuntRes;
mAh3 = mAh3 + (current * 1000.0) * (millisPassed / 3600000.0);
previousMillis = millis();
Serial1.print("B3");
Serial1.print("\t");
Serial1.print(battVolt3);
Serial1.print("\t");
Serial1.print(current);
Serial1.print("\t");
Serial1.println(mAh3);
delay(interval);
}
if(battVolt3 < battLow)
{
digitalWrite(gatePin3, LOW);
finished3 = true;
delay(interval);
}
// BATTERY 4
if(battVolt4 >= battLow && finished4 == false)
{
digitalWrite(gatePin4, HIGH);
millisPassed = millis() - previousMillis;
current = (battVolt4 - shuntVolt4) / shuntRes;
mAh4 = mAh4 + (current * 1000.0) * (millisPassed / 3600000.0);
previousMillis = millis();
Serial1.print("B4");
Serial1.print("\t");
Serial1.print(battVolt4);
Serial1.print("\t");
Serial1.print(current);
Serial1.print("\t");
Serial1.println(mAh4);
delay(interval);
}
if(battVolt4 < battLow)
{
digitalWrite(gatePin4, LOW);
finished4 = true;
delay(interval);
}
// All batteries have finished discharging.
if(finished1 == true && finished2 == true && finished3 == true && finished4 == true)
{
digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment