I'm trying to monitor the voltage of the battery that is powering the Arduino. I'm having to power my project with 4x1.5V AA cells which is cutting it fine, so keeping an eye on the voltage is going to be important. I'm powering two servos and and Xbee transmitter from the same pack. (This set up is working so far, using good fresh cells I've got enough power).
I have a voltage divider set up on my power rails and connected to A0 on the Arduino. The servos and Xbee draw their power direct from the power rails, not from the arduino's +5v pin (learnt from my previous mistake! http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1242315442). The arduino can either draw it's power from the rails, or from USB. Either way the Ground Pin is connected to the -Ve rail.
When the Arduino is powered from USB, it reports the voltage from the power rails accurately, but when I power the Arduino through it's external power jack from the power rails with everything else, the value of A0 stays at 1024. How can I report the voltage accurately?
Although I've read up on Voltage Dividers, and understand the principle, I've not found any suggestions what are good values to use for R1 and R2. Obviously I want to conserve as much power as I can so i would have thought that using high Ohm resisters would be best. As I mentioned, the battery is 6V and i'm using the divider to take this down to about 5V. R1 is 2K2 and R2 is 120K, are these good values? There was a fair bit of trial and error in arriving at this point.
If you have two resistors R1, R2 in series, R1 being connected to V and R2 connected to ground, the voltage at the midpoint is given by V * R2 / (R1 + R2).
Using your values, I get 6 * 120 / (122.2), which is 5.89.
Try using something like R1 = 120K, R2 = 22K, which would give 0.93 volts for 6 volts in.
Yes it will because the Arduino takes it's voltage reference from the power rails. If this is the USB then it is independent of what you are measuring. If you are using the power rails then it is the value produced at the output of the Arduino's voltage regulator that is taken as the reference.
For a true independent reading you need to derive a reference voltage and feed that into the Ain pin, then change the software so it looks at this pin.
Sorry yes it is the Vref. All voltages are taken with reference to that. By default this is the power rail of the processor chip. However, if you get a stable voltage, feed it in Vref and tell the processor to look at this instead voltages will be measured relative to this. This voltage has to be below the processor's power rails and greater than 1.2V (otherwise it will be noisy). There are special voltage reference chips you can get to do this. See the playground for more details.