Arduino Battery Voltage Indicator

217,738

443

135

Introduction: Arduino Battery Voltage Indicator

About: Just an ordinary person who loves #thinking and #tinkering

When we are using a battery powered Arduino such as RC robots or Temperature Controller, we might want to check the battery voltage if it needs to be charged or replaced. It happens to me with my RC Panzer. Sometimes when my kids are about to run it, it moves very slow, low battery. Then they are disappointed and need to wait for charging time. I would rather had noticed this battery condition on the last run but I am too lazy to check it with multimeter.

Arduino Uno needs 5 volts power to run, then we need at least 7.4 volts to 9 volts battery. Since Arduino pins support only 5 volts maximum, then we need a Voltage Divider. It is simply made up of two resistors in series. To divide the voltage to half, we need two resistor with the same value. 1K to 20K resistors can be used, but the larger the resistance the lower the power consumed by the Voltage Divider. I am not that good in calculating such thing but that is what I summarize from sources I read. You can correct me if I am wrong and any better explanation to this is most welcome on the comment section.

Step 1: Bill of Materials

  • Arduino Uno or compatible.
  • Male pins (we need four pins only).
  • Two same value resistors (here I use 12K).
  • Two pin female connector. Photo shows three pin because that is what I have. Here I use power switch connector to motherboard instead :)
  • A battery (I use 7.4V lipo battery).
  • 16x2 LCD with I2C adapter. Later on you can switch this to a red LED to indicate low battery at your desired voltage level.
  • A mini breadboard is optional for testing phase.

Step 2: Arduino Sketch

Well, I would like to upload this sketch to Arduino first before connecting it to a battery for testing. Uploading this will show you nothing before you connect all the parts needed for this project, but sooner or later you will still need to upload this sketch. I am not sure what will happen if you have power from usb and also from Vin at the same time. I guess it will be okay, Arduino designers must have think of this possibility and prevent this power conflict. But I will never try it on purpose and risking my Arduino to get burnt :P

In this instructable, I am not explaining about "how to get your LCD display works", but I will leave some links here (which I use) to get your LCD works through I2C connection:

// Print battery voltage
// to 16x2 LCD via I2C
// with Voltage Divider (2x 10K resistor)
/*
  Resistors are aligned in series.
  One end goes to Battery - and also to Arduino GND
  The other goes to Battery + and also to Arduino Vin
  The middle (connection between two resistors) goes to Arduino A0
*/

#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>

#define I2C_ADDR    0x27 //Add your address here.  Find it from I2C Scanner
#define BACKLIGHT_PIN     3
#define En_pin  2
#define Rw_pin  1
#define Rs_pin  0
#define D4_pin  4
#define D5_pin  5
#define D6_pin  6
#define D7_pin  7
#define led_pin 13
LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

void setup()
{
  lcd.begin (16,2); //My LCD was 16x2
  lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
  lcd.setBacklight(HIGH);
  lcd.home (); // go home
  
  pinMode(led_pin, OUTPUT);
  digitalWrite(led_pin, LOW);
}

void loop()
{
  printVolts();
}
 
 void printVolts()
{
  int sensorValue = analogRead(A0); //read the A0 pin value
  float voltage = sensorValue * (5.00 / 1023.00) * 2; //convert the value to a true voltage.
  lcd.setCursor(0,0);
  lcd.print("voltage = ");
  lcd.print(voltage); //print the voltage to LCD
  lcd.print(" V");
  if (voltage < 6.50) //set the voltage considered low battery here
  {
    digitalWrite(led_pin, HIGH);
  }
}

Step 3: Circuit

The wire connections are simple as you can see on the images above.

Using 16x2 LCD and its I2C adapter:

  • Adapter GND to Arduino GND.
  • Adapter VCC to Arduino 5V.
  • Adapter SDA to Arduino A4 (or the pin next to AREF on Digital Pins side).
  • Adapter SCL to Arduino A5 (or the pin next to SDA, two pins from AREF on Digital Pins side).
  • Align two 10K resistors in series on breadboard.
  • Connect the middle of the resistors-in-series to Arduino A0.
  • Connect one end to Arduino GND and also to Battery - (negative).
  • Connect the other end to Arduino Vin and also to Battery + (positive). I think you should connect this one after you load the Arduino Sketch as I tell you my reason before.

