I am trying to measure the voltage of 2 x AA batteries so I know when to replace them, but the code is not accurate. When I apply voltage from my batteries to Analog pin 0 (A0), it displays the same voltage as my multimeter does. However, if I unplug the batteries, it displays 1.4 - 1.3 volts (it should display 0v).
void setup()
{
Serial.begin(9600);
}
void loop() {
int sensor = analogRead(A0);
Serial.println(sensor);
float voltage = (sensor * 5.0) / 1023;
Serial.println(voltage);
delay(2000);
}
As the picture shows, it instantly shows the correct battery voltage when I attach the battery (other question - why does the voltage diminish really quickly from 2.72v to 2.67v, but the multimeter still says 2.72v??), but with no battery:
Circuit diagram:
If anyone can help, I would greatly appreciate it!
Thank you!
When you disconnect the Battery, the A0-pin becomes floating and picks up environmental electrical noise; so, connect a 10k resistor between A0-pin and GND?
2 Likes
I don't want to add a resistor between A0 & GND every time I unplug the battery, can I leave the resistor in place?
Please show your real circuit diagram including your meter. Your Fritzing is nonsense and cannot work.
Yes.
The Mega2560 IS the meter, that is my exact circuit layout.
Adding the resistor fixed THAT issue, but now it is 0.1 volts off...
Common ground (negative) connection ?
Disconnect battery from A0-pin. Make a direct short with a jumper between A0-pin and GND. Now, report the result.
Look at your Fritzy. To measure a voltage you need TWO connections - because a voltage is a potential DIFFERENCE.
The Fritszy IS WRONG. What is the LED for?
Connect the wire from the bent leg of the LED to ground.
DISconnect the wire from the straight leg of the LED to battery +.
Permanently connect a resistor (say 100k) between A0 and ground
now when you remove a wire from the battery the reading should go to zero.
More information on measuring voltages with an arduino on my page here
Should it be 100k, or 10k? One person says 10k, another says 100k.
This is my final circuit:
At this point the resistor is 10k ohm.
Yes you can leave the resistor connected between A0 and gnd all the time. However you should know when the batteries are connected to the A0-GND and the 10k there will be extra load on the batteries. That load current would be:
current = voltage / resistance
current = 1.5 / 10,000
current = 0.00015 amps
current = 150 µA (micro amps, aka amps divided by 1 million)
What is the consequence of that extra load? Does it drain the batteries faster? Does it cause measurement inaccuracies?
Given that your battery's capacity will be rated in milliamp-hours, or equivalent, what do you think?
It will drain the batteries SLIGHTLY faster, but I'm not sure if it will cause inaccuracies in the battery voltage level or not. The battery will also be running a RTC module in the future.
The slight drain will be noticed only over long periods of time. It will not effect the accuracy of your readings.
Another option is to use a 100k resistor and a 0.1µF capacitor (ceramic). This will reduce the drain by 10 and the capacitor will keep the noise down. After all the goal is to not allow A0 to "float" unconnected.
@JohnRob
Is it necessary to use a 100k and a capacitor? Does that make it more accurate, or is that only a cosmetic fix?
My understand of the goal is to make the reading at or near 0 when the battery is not connected.
The 100k (or any resistor in the general range) will do that, however it may not be "0" but it will be close. The Capacitor will make the readings more stable (i.e. so they don't "jump around")
Neither component will effect the accuracy although the capacitor will make your readings more stable.
Note on stability. Unlike the examples in a text, the real world readings will have some variation from reading to reading. So for example a battery at 1.32 volts; the reading will drift (or change as you watch the reading) maybe 0.05 volts. The capacitor will reduce this variation.
Ahhh ok. That makes sense. I'll try that and let you know.
It certainly is more stable!!!!
You can see where I unplug the battery, it isn't 0.00, but SO close I'm not fussed.