Reading Battery Information

Hi;

For my web controlled robot project how do I read the battery condition of a battery connected to an Arduino Nano?

I found this code from another project that prints to the serial monitor but it returns values around 1.53, I have a Lithium Poly 2 Cell 7.4v battery? I guess the formula needs changing??

Thanks

void get_battery()
{
  int raw = analogRead(0);
  float val = fmap(raw, 0, 1023, 0.00, 5.00);
    Serial.println(val);
}

float fmap(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

Or your circuit needs changing?

Just thinking about my question, the Nano is being supplied from a 5v Regulator on an L298N Motor Drive so it would only ever be able to read its own voltage without some additional hardware??

So what would I need?

A voltage divider. If you are displaying 1.53 then you seem to already have a voltage divider.

MarkT:
A voltage divider. If you are displaying 1.53 then you seem to already have a voltage divider.

So how do I display the correct voltage in the serial monitor?

JonnyAlpha:
So how do I display the correct voltage in the serial monitor?

Simple multiplication. Unless you want to change resistor values. If that doesn't work, show us a schematic of your circuit.

"float val = fmap(raw, 0, 1023, 0.00, 5.00);"

You said you wanted to measure a 7.2v battery.

should the fmap() function be more like:

float val = fmap(raw, 0, 1023, 0.00, 7.20);

When using the map function, I celebrate it by using a volt meter, compared to the analog input value (raw).

Sure, assuming that 7.2V at the battery does actually produce 5V at the analog input... and yes, you should calibrate it. Please end the confusion and tell us what kind of voltage divider you have.

"Just thinking about my question, the Nano is being supplied from a 5v Regulator on an L298N Motor Drive so it would only ever be able to read its own voltage without some additional hardware??"

The "additional hardware" would consist of a piece of wire.

You can't read your 5v power, by referencing it to the 5v rail. If one changes, the other one will also.
To measure your own 5v power, you need to change your analog reference to a different value (like 3v3). analogReference(type)

OK sorry for all the confusion here is my setup.

Apart from the L298N Motor Driver which also provides a regulated 5v output which I am using to power the Nano I have no other voltage divider setup. So I am not sure if this is possible without adding some resistors?

Yes, you need a voltage divider (adding resistors), and you should put a small capacitor (0.01 - 0.1 uf), between the analog input, and ground.

I would prefer to get my power from a different source than a motor controler. I suspect the motor controller could be adding noise .

I see a 3.7V battery, not 7.2V. Is that just a "fritzo"?

aarg:
I see a 3.7V battery, not 7.2V. Is that just a "fritzo"?

Yup - it only has a single cell LiPo in the Library :frowning:

The nano has an onboard voltage regulator. Why not use it?

aarg:
The nano has an onboard voltage regulator. Why not use it?

So just run 7.4 straight from the LiPo to the Vin on the Nano?

So add a couple of resistors connected to A0, one going to the V+ on the battery and the other to Gnd. And as you suggest a Ceramic Capacitor between A0 and Gnd? Then the software should work?

JonnyAlpha:
So just run 7.4 straight from the LiPo to the Vin on the Nano?

Yes.

aarg:
Yes.

Will do, thanks

I agree with aarg

The whole post? Or just the Capacitor bit?