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.
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
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
}