Introduction: Arduino Battery Voltage Indicator

Picture of Arduino Battery Voltage Indicator

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

Picture of 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

Picture of 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

Picture of 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

Picture of 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 :)

Comments

MahmudS7 (author)2017-12-03

sir if i want to measure the battery percentage with arduino... how can i do that? can u help me sending a code for it? thanks in advance sir

chienline (author)MahmudS72017-12-03

You didn't mention your battery spec clearly. If you are using a battery between 5V to 10V then the above code is okay. If you want to switch the voltage to percentage, simply change these lines:

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

to

float voltage = sensorValue * (5.00 / 1023.00) * 2 / [your battery voltage here] * 100;

Then change :

lcd.print(" V");

to

lcd.print(" %");
PaundraT (author)2017-11-30

will it work with 3.7v Li-Ion battery?

chienline (author)PaundraT2017-11-30

Yes, sure. Just plug the possitive of battery into Arduino A0. Here you don't need voltage divider resistors but you have to change something in the code, where we count the voltage, a line like this:

float voltage = ...

You have to omit "* 2" on that line.
Remember that in this case you need separate power to run your Arduino and the LCD :)

adiif1 (author)2017-11-14

Is possible to oled screen?

chienline (author)adiif12017-11-15

Yes, sure. If only you know how to connect your OLED. Just print the result to your OLED screen. I have an OLED but I have never succeeded to make it work on my ESP12 board :D

I will be back working on it some other day.

Pak Yat (author)2017-10-11

Dear sir,

I am using a custom 14.8v li ion batteries for my e bike. can i used your design to monitor the batteries ?

chienline (author)Pak Yat2017-10-11

Yes. Sure. Use 4 resistors of the same value to divide the voltage. Do not use 3 resistors because your battery can be charged up to15V and feeding more than 5V to Arduino i/o pin is not safe.
You can refer to previous comment that was provided with sample wiring diagram.

MarkW421 (author)2017-09-26

Hi sir, It is possible i use arduino nano for this project. and combine with this circuit in order to create charging and discharging circuit for the litthium ion battery. ?

http://henrysbench.capnfatz.com/henrys-bench/arduino-projects-tips-and-more/arduino-18650-battery-charger-project-1/#Opportunities_for_Improving_this_Project

chienline (author)MarkW4212017-09-27

Yes you can. Only that you need to make sure it is 5V and not 3.3V. For 3.3V Arduinos you need more resistors for voltage divider and also change the code to get the real voltage back ^^

Hồ MinhT (author)2017-09-16

Hi Sir,

I'm doing a project using arduino to measure the battery and display it on the LCD.I use a 5V battery for it. But after I follow the instructions and use the formula (float voltage = sensorValue * (5.00 / 1023.00) * 2;). On the LCD display measured 7.75 V. U can help me. thanks

chienline (author)Hồ MinhT2017-09-16

Your wiring looks okay. But I suspect that you are using two sources because 5V seems like not enough to power up Arduino and LCD. If you are using more than 1 source, make sure it is 5V that goes to voltage divider resistors.

Hồ MinhT (author)chienline2017-09-17

I am using a battery 5V for LCD, and arduino plugged into the computer. I do not understand why when I plug the arduino into the battery 5V with LCD. It still displays parameters like when plugged into the computer. I use source like in pictures attached

chienline (author)Hồ MinhT2017-09-17

Oh, yes. Your powerbank is possibly overcharged to 7V or 8V. To figure this out please use a multimeter and test the voltage on your breadboard power pins and see what we get.

Hồ MinhT (author)chienline2017-09-17

I will do and send test results if correct. Thank man

RagavanS (author)2017-09-10

Totally worked! Thanks man. My machine was going nuts and it turned out to simply be a case of low battery!

chienline (author)RagavanS2017-09-10

You are welcome ^^

JosuaR1 (author)2017-09-05

If your battery is already the the correct voltage for the Arduino, can you just plug the 5v straight into the Analog pin?

chienline (author)JosuaR12017-09-05

Yes you can plug 5V battery directly into Analog pin, but it is not recommended because 5V battery can be fully charged to more than that. Arduino I/O pins can bear slightly higher than 5V but it is too risky.

You are highly recommended to use voltage divider circuit for your Arduino safety.

howweyd (author)2017-08-08

can this method handle 12V battery?

chienline (author)howweyd2017-08-09

Yes you can. Use 3 identical value resistors as voltage divider. Then find the code :

float voltage = sensorValue * (5.00 / 1023.00) * 2; //convert the value to a true voltage.

