measuring battery voltage

I'd like to add a two color led to my project that would turn from green to red when the battery gets low. I'm more of a software guy with low confidence in my electrical side. So it seems I can accomplish this by connecting a voltage divider circuit to the 9v and the output of that divider into an analog pin. Then when the analog reading hits a certain level, I can change the led to go red. Does this sound like it will work? If so, what resistors would be good for getting a suitable range for the analog pin?

Don

Using the formulas here ...

... I would choose resistors such that:

  1. There is little current constantly flowing through the resistors from +9V to GND, so their sum should be on the order of 100K or higher. I don't know what the input impedance of the Arduino analog inputs are, but hopefully it is in the high megohms range, so it won't mess up the voltage divider math.
  2. Fresh 9V batteries are actually above 9V. Perhaps a simple divide-by-2 would work, such that 10V is converted to 5V, which yields the maximum 10-bit analog sample value of 1023.

Putting these together, two 47K resistors ought to do the trick. One resistor goes from +9V to analog pin, and the other goes from analog pin to GND. I notice my (homemade) Arduino tends to flake out when the supply falls below 6.5V, which means the analog pin will see 3.25V, which will show up as an analog reading of (3.25/5.00)*1023 = 665. Maybe as a safety margin, your red LED should come on when the value drops below 700 or 750?

Also, keep in mind that real resistors are never perfectly accurate. I highly recommend getting a digital multimeter if you don't already have one. (Even a cheap US$30 one will be one of the best hobby electronics investments you have ever made!) That way you can measure the real value of your resistors, plus you can measure the exact voltage your analog pin is seeing to double-check your math and wiring.

Hope this helps...

  • Don

Thank you! That works perfectly. I do have a multimeter so now I just need to find out what level my board starts to flake out and set it a little above that.

Don