Using a LED instead of LCD:

  • Connect LED anode (small piece inside) to Arduino D13.
  • Connect LED cathode (large piece inside) to GND (next to D13).
  • Align two 10K resistors in series on breadboard.
  • Connect the middle of the resistors-in-series to Arduino A0.
  • Connect one end to Arduino GND and also to Battery - (negative).
  • Connect the other end to Arduino Vin and also to Battery + (positive).

When you plug the battery to Arduino Vin, it should work right away showing the voltage of your battery on your 16x2 LCD because Arduino is powered by that battery. If it is not working, please re-check your connection or the battery you use might be lower than 5 volts needed by Arduino to power up. Please try another battery or check it with your voltmeter.

On my test with multimeter, the voltage shown on the LCD is slightly lower then the multimeter display. We are loosing around 0.05V to 0.15V on breadboard and Arduino circuits. But that is not a big problem for me (I don't know what about you), as long as I know whether my battery has enough power to run my robot. That's all.

Step 4: From Mini to Micro

Well, I don't want that "mini" breadboard goes along on my Panzer, then I make it "micro".

  • The first resistor : One end soldered to pin 1 from the left. The other end to pin 4 from the left.
  • The second resistor : One end soldered to pin 2 from the left. The other end to pin 4 from the left.
  • Soldered the connector inner pins to pin 1 and pin 2 from the left.
  • Put the black connector jacket on.
  • Pull out pin 3 from the left.

Well, now we have add-on to Arduino pin : GND, Vin, A0.

Step 5: Re-Connect and Run

Now re-connect the LCD and Battery, we have simpler connection without breadboard.

  • Adapter GND to Arduino GND.
  • Adapter VCC to Arduino 5V.
  • Adapter SDA to Arduino A4 (or the pin next to AREF on Digital Pins side).
  • Adapter SCL to Arduino A5 (or the pin next to SDA, two pins from AREF on Digital Pins side).
  • Battery - (negative) to Arduino GND (on our add-on pins).
  • Battery + (positive) to Arduino Vin (on our add-on pins).

On my test I lost 0.1V and pretty stable. Actually we lost only 0.05V on Arduino circuit. 7.79V is shown on my multimeter. Voltage Divider reduced it to half, that is 3.89V entering the A0 pin. The Arduino reads 3.84V. Then we double it to show the exact voltage back, that is 7.68V.

We can fix this in the sketch, but we need more data population to see the stable voltage lost. One more question is : "Is it my multimeter that is not accurate? Because I bought a cheap one."

Again, I don't mind that little difference as long as I know my battery is fit enough to run my RC toys :)

3 People Made This Project!

Recommendations

  • Toys & Games Contest

    Toys & Games Contest
  • Big vs Small Challenge

    Big vs Small Challenge
  • Fix It Challenge

    Fix It Challenge

135 Comments

0
Zague
Zague

Question 3 months ago

Hello guys thank you for the article, I love it. I have a question, I have a project with arduino and it use batteries, I would like to know the %of the battery in order to get an idea when to charge it. This article say how to measure external battery. I tried to use this article to send the voltage from my battery to the dive voltage then to A0 but it didn't work. Any idea?
Thank you

0
chienline
chienline

Answer 3 months ago

If I don't read it wrong, you have the same setup as my project above. Powering Arduino with battery and you want to check the battery level to know when to re-charge the battery.

The circuit setup is the same as step #3. Battery to Vin, GND and A0 (through voltage divider resistors) and if you use the above setup and the same code (arduino sketch) then the internal LED or the LED you plug on PIN13 will light up when the voltage reach 6.5V which I set as my "time to re-charge" as you can find in the code.

Percentage is just a matter of calculation when we set the max and min of our battery. Let's say my max voltage is 7.4 and min 5V (min voltage to power up the arduino), then 7.4V = 100%
5V = 0%
6.5V = (6.5 - 5) / (7.4 - 5) x 100% = 62.5%

Why I pick 6.5V to recharge when it has 62.5% remaining? That numbers are on paper. In real run, maybe the arduino will shut down when the battery reach 6.2V, because we are not running only the arduino, you should consider the power consumed by LCD or any other modules on your project.
If my project dies at 6V and my battery fully charged at 7.8V then the calculation become :
7.8V = 100%
6V = 0%
6.5V = (6.5 - 6) / (7.8 - 6) x 100% = 17.86%, yes.. time to re-charge.

So change the code according to your battery used in particular project. LCD consumes a lot of battery juice then I would prefer set the limit in the code and tell me when to re-charge by lighting up the internal LED.

0
leilahertz
leilahertz

Question 6 months ago

Hey thanks so much for this article, it is so helpful! This is a pretty basic question, but what connecter do I need to connect the positive and negative ends of the battery to the Arduino circuit? I'm using a Protek RC Graphene Plus LiHV 4100 mAH battery pack.

0
chienline
chienline

Answer 6 months ago

Okay, I used to do the battery checking before and after toying. I see that you want to connect the battery voltage indicator permanently to your battery and your RC toy, but remember that the battery voltage indicator circuit will also drain your battery this way.

You need these connectors :
T-style connector 1 female, 2 males. (Name this connector C1)
T-style connector female to JST male. (Name this connector C2)

Battery plugs into C1-Male1.
C1-Female plugs into RC toy.
C2-Female plugs into C1-Male2.
C2-Male goes to Battery Voltage Indicator Circuit.

Or you don't even need the C2 connector if you want to solder the circuit directly to C1-Male, but remember to seal the connector properly to avoid short-circuit.

0
adityahakani29
adityahakani29

Question 7 months ago

hey your code works perfectly and also made it thanks for it but i want make it for 12v battery can you please tell me how can i do it and is it possible to to also show percentage as well for 12v battery please reply

0
chienline
chienline

Answer 7 months ago

Actually you can find the answer in comment section. Saving your time let me do it for you. I love refreshing my memories after all ^_^

For 12V battery you can use voltage divider with 3 identical value resistors. So your battery voltage will be divided by 3 and as long as it is not over 15V at peak, it is safe for the arduino analog pin to read.

Let's say the voltage divider circuit is now :
BatteryPossitive -- R1 -- R2 -- R3 -- BatteryNegative/GND

Run a jumper wire from between R2 -- R3 to Arduino A0.

Then you need to change the code from "*2" to "*3" where we calculate back to RealVoltage to be shown on LCD.
Find in the code:

float voltage = sensorValue * (5.00 / 1023.00) * 2;
Change it with this:
float voltage = sensorValue * (5.00 / 1023.00) * 3;

Yes, simply change the number "2" to "3" in multiplication, because now we use 3 identical resistors instead of 2.

0
manangupta07
manangupta07

Question 1 year ago

What is the other Module used in the process except the arduino uno and the lacd screen ?

0
chienline
chienline

Answer 1 year ago

Do you mean the I2C adapter module? It is a module that simplify LCD connection to the Arduino Uno. Without it you need a lot of cables and Arduino I/O pins to get the LCD works ^_^

0
gaikwadhm000
gaikwadhm000

Question 1 year ago

Hi @chienline,
I have used 7.4V ,I have interfaced battery with controller but in wanted calculate the percentage of battery.

I have interfaced battery via voltage divider with ADC Input ,My divider is as ,
R1=10k,R2=4.7k and i have used 3.3v as vref
I have referred your tutorial,
as Max Cutoff voltage of battery is Vin 7.4V
=(R2/R1+R2)Vin
=2.36V and My Max ADC count is 800
and if i consider min cuttoff is 5.2 so,
=1.66V
Hence ,
At 100% =2.36-1.66=0.7
at 1% 0.7/100 =0.007

BatteryPercantage = (realvtg-1.66)/0.007
realvtg=ADCCount x(2.36/800)

But My result is Coming wrong ; I dont know where im doing wrong.
Can you Please tell me how can do this.

0
chienline
chienline

Answer 1 year ago

I am not good at explaining math or physics but based on your formula, simple answer would be:

BatteryPercantage = (2.36 - readVoltage)/0.7 * 100;

realvtg = (readVoltage/2.36) * 7.4;

0
DarylL22
DarylL22

1 year ago

Hi. if i am using a 3.7V LiPo battery , i will not need a voltage divider right? then may i know where should i connect the A0 pin?

0
chienline
chienline

Reply 1 year ago

Your Battery positive lead (the one you are having the voltage measured) goes to A0 pin :)

