Sunday, January 28, 2018

Arduino LiPo Capacity Tester

After obtaining a lot of Chinese 18650 batteries labeled "5000 mAh" I decided to find out just how much capacity they DO have.
This tester features an Ardunio Nano, a Nokia 5110 display, a rotary encoder for operation, and a FET current source using a high -power mosFET (IRLZ44).



The schematic:

The current source consists of the IRLZ44 mosFET, and the 10 turn pot which can be any value between 50K and 500K. I used a 500K I had lying around. The FET acts as a variable power resistor to load the test battery.

My Nokia display was a 5 volt version... be sure to check which version you have and power it accordingly... many out there are 3v3 versions.

I tried many different encoder libraries with somewhat dismal success until I found SimonM83 excellent Instructables article. That code is awesome its very 'light' (no libraries and interrupt driven) and is used in this sketch. Thanks Instructables and SimonM83!

Using the Vin on the Nano to power insures accurate ADC measurements since the reference used is the 5 volt rail. Using the USB to power will cause inaccuracies unless you compensate in the sketch.

Download INO file.

Here is the sketch:
/*
* Battery Capacity Calculator
* Uses Nokia 5110 Display and encoder for navigation
* ©2018 Tim Stoddard
*/
/*******Interrupt-based Rotary Encoder parts*******
by Simon Merrett, based on insight from Oleg Mazurov, Nick Gammon, rt, Steve Spence
*/
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_PCD8544.h>
Adafruit_PCD8544 display =  Adafruit_PCD8544(6, 5, 4);
#define pinA 2 // Encoder (CLK): Our first hardware interrupt pin is digital pin 2
#define pinB 3 // Encoder (DT): Our second hardware interrupt pin is digital pin 3
#define pinC 7 // Encoder switch
#define gateFET 10
#define battPin A0
#define sensePin A1

int printStart = 0;
int eValue = 0;

float mAh = 0.0;
float senseR = 1.0;  // In Ohms - Shunt resistor resistance
float voltRef = 5.00; // Reference voltage for ADCs
float current = 0.0;
float battV = 0.0;
float senseV = 0.0;
float battLow = 3;
unsigned long previousMillis = 0;
unsigned long millisPassed = 0;
//Encoder variables
volatile byte aFlag = 0; // let's us know when we're expecting a rising edge on pinA to signal that the encoder has arrived at a detent
volatile byte bFlag = 0; // let's us know when we're expecting a rising edge on pinB to signal that the encoder has arrived at a detent (opposite direction to when aFlag is set)
int encoderPos = 0; //this variable stores our current value of encoder position.
int oldEncPos = 0; //stores the last encoder position value
volatile byte reading = 0; //somewhere to store the direct values we read from our interrupt pins before checking to see if we have moved a whole detent

