I'm combining an op amp-based constant current load with an arduino for measuring the capacity of 18650 cells. I've got the CC load working on its own, but I'm running into issues measuring the battery voltage. Attached is the whole circuit. Here's the code:
unsigned int reading;
unsigned long testTime;
float VBatt, Vcc;
bool doneMeasuring = false;
long readVcc() {
long result;
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2);
ADCSRA |= _BV(ADSC);
while (bit_is_set(ADCSRA, ADSC));
result = ADCL;
result |= ADCH << 8;
result = 1121280L / result;
return result;
}
void setup() {
pinMode(8, INPUT);
Serial.begin(9600);
Serial.println("Starting test...");
testTime = millis();
}
void loop() {
if (!doneMeasuring) {
reading = analogRead(A3);
Vcc = readVcc() / 1000.0;
VBatt = reading * Vcc / 1024.0;
Serial.print("Battery voltage: ");
Serial.println(VBatt);
if (VBatt < 2.7) {
testTime = millis() - testTime;
doneMeasuring = true;
pinMode(8, OUTPUT); //
digitalWrite(8, LOW); // Pull the non-inverting input of the op amp low to disable the output...maybe? (nope)
float totalHours = testTime / 1000.0 / 60.0 / 60.0; // hours
float mAh = totalHours * 200; // Test load is 200 mA constant
Serial.print("Test finished, took ");
Serial.print(totalHours);
Serial.println(" hours");
Serial.print("Capacity: ");
Serial.print(mAh);
Serial.println(" mAh");
}
delay(10000);
}
}
The battery voltage is consistently reading low. I measure it with my DMM and I get 3.84 while the serial monitor says its 3.61 (give or take some error, it seems to be between -.15 and -.2 volts.)