Hi. Can any point me to any previous discussions or give me some pointers on how I can get an arduino to read the voltage of the battery that is powering it? As I understand it a voltage measuring circuit would test the voltage at a pin against the supply or reference voltage. As I don't have a separate supply or reference voltage to compare against, how could i tell if the battery running the Arduino is getting low? It would also be useful to measure the current consumption at the power jumper, which is a variation on the same problem.
Just wire two series connected resistors of say 1k ohm each. Wire one end of the series string to ground and the other end of the string to the battery input, Vin. Now wire the junction of the two resistors to a Arduino analog input pin. The value read by the arduino will be battery voltage / 2. You can rescale that in software. This will work for battery voltages up to 10vdc. If higher voltage batteries are used then one of the series resistor will have to be changed to say 5K ohm and result will be that the Arduino will read the battery voltage = volts/3. In reality the best construction method would be to use a 3 pin 10 turn pot for the voltage divider. Wire the two fixed terminals to the ground and battery and adjust the pot so the the analog voltage reads 4 vdc, then the software can read and watch the falling battery voltage as it discharges.
One thing to consider is that the above mentioned voltage divider will constantly create a drain on the battery the whole while it's connected. Not a lot of current will go through 6k but some will (2ma at 12v). 2ma is probably nothing to worry about.
The math hurts my head today, got a cold. Best bet is to just use a ten turn 10K trimmer pot and adjust it to say 4 volts out and use that to monitor where the battery is in it's discharge curve.
Which brings up the topic of monitoring battery voltage Vs type of battery being used. Lead acid, Li-poly and alkaline all have a somewhat linear discharge voltage curve Vs remaining charge. Where as Nicad and Nimh have a pretty flat line discharge curve until very near the end of their charge. I guess it depends on what the sketch is going to do with the battery voltage info.
@AdderD - Couldn't the mA be further reduced by using higher resistor values? If I use 50K and 10K resistors, only .2 mA would flow through the resistors.
The only problem I could see occuring, would be the current being too small for the Arduino to read
Exactly right. You can reduce the waste current with higher resistance but past a certain point you will limit things too much to get a good reading. I don't know off the top of my head what the highest resistance is that you can get away with. I'm sure one could figure it out by reading the ATMEGA168 spec sheet. But 1k and 5k should be safe. 2ma is only an amp hour every 500 hours. So a 1AH battery with nothing but the divider could concievably last almost 21 days. I'd bet that such a small draw is inconsequential for most applications.