0
DarylL22
DarylL22

Reply 1 year ago

Hi, thanks for your prompt reply.So basically, i am using A5 instead of A0 and when i connect the battery positive lead directly to A5 and negative lead of battery to GND of Arduino UNO the LED on the arduino UNO board is not lighting up, i suspecting that the power supply is not huge enough or am i wrong ? please correct if i mentioned anything wrongly. P.S. I have attached a image of my coding for reading the battery voltage, please help me check thanks :)

battery voltage coding.JPG
0
chienline
chienline

Reply 1 year ago

"The LED on Arduino Uno board is not lighting up", do you mean the onboard LED (one the same with D13) which we set to be "light up" on low battery (below 4.1V in the code) ...
or the LED telling the Arduino Uno board is not running?

Are you using the same battery (3.7V) to run your Arduino? Arduino need at least 6V to power up and at least 7V to feed 5V to its I/O pins.

0
poli.karlo
poli.karlo

Question 2 years ago

I was wondering if this is possible with a 24v batterys. I have a electric scooter so i want to see the charge % ??

0
chienline
chienline

Answer 1 year ago

Yes, why not?
24V battery can be fully charged up to 28V or more. Fully charged your battery and measure the voltage with a multimeter. If it is below 30V then you can use 6 resistors with same value for the voltage divider. If it is over 30V then pick 7.
Just a little change in the code (sketch), find the line :

