Hi all, I am very new to Arduino and I would like to know how to measure the battery voltage that is connected to the Arduino Mega 2560, can please guide me on how to do it? I'm using a 2 cell 7.4v li-po battery.
An analog input on the arduino can read up to 5 VDC, so you will need to create a voltage divider to scale the 7.4 v down to 5 (or slight less). Basically hook the voltage divider across you 7.4 v battery then the voltage from Vout and ground will be some scale of the 7.4 (scale based on R1 and R2 values). Then in your code, you will need to scale the voltage back up to get the correct reading. Make sure your R1 and R1 are large enough to not draw your battery down.
For example if R1 and R2 were 100K each, Vout will be 3.7 V and the current draw on the 7.4 battery will be 0.037 mA (nothing). the you analogread will see 3.7 VDC, the multiply that value by 2
math for the voltage divider.
Note that the above way has a minimum measuring voltage of ~6-6.5volt.
Once the voltage drops below that, the 5volt rail drops, and so does Aref.
I assume you have connected the battery to Vin, not to the DC jack.
A better, more stable way for a Mega is to use the internal 2.56volt reference voltage.
That does require a 1:2.5 (2:5) divider. e.g. 10k:25k.
Higher values, like the 100k:100k mentioned in the previous post, require a 100n sampling cap from A-in to ground.
Here is some (untested) Mega code to start off with.
Leo..
/*
display the voltage of a 2-cell LiPo battery with a Mega
uses the Mega's internal 2.56volt reference
10k resistor from A1 to ground, and 25k resistor (10K and 15k in series) from A1 to +batt
optional 100n capacitor from A1 to ground for stable readings
*/
float Aref = 2.478; // change this to the actual Aref voltage of ---YOUR--- Mega (~2.56volt), or adjust value to get accurate voltage readings
unsigned int total; // holds a number of readings for averaging
float voltage; // converted to volt
void setup() {
analogReference(INTERNAL2V56); // use the internal ~2.56volt Aref of a Mega
Serial.begin(9600); // ---set serial monitor to this value---
}
void loop() {
analogRead(A1); // one unused reading to clear any ghost charge
for (int x = 0; x < 10; x++) { // 10 analogue readings
total = total + analogRead(A1); // add each value to a total
}
voltage = total * Aref * 0.35 / 1024; // convert readings to volt, 0.35 is resistor ratio and 10 readings
// print to serial monitor
Serial.print("The battery is ");
Serial.print(voltage);
Serial.println(" volt");
total = 0; // reset value
delay(1000); // readout delay
}
Thank you guys!!
Remember that a fully charged LiPo cell is 4.2V (8.4 for your 2S pack).
You may want to measure the ratio of battery voltage to voltage divider output. That way you can compensate for the accuracy of the resistors (typically 5% or 10%).
Easier to adjust only one thing in the maths.
I adjust Aref untill I have the right voltage on my display.
10% resistors are last century.
1% metalfilm is standard, but 5% carbon is still around.
Leo..