Low Battery Indicator

I'm looking to make a battery life indicator that I can use in conjunction with a micro-controller. I'm hoping that I can then read the current battery life remaining and increasingly put the micro-controller to sleep to preserve battery life.

-
Which microcontroller? –  Brian Carlton Apr 15 '10 at 14:35

To the primary question, as to how to measure the voltage of a battery, the most simple method is to use one of the ADC pins on the arduino.

If you're providing 5v from the battery (unlikely), then you can run the + straight to one of the analog in pins, and use analogRead() - each increment in value (from 0-1023) will represent 4.9mV. So multiply the read value by .0049 to get the volts read.

If you're running higher than 5v (more likely), you can use a voltage divider circuit (see: http://en.wikipedia.org/wiki/Voltage_divider) to bring the voltage you're sending to the analog input to <= 5v. If you're running 12V, you'll want to divide the voltage into roughly 1/3. Then, after multiplying the ADC value, multiply by 3 to get actual voltage.

Thus, if using 12V, and a 1/3rd voltage divider:

#define BAT_PIN 14

float volts = (float) val * (float) 0.0049 * (float) 3;

return(volts)
}


!c

-
I have two major things to add to this. First, most 12V batteries do not die at 0V, but instead at something quite close, at least within 5 V, of your full charge voltage. A common technique to get higher resolution measurments in the operating range is to step down the voltage with a zener diode. A resistor in series with a zener and you measure across the resistor. Second, most people do not realize you need to draw current from a battery to get a good idea of the power left. –  Kortuk Nov 24 '09 at 8:18
@Kortuk: Could you make two clarifications for my learning: (1) To get within the pin-accepted voltage range, are you suggesting that the zener-diode approach will give higher resolution than the voltage divider method? (2) Why did you make a particular mention here that "you need to draw current...", etc.? –  boardbite Aug 25 '12 at 12:10

tinker.it published a "secret" voltage level meter in the Atmega168/atmega328. With a little trick an Arduino is able to compare VCC against an internal precision 1.1v reference. The explanation and the code is here: http://code.google.com/p/tinkerit/wiki/SecretVoltmeter

-
excellent, I am running an atmega on batteries, and was not able to have a reliable vref value. thanks for posting this, it solved my issue like magic! –  radhoo Jul 9 at 18:29