and change it to :

float voltage = sensorValue * (5.00 / 1023.00) * 3; //convert the value to a true voltage.

Concentrate on wiring the A0 pin, it is next to the resistor which is connected to the Ground. See previous comments where I attach photo for the A0 wiring diagram using 24V battery ^^

howweyd (author)chienline2017-08-10

was it possible not to use 12C adapter?

.. bcos i dont have one..

chienline (author)howweyd2017-08-11

On Step #3 there is an option to use a LED instead of LCD. You just need to turn the LED on in the code when it reads certain minimum voltage :)
If you have no I2C adapter and still want to use LCD, then just search on the net about wiring LCD to Arduino. You need more I/O pins on Arduino and add some code to get the LCD works :)

howweyd (author)chienline2017-08-09

thanks man

chienline (author)howweyd2017-08-09

You are most welcome ^^

ninaliuzm (author)2017-04-25

Sir , I am doing a solar car project . We need to change the output voltage of the solar panels(or rechargeable battery,which is 6V in total). How can we do that with the Arduino? For example, At the starting period of the race, we need less voltage more current to gain more torque(shift the voltage to about 3V). After a few second we wanna to change the voltage output to 6V. Another problem is the max voltage of the Arduino is 5V. So should we add a resistor to fix this problem.

chienline (author)ninaliuzm2017-04-25

If you want to use this voltage indicator circuit to know the remaining power then yes you need two resistors with the same value as voltage divider, because A0 pin can only handle 5V while your battery is 6V and could be more when fully charged.

If you don't need the battery indicator then your 6V battery can goes directly into VIN pin which can handle up to 12V.

How will you bring down the voltage to get more current? Dropping the voltage to 3V will shut down the Arduino doesn't it?

GlenL22 made it! (author)2017-03-30

Thanks, used this with some edits to suit my Due board to monitor my 2 x 18650 batteries powering my weather station. When volts drop to 5.5, text turns red...time to change/charge batteries. If you don't and volts go too low, the board memory gets wiped! This saved me from having to reload the sketch!

Thanks

Glen

chienline (author)GlenL222017-03-30

That's great. You're welcome ^^

suryaprasanth (author)2017-03-28

Sir I need the various camand for various voltage level what can I do

chienline (author)suryaprasanth2017-03-28

First, define the maximum voltage of your battery. Remember that LiPo batteries may be higher than their ratings when fully charged. For example, I have a maximum of 24V to be tested. I use 6 resistors that have same values. Actually 5 are enough in my case, but I just want to explain better with 6 in the picture I attach. Battery that is higher than the voltage Arduino can handle is not recommended because it will be bad if it is not carefully wired.

You should take between the first and the second resistors CLOSEST TO GROUND (negative of battery) those are R5 and R6 in the picture and wire it to Arduino analog pin A0.

