Arduino Battery Voltage Indicator

131,296

404

96

Posted in TechnologyArduino

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

  • Casting Contest

    Casting Contest
  • Microcontroller Contest

    Microcontroller Contest
  • Woodworking Contest

    Woodworking Contest
user

We have a be nice policy.
Please be positive and constructive.

Tips

1 Questions

Really cool post! I'm confused by the voltage you are reading in though. Isn't this just the voltage across one of the 10k resistors? So then you aren't reading the full voltage across the battery? And aren't you still sending too much voltage to Vin or can that pin take 9V?

Great :)
About Vin, yes on Arduino Uno it can take up to 12V :)

1 more answer

never mind I missed the part that you doubled it in the code. all good :)

96 Comments

Any one know how to measure battery level? How much charge is remaining?

2 replies

This is it, isn't it? :D

No. It just shows or displays how much voltage battery can supply.

What I asked is Battery %. How much charge is remaining?

You know... Great work on using Arduino and I would never put down anyone for furthering the maker community, but you know they sell literally hundreds of battery gauges on amazon for under $20, right?

2 replies

You are right about that. I am living in a small town in North Sumatra, Indonesia and this was mad three years ago. At that time, local online stores were not as much as today. If I were to order from eBay or Amazon, the shipping fee would cost more than the item we were buying. Today we have more options, but still there are small towns where people can't get the battery gauges easily, then they have another option, make one ^_^

Also, if your robotics are made of Arduino (just like my RC Panzer), then I only need to add the code in and plug a red LED on any I/O pin to shine when the battery is too low. In this case all I need is a red LED and two resistors for my 7,4 volts battery, much cheaper than a fancy battery gauge isn't it? ^_^

I was thinking about your project and I thought of a cool way to add a second battery and have it automatically switch when the power from the first got low. So your project succeeded in getting me thinking about things I can do to improve my drone. :) cheers.

Very Nice and Informative project. A small Question I want to read the voltage from 12V battery and want to print with the time and Date on the LCD and also save the values with date and time in .csv file. Can you please help me?
Which Resistor value for voltage divider I should use?
how I have to change the code. I will be really thankful for your kindness.

3 replies

Sorry for a very late reply. For 12V battery you can use 3 identical value resistors. Let's say 3 x 10K resistors.

Simply change this code:

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

To this one:

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

Regarding to .csv file, I have never worked with SD card module but you can take a look at this topic : https://forum.arduino.cc/index.php?topic=447602.0

Thank you for your reply. I am using at battery and will be having alot of current output. It is 12v 100 Ah battery so ho to use it.

It is better to power up your Arduino with a separate power supply. If you use the same battery to power your Arduino, make sure you have a voltage regulator circuit to step down the voltage to 9V, because 12V battery may be fully charged to some kind of 14V. About high current normally Arduino will draw only as much as needed, but it is better to put a fuse in case of short-circuit. Technically, following this instructable by using 3 pieces of 10K resistors and a little change in the code (*2 to *3) are all you need.

how can i make it for 44volts Li ion batteries for an electric car

1 reply

Technically you can use 10 x 10K resistors to divide the voltage. But, looking at your high current battery which may harm your Arduino, it is best to take another option. There are high current (30A) digital voltmeter and ammeter in markets. You just need to pick one and learn how to connect it to arduino+gsm shield if you say you want to send message to your phone when the battery is low.

Hi, thanks a lot for this project it was really helpful.
Although I have a problem : I want to use a 3V battery without a voltage divider and for some reason it displays the wrong voltage value (I used a fully charged 3V battery, and it displayed something like 8,4V).
I guess it's coming from this line : sensorValue * (5.00 / 1023.00) * 2

Any help ? Thanks again btw !

1 reply

Yes, that is so true. Omit the " * 2 ", because I was using 2 resistors as voltage divider, then I had to multiply by 2 to get the divided voltage. In your case, when you remove " * 2 " (read: multiply by 2), you will get 4.2V, try to measure using a voltage meter ;)

Hi,
what's about if voltage divider result max volt is not 5 V, may be 6.25 Volt
as battery volt measure with avo meter is 12.5 with divider use R 2k

thx before

1 reply

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 ^^

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

1 reply

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(" %");

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