Low Battery Indicator

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: https://provideyourown.com/2012/secret-arduino-voltmeter-measure-battery-voltage/


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 read_batt_volts(void) {

  int val = analogRead(BAT_PIN);

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

  return(volts)
}

!c


shutterdrone's suggestion is very good, but is missing a detail, as it implies that the analog voltage reference is the same as the voltage being measured (aside from assuming that an Arduino is being used, which is not explicitly stated).

You'll need an ADC, whether it's integral to your uC or an external component. The ADC needs an analog voltage reference (ARef) to compare the input with. You don't want the ARef to be the same as the voltage you're sensing because then the ADC will always see the input and reference voltages as being equal, so the analog value for the sensed voltage will always be the maximum. What you need is a very accurate analog voltage reference that won't change as the power supply voltage changes. The ADC will compare that reference voltage to the battery voltage; you can use a voltage divider to reduce the battery voltage being sensed into the range of the ARef.

The Arduino (Atmega8/168/328/etc.) has a built-in ADC and a built-in 1.1V analog reference, so the only external components you'd need are resistors for the voltage divider. I think you would also want to use very high values for the resistors to reduce the current drain on the battery.