read voltage of battery powering arduino

Hi,

I have a battery powering my arduino project. I'm wondering how I could read the voltage left in the battery? I'm sending messages back to the cloud and want to send that back to so I know when its running low.

Thanks

@ultrasonicbananna, check this out:- Monitor Arduino Battery

@ultrasonicbananna,

You can read the voltage of the battery the same way you read a potentiometer. It's called the voltage divider.

Where:
U ==> Input voltage (the battery voltage)
Note that the battery is sharing the ground with Arduino.

// number of analog samples to take per reading
#define NUM_SAMPLES 20

int sum = 0;                    // sum of samples taken
unsigned char sample_count = 0; // current sample number
float voltage = 0.0;            // calculated voltage

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    // take a number of analog samples and add them up
    while (sample_count < NUM_SAMPLES) {
        sum += analogRead(A2);
        sample_count++;
        delay(10);
    }
    // calculate the voltage
    // use 5.0 for a 5.0V ADC reference voltage
    // 5.015V is the calibrated reference voltage
    voltage = ((float)sum / (float)NUM_SAMPLES * 5.0) / 1024.0;
    // send voltage for display on Serial Monitor
    // voltage multiplied by 11 when using voltage divider that
    // divides by 11. 11.132 is the calibrated voltage divide
    // value
    Serial.print(voltage * 11.002);
    Serial.println (" V");
    sample_count = 0;
    sum = 0;
}

I personally used a 1M ohm resistor as R1 and a 100k ohm resistor for R2

You need to measure your voltage across 5Vout and the ground of Arduino, as that's what is the ADC reference (unless otherwise specified). After that you can change the 5.0 is this line

 voltage = ((float)sum / (float)NUM_SAMPLES * 5.0) / 1024.0;

After that you need to measure/calculate the divider factor (this is due to the tolerance of every component).

To calculate the divider factor you need to divide the voltage drop across the whole circuit by the voltage drop across R2 .

Then you should modify this line:

  Serial.print(voltage * 11.002);

See the image for calculations:

Many thanks for your advice :slight_smile:

This Thread has code for measuring the battery voltage by reference to the internal reference voltage

...R