void setup() {
  pinMode(pinA, INPUT_PULLUP); // set pinA as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  pinMode(pinB, INPUT_PULLUP); // set pinB as an input, pulled HIGH to the logic voltage (5V or 3.3V for most cases)
  attachInterrupt(0,PinA,RISING); // set an interrupt on PinA, looking for a rising edge signal and executing the "PinA" Interrupt Service Routine (below)
  attachInterrupt(1,PinB,RISING); // set an interrupt on PinB, looking for a rising edge signal and executing the "PinB" Interrupt Service Routine (below)
  pinMode(pinC,INPUT);

  display.begin();
  display.clearDisplay();   // clears the screen and buffer
  display.display();// Update display

  pinMode(gateFET, OUTPUT);
  digitalWrite(gateFET, LOW);

  display.setContrast(50);
  display.clearDisplay();
  display.println("BattCapacity");
  display.println("v2.0");
  display.println("");
  display.print("Tim Stoddard");
//  display.drawRect(0, 0, 84, 48, BLACK);// start x,y then width,height
//  display.fillRect(0, 30, 16, 4, BLACK);
  display.display();
  while (digitalRead(pinC)){continue;}// wait for press and release of switch
  while (!digitalRead(pinC)){continue;}
}
void PinA(){
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0xC; // read all eight pin values then strip away all but pinA and pinB's values
  if(reading == B00001100 && aFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos --; //decrement the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00000100) bFlag = 1; //signal that we're expecting pinB to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

void PinB(){
  cli(); //stop interrupts happening before we read pin values
  reading = PIND & 0xC; //read all eight pin values then strip away all but pinA and pinB's values
  if (reading == B00001100 && bFlag) { //check that we have both pins at detent (HIGH) and that we are expecting detent on this pin's rising edge
    encoderPos ++; //increment the encoder's position count
    bFlag = 0; //reset flags for the next turn
    aFlag = 0; //reset flags for the next turn
  }
  else if (reading == B00001000) aFlag = 1; //signal that we're expecting pinA to signal the transition to detent from free rotation
  sei(); //restart interrupts
}

int getEncoder(int s,int e){
  encoderPos=s;
  display.setTextSize(2);
  display.setCursor(37,32);
  display.setTextColor(BLACK);
  display.print(encoderPos);
  display.display();
  while(digitalRead(pinC)){
    if(oldEncPos != encoderPos) {
      display.setCursor(37, 32);
      display.setTextColor(WHITE);
      display.print(oldEncPos);
      if (encoderPos > e){encoderPos=s;}
      if (encoderPos < s){encoderPos=e;}
      display.setCursor(37,32);
      display.setTextColor(BLACK);
      display.print(encoderPos);
      oldEncPos = encoderPos;
      display.display();
    }
  }
  display.setTextSize(1);
  while (!digitalRead(pinC)){continue;}
  return(encoderPos);
}
void configuration(void){
  display.clearDisplay();
  display.println("BattLow");
  display.print("Current:");
  display.println(battLow);
  display.print("Set 10s");
  display.display();
  battLow=getEncoder(0,3);
  display.clearDisplay();
  display.println("BattLow");
  display.print("Current:");
  display.println(battLow);
  display.print("Set decimal");
  display.display();
  battLow=battLow + float(getEncoder(0,99))/100;
  display.clearDisplay();
  display.println("BattLow");
  display.print("New:");
  display.println(battLow);
  display.print("Click to Exit");
  display.display();
  while (digitalRead(pinC)){continue;}// wait for press and release of switch
  while (!digitalRead(pinC)){continue;}
  }
void completed(void){
  digitalWrite(gateFET, LOW);// turn off FET
  display.clearDisplay();
  display.setCursor(12, 0);
  display.setTextColor(WHITE,BLACK);
  display.println("COMPLETED");
  display.setTextColor(BLACK);
  display.print("Voltage: ");
  display.print(battV);
  display.print("v");
  display.drawLine(0, 20, 84, 20, BLACK);
  display.setCursor(0, 24);
  display.println("Capacity:");
  display.print(mAh);
  display.println("mAh");
  display.display();
  while(digitalRead(pinC)){continue;}
}
void discharge(){
 while(digitalRead(pinC)){
    battV = analogRead(battPin) * voltRef / 1024.0;
    senseV = analogRead(sensePin) * voltRef / 1024.0;
    if(battV < battLow){completed();}
    if(battV >= battLow)
    {
      digitalWrite(gateFET, HIGH);
      millisPassed = millis() - previousMillis;
      current = (senseV / senseR)*1000;
      mAh = mAh + (current) * (millisPassed / 3600000.0);
      previousMillis = millis();

      display.clearDisplay();
      display.println("Discharging");
      display.print("V: ");
      display.println(battV);
      display.print("I: ");
      display.print(current);
      display.println("mA");
      display.println("Capacity:");
      display.print(mAh);
      display.println("mAh");
      display.display();
    }
  }
  digitalWrite(gateFET, LOW);// turn off FET
  delay(500);
}
void loop() {
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(BLACK);
  display.setCursor(30, 0);
  display.println("MENU");
  display.drawLine(0, 8, 84, 8, BLACK);
  display.setCursor(0, 12);
  display.print("1) BattL=");
  display.println(battLow);
  display.println("2) Start");
  display.display();
  eValue = getEncoder(0,2);
//  encoderPos=0;
  switch(eValue){
    case 1:
      configuration();
      break;
    case 2:
      discharge();
      break;
    default:
      break;
  }
  eValue = 0;
  while (!digitalRead(pinC)){continue;}// wait for release of switch


No comments:

Post a Comment