Monitor Battery Level

hi i am using arduino one and want to see the charge level of the battery.

I'm using an 8v battery and one voltage divider:

vi = 8v,
R1 = 1000
R2 = 1000

I am using this code but not work for me, does not yield the correct values.
any ideas?

const int referenceVolts = 5; // the default reference on a 5-volt board
const int R1 = 1000; // value for a maximum voltage of 20 volts
const int R2 = 1000;
// determine by voltage divider resistors, see text
const int resistorFactor = 255 / (R2/(R1 + R2));
const int batteryPin = A0; // +V from battery is connected to analog pin 0

void setup()
{
Serial.begin(115200);
}

void loop()
{
int val = analogRead(batteryPin); // read the value from the sensor
float volts =  (val / resistorFactor) * referenceVolts ; // calculate the ratio
Serial.println(volts); // print the value in volts
delay(500);
}

http://arduino.cc/en/Reference/AnalogRead

const int referenceVolts = 5; // the default reference on a 5-volt board
should be
const int referenceVolts = 5.0; // the default reference on a 5-volt board

Need a decimal in the equation.

Try:
int val = analogRead(batteryPin); // read the value from the sensor
float volts = (val*2.0 / 1023.0) * 5.0 ; // calculate the ratio
Serial.println(volts); // print the value in volts
delay(500);

not work for me.
so I have connected to the Arduino, please help

Works fine here on UNO.

Put .1uf cap as close as possible on the input to ground on the regulator.
What colors are on your resistors?

LarryD:
Works fine here on UNO.

Put .1uf cap as close as possible on the input to ground on the regulator.
What colors are on your resistors?

1.5 K,

BROWN, GREEN , RED, GOLD

Hi, do you have a meter to measure voltage, ie a DMM.
Check what the voltage is from gnd to A0.

Tom..... :slight_smile: