Measuring Voltage With An Ardunio
I’ve been thinking about a way to monitor the battery supply voltage of my weather station remote node, as I was thinking of buying some LiFePO4 or LiPo batteries which don’t cope well with over-discharging.
Its all a bit moot as in the end I decided to stick with regular NiMH/NiCd rechargeable batteries and get some of Pololu’s new miniature boost regulators in 3.3v and 5.0v varieties. They’re pretty nifty as not only can they make 3.3/5.0v from as little as 0.5v, but they can also automatically regulate down from 5.5v 1.2A, so not only can you run off of two AA batteries, you could also run off of four, all from about 1cm2!
Anyway, back to measuring voltages. I found that the AVR chips have an internal voltmeter that compares its 1.1v analog reference with the AVcc:
|
|
The problem there is that its measuring the internal voltage across the chip (which could be the output from a regulator) not the supply voltage to the circuit. For that we need to make a voltage divider from a couple of resistors, to lower the maximum probably voltage to under 1.1v so we can compare it again to Aref. The resistors I chose were R2=4.7k and R1=10k, VIN=3.3v (from a regulated supply, the Mega was powered via USB) which gave me VOUT=1.055v. I also tried 470k/1M with a 100nF smoothing capacitor, which would drain the battery less but be less accurate.
The problem I found at this point was like all things Arduino, the hardware is wildly inaccurate! The 1.1v Aref on my Mega2560r3 for example is more like 1.09v, so I had to fix that in software, which then gave me spot-on results:
|
|
So I’m going to re-wire, or possibly redesign from scratch my weather node to be powered from 2xAA (giving 2.4-2.8v maximum approximately) and the boost converter instead of a coincell, and also record Vbatt in the packet I send to the Pi.