Chances are this has been worked out before, I don't know, I didn't really look. But here is the process that was used to determine how to calculate the battery voltage and current. (its like circuits 101 all over again).
- Vbg = Voltage of internal bandgap reference on the AVR = 1.1V
- Aref = ADC value returned by reading Vbg
- Vbat = Voltage of battery
- Abat = ADC value returned by reading Vbat
- Vsense = Voltage of the negative side of the current sense resistor
- Asense = ADC value returned by reading the negative side of the current sense resistor
- Rsense = the value of the resistor used to sense current
- Iread = current read by the micro-controller
First assume the ratio of the 1.1V reference to the battery voltage is equal to the ADC reading of the reference to the ADC reading of the battery voltage. (this is mostly true)
And that's about it for the voltage calculation, onto current. First we find the voltage at the sense resistor using the same assumption as before that the ratio of the voltage is equal to the ratio of the ADC readings:
Using Ohm's law the current can be found by dividing the change in voltage by the resistance:
Now substituting in the above equations and simplifying yields Iread:
That's everything, since everything's now in terms of ADC values its fairly straight forward to simply get the values in software and calculate both the voltage of and current draw from the battery. All the units check out and a few sample calculations I ran seem to show that these equations work well enough (accuracy of the ADC throws current readings off by a few percentage points). If I overlooked something please let me know.