Hi,
I am developing a little autonomous robot project (with Arduino Nano, Atmega 328), powered by a single cell lipo battery, and I want to periodically measure the voltage of that battery, and once it drops below 3.5V, shut everything down to prevent damage to that battery (I've got the shut-down part covered).
I have one analog pin remaining for this task, but I was wondering if this measurement could be taken by measuring the voltage that comes into the Vin pin, using internal 1.1V reference. I am using the following code:
float readVcc()
{
const float V_BAND_GAP = 1.1; // typical
ADMUX = _BV(REFS0) // ref = Vcc
| 14; // channel 14 is the bandgap reference
ADCSRA |= _BV(ADSC); // start conversion
loop_until_bit_is_clear(ADCSRA, ADSC); // wait until complete
return V_BAND_GAP * 1024 / ADC;
}
This code sort of works, but unfortunately, the readings are off by a certain amount. I am powering Arduino from a custom-made PSU, and displaying the voltage readings on an LCD screen, so no USB cable to interfere with anything.
With 5V source coming into the Vin, this function returns 4.15V. At 4V, it displays 3.12V. Of course, this isn't the end of the world, I can still test what this function thinks the voltage is when it is actually 3.5V, and use that for shut-down reference, but I don't like not knowing what is causing such a large discrepancy...
To correct the measurements, I would have to set reference voltage to 1.3V... Which can't be right. And unfortunately, that only fixes the voltage reading at 5V - at other voltages, numbers don't match again, so it's clearly a scaling issue, not just an offset. Can anyone tell why these numbers don't match?
I am also considering using a boost converter to bring 4.2V from the lipo to 6V in order to power motor driver, so I could power Arduino from these 6V as well, and take measurements of the lipo voltage by connecting to the positive rail between the lipo and the boost converter - that way, the converter will keep the 6V stable even when lipo gradually discharges, and I'll be able to use that 6V as a reference to measure the lipo voltage with a simple analogRead. Is this assumption correct?
Or are there any other better ways of measuring the voltage of my battery?
Thanks in advance!