I'm doing a little project using a Lipoly charger and a 2500mAh 3.7V battery, both from Adafruit. I'm trying to display the percentage left on the battery. For that I'm using this little sketch :
long readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return result;
}
I saw on that page that the minimum voltage is 3.0V so I use that to get my values :
float volt = (float)readVcc()/1000;
float percent = ((volt-3)/0.7)*100;
When I display my values, it shows me that the percentage left is -4%. Should I worry or am I doing something wrong while calculating?
Answer
I think your calculation is OK.
Proof:
If your ADC conversion results is 1024 (full scale 10 bit) - when AVcc = reference
result = 1126400L / 1024 = 1100;
If your ADC conversion results is 512 (half scale 10 bit) - when AVcc = reference / 2
result = 1126400L / 512 = 2200;
Your percent result is -4% because voltage is below 3V.
percent = ((volt-3)/0.7)*100
-4 = ((volt-3)/0.7)*100
-4/100 = (volt-3)/0.7
-0.04*0.7 = volt - 3
-0.28 + 3 = volt
2.972 = volt
No comments:
Post a Comment