float voltage = sensorValue * (5.00 / 1023.00) * 2;

and change it to :
float voltage = sensorValue * (5.00 / 1023.00) * 6;

Change the multiplication to * 6 or * 7 according to the number of resistors you use on your voltage divider. We divide the voltage with resistors, then we multiply it back on the sketch, simply like that ^_^

0
BriceA4
BriceA4

Question 2 years ago on Step 2

Hi! I'd like to know how you came up with that relation : (5.00/10.23)*2 when you calculate the battery level/voltage ?

0
chienline
chienline

Answer 2 years ago

The maximum voltage on Arduino analog pin is 5V. That pin has 10-bit reading = 2^10 = 1024. It means that the analog pin reads value between 0 - 1023.
Let's say our battery has 8.2 volts left in it. 8.2V is beyond maximum of Arduino pin voltage which is 5V and it will fry that pin if plugged directly. So we need a pair of resistors as voltage divider which divides the battery voltage to 8.2 / 2 = 4.1V.
That 4.1V is read by Arduino pin as : 4.1 / 5 * 1023 = 839 (rounded).

Now we try to calculate back that sensor reading value to voltage :
839 * (5.00/1023)* 2 = 8.20136852394 (according to my 12 digits calculator)
The number 5.00 (five point zero zero) will round the value to two decimal digits.

If we have 10V battery, the pin reads 5V (after voltage divider), the digital number read by Arduino pin is 1023 (max). If we stick to the 10-bit (1024) in the formula, we will get :
1023 * (5.00/1024)*2 = 9.99V, and we will never get the 10V. That is why it is 1023 in the formula ^_^

0
SreeA7
SreeA7

Question 2 years ago on Step 2

is this program is correct

0
chienline
chienline

Answer 2 years ago

Yes it is. There was about 0.1 volt difference compared to (my) multimeter but it had stable result on several tests. Then I could simply reduce 0.1V in the code, but I left it as is. If you want higher accuracy, collect test data to count average voltage lost in the measurement and adjust the calculation in the code (like simply reduce the result with 0.1V) to get the same result with your multimeter.