Concentrate. If you plug somewhere at where I mark with red A to E, then BOOM!! you will lost your Arduino pin, or worst, the whole Arduino :(

Because the voltages there are higher than that 5V Arduino pin can handle.

Don't forget to change the sketch with *6 to convert into true voltage :

float voltage = sensorValue * (5.00 / 1023.00) * 2; //convert the value to a true voltage.

to

float voltage = sensorValue * (5.00 / 1023.00) * 6; //convert the value to a true voltage.

With 6 resistors, you can measure battery up to 30V. Just remember to concentrate on wiring the A0 pin, that is near the Ground. ^^

عمرأ1 (author)2017-03-16

Thanks alot sir for this project
i used it without lcd change it to serial
i have a problem reading the values
when i put 3.7v battery it gives me 4.6
when i used 4.1v battery it gives me 3.9v
what should i do ?

chienline (author)عمرأ12017-03-16

Do you change any of the circuit above? If you are using battery lower than 5V then you don't need the voltage divider circuit (two identical resistors). Simply plug your battery (-) to arduino GND and battery (+) to Arduino A0, then you need to change the code a little. Find a line commented with "//convert the value to a true voltage" and remove the " * 2".

عمرأ1 (author)chienline2017-03-18

I know i have done all of these
i want to use a battery 3.7 v to feed the arduino i put it on the vin as well as the analog pin but on the lcd it gives me 5 v
wht ahould i do ?

chienline (author)عمرأ12017-03-22

Sorry for my late reply. From what you described, you must be using Arduino Mini or Pro 3.3V? If you are using Arduino Uno, then you must be connecting your USB cable because 3.7V battery is not enough to power an Arduino Uno. If you power your Arduino Uno with USB cable and you only need to test the battery voltage then DO NOT connect battery to Arduino Vin. If it doesn't workout for you, then maybe you can share your wiring by uploading the picture of it here for me to investigate ;)

vipul.swamy (author)2017-03-17

I made it Sir Thank you

chienline (author)vipul.swamy2017-03-17

You are welcome ^^

KlausHoller (author)2017-03-08

I made one with LED. I was wondering what needs to be cahnged to use a 12V battery. Any advices?

chienline (author)KlausHoller2017-03-11

​Sorry for being late to reply. 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.


Hope this help ^^

KlausHoller (author)chienline2017-03-11

Thank you, I did it and it worked perfectly!

chienline (author)KlausHoller2017-03-11

You are welcome ^^

John AngeloS2 (author)2017-02-22

Hi I'm doing a project using lithium ion batteries and I need to measure the voltage. ratings of the battery are 2250mah and 4.2v each. I know arduino can measure the voltage right away but can the arduino withstand the 2250mah total of 36A ? thank you

chienline (author)John AngeloS22017-03-11

Arduino pin will only read the voltage and drawing a very little current. The rating 2250mah simply tell us how long your battery will last on certain circuit/power consumption.

ghostriderA (author)2017-03-06

float voltage = sensorValue * (5.00 / 1023.00); please explain this formula how u got this ?

chienline (author)ghostriderA2017-03-06

Well, Arduino analog pin reads from 0 to 5V. It has 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. What we get from analogRead() function is an integer from 0 to 1023. So let's say we read an "800" (eight hundred). We need a voltage from that reading so it is : 800/1023 * 5 V = 3.91 V.

If you have previously reduced your battery voltage with a voltage divider into half, then you need to multiply by 2 to get back the real voltage.

ghostriderA (author)chienline2017-03-07

Thank u

rizay (author)2017-02-05

Thanks for the great tutorial. I just wanna ask you one thing , if I have 3,7 V LiPo Battery , Can I plug the battery pin directly to A0 ? is it okay with the battery and Arduino ?

If not , can you give me some recommendation circuit

chienline (author)rizay2017-02-07

Thank you. Yes you can connect battery ground to Arduino ground. Then battery possitive pin to Arduino A0. Then simply change this line in the sketch:

float voltage = sensorValue * (5.00 / 1023.00) * 2; //convert the value to a true voltage.

to this one:

float voltage = sensorValue * (5.00 / 1023.00); //convert the value to a true voltage.

That is removing "*2" because your battery's voltage is below 5V and can be handled by Arduino pin directly without voltage divider.

ArturK21 made it! (author)2017-01-15

Great tutorial, thank you :) could you explain me two thing please :

1) after i connect my 9V battery , initial voltage is around 8.45V , but it quickly drops down to below 8 and continues to go down quickly ( 1/100V every couple of seconds seconds ) - is it really that fast battery gets depleted?

2) Resistors and voltage divider - i had only 1K resistors ( if i'm not mistaken ) so i connected both - it worked fine - why did you recommend 10k? is it why my voltage drops so quickly? also way how i connect to A0 - i did it in the middle of 2 resistors ( on the photo so R A0 R ) does it matter if it would be before or after a leg of resistor ( A0 R R ) , or what would happen if i would connect it after second resistor?

Thank you

chienline (author)ArturK212017-01-16

Congratulation you have made it :)

From what your explanation you are using 1K resistors. It means more current is flowing in the circuit (and the voltage is dropping) and the resistors are getting hot. If you use larger value resistors let's say 10K or even 100K, that means less current is flowing and less hot on resistors.

It is important to use the same value resistors and connect your A0 at the middle of those resistors. It means we divide the battery voltage into half for A0 to read (max 5V), then we double the reading in the sketch to get back the battery voltage. For more than 10V battery, see previous comments where we can use 3 or 4 resistors and a little change in the sketch.

If you plug A0 before resistor1, it means you read the battery voltage directly. It is okay if your battery is lower than 5V, then you need no multiplication by 2 in the sketch. But if your battery is more than 5V, then you will burn your A0 pin, or any other part of your Arduino at worst.

If you plug A0 after resistor2, you will read zero voltage because your A0 is now connected to Ground.

About This Instructable

114,924views

404favorites

License:

Bio: Just an ordinary person who loves #thinking and #tinkering
More by chienline:DSLR Cowboy HolsterWoven Tag Hiding PlaceDouble Spaces Inhaler
Add instructable to: