Instructables

Arduino True Battery Capacity Tester (Li-Ion/NiMH/NiCD/Pb)

FeaturedContest Winner

Step 9: My SW (free for anyone to use)

// include the library code:
#include


LiquidCrystal lcd(12, 11, 5, 4, 3, 2);// initialize the library with the numbers of the interface pins
int sensorPin = 0;    // select the input pin for the potentiometer (pin 23)
int sensor2Pin = 2;    // select the input pin for the potentiometer (pin 23)
int ledPin = 13;      // select the pin for the LED
int SPKPin = 6;
int sensorValue = 0;  // variable to store the value coming from the sensor
int sensor2Value = 0;  // variable to store the value coming from the sensor
float LiMinThreshold = 2700; // Lithium Minimal Voltage for load removal
float LiMaxThreshold = 4200; // Lithium Max Voltage for load removal
float NmhMinThreshold = 950; // NMH Minimal Voltage for load removal
float NmhMaxThreshold = 1600; // NMH Max Voltage for load removal
float SelectedMinThreshold = 5000;
int i;
int BatVoltage = 5000;
int FetVoltage = 5000;
long TotalCurrent = 0;
boolean done = false;
unsigned long PrevMillis ;
unsigned long MillisPassed ;

void CL2(){
  lcd.setCursor(0, 1);// Second line first char
  lcd.print("                        ");
  lcd.setCursor(0, 1);// Second line first char


void setup() {
  Serial.begin(9600);// start serial port to send data during run to the PC
  pinMode(ledPin, OUTPUT);//activation led and enable for FET
  pinMode(SPKPin, OUTPUT);//activation led and enable for FET
  lcd.begin(24, 2);// set up the LCD's number of rows and columns:
  lcd.print("Bat PWR Tester[Active]");  // Print a message to the LCD.
  lcd.setCursor(0, 1);// Second line first char
  lcd.print("Detecting Bat Type..."); // print voltage value
  delay(2000);
  lcd.setCursor(0, 1);// Second line first char
  lcd.print("                        ");
  lcd.setCursor(0, 1);// Second line first char
  digitalWrite(ledPin, HIGH);   // set the LED on
  sensorValue = analogRead(sensorPin);   // read the value from the sensor:
  digitalWrite(ledPin, LOW);   // set the LED off
  // Detecting battery type
  BatVoltage = sensorValue*4.887;
  if (BatVoltage > 4500){
    lcd.print("Warning high-V! ");
    done = true;}
  else if (BatVoltage > LiMinThreshold){
    lcd.print("Type:Li-Ion Bat ");
    SelectedMinThreshold = LiMinThreshold;}
  else if (BatVoltage > NmhMinThreshold){
    lcd.print("Type:NiMH/Cd Bat ");
    SelectedMinThreshold = NmhMinThreshold;}
  else{
    lcd.print("Unknown Bat V<1 ");
    done = true;}
  lcd.print("V=");
  lcd.print(sensorValue*4.887); // print voltage value
  Serial.print("DT[ms]"); 
  Serial.print("\t"); 
  Serial.print("Bat[mV]"); 
  Serial.print("\t"); 
  Serial.print("Fet[mV]"); 
  Serial.println("");
  delay(3000);
  CL2();
  PrevMillis = millis();
}

void loop() {
    if (BatVoltage > SelectedMinThreshold && !done) {
      digitalWrite(ledPin, HIGH);   // set the LED on
      sensorValue = analogRead(sensorPin);   // read the value from the sensor:
      sensor2Value = analogRead(sensor2Pin);   // read the value from the FET:
      FetVoltage = (sensor2Value*4.887);
      BatVoltage = (sensorValue*4.887);
      CL2();
      lcd.print("V=");
      lcd.print(BatVoltage); // print voltage value
      lcd.print("mV");
      //lcd.print(FetVoltage); // print voltage value
      TotalCurrent=TotalCurrent+MillisPassed/1000*(BatVoltage-FetVoltage)/2.2/3.6;
      lcd.print(" I=");
      lcd.print(TotalCurrent/1000);    
      lcd.print("mAH         ");
      delay(1000);
      MillisPassed = millis()- PrevMillis;
      PrevMillis = millis();
      Serial.print(int(MillisPassed));
      Serial.print("\t");    // prints a tab
      Serial.print(BatVoltage);
      Serial.print("\t");    // prints a tab
      Serial.print(FetVoltage);
      Serial.println("");    // prints a tab
      CL2();
    }
    else
    {
      done=true;
      digitalWrite(ledPin, LOW);   // set the LED off - stop loading
      lcd.setCursor(0, 0);// First line first char
      lcd.print("Bat Power Tester [DONE] ");  // Print a message to the LCD.
      CL2();//clear line 2
      sensorValue = analogRead(sensorPin);   // read the value from the sensor:
      BatVoltage = (sensorValue*4.887);
      lcd.setCursor(0, 1);// Second line first char
      lcd.print("V=");
      lcd.print(BatVoltage); // print voltage value
      lcd.print("mV I=");
      lcd.print(TotalCurrent/1000);    
      lcd.print("mAH            ");
        for (int i=0; i<100 ; i++){
          digitalWrite(SPKPin, HIGH);
          delay(1);
          digitalWrite(SPKPin, LOW);
          delay(1);
        }
      delay(1000);
    }
}

 
Remove these adsRemove these ads by Signing Up
slvr00gt3 years ago
Here is my change to the code. Change adjusts the voltage when the test stops. It also prints out capacity over time to the serial port. and changes input pin numbers to what I end up using


$ diff my.cpp orig.cpp
7,8c8,9
< int ledPin = 8; // select the pin for the LED
< int SPKPin = 9;
---
> int ledPin = 13; // select the pin for the LED
> int SPKPin = 6;
18c19
< int FetVoltage = 0;
---
> int FetVoltage = 5000;
66,67d66
< Serial.print("\t");
< Serial.print("Capacity[mAH]");
75c74
< if ( (BatVoltage-FetVoltage) > SelectedMinThreshold && !done) {
---
> if (BatVoltage > SelectedMinThreshold && !done) {
86c85
< TotalCurrent=TotalCurrent+MillisPassed/1000*(BatVoltage-FetVoltage)/10/3.6;
---
> TotalCurrent=TotalCurrent+MillisPassed/1000*(BatVoltage-FetVoltage)/2.2/3.6;
98,99d96
< Serial.print("\t"); // prints a tab
< Serial.print(TotalCurrent/1000);