Arduino Battery Voltage Indicator

138,243

407

113

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

  • Metalworking Contest

    Metalworking Contest
  • Tiny Home Contest

    Tiny Home Contest
  • Creative Misuse Contest

    Creative Misuse Contest

113 Discussions

0
None
beic

Question 5 weeks ago

Hi, I really love your tutorial, but have a question.

I have a 100k and 10k voltage divider and I wish to monitor vehicle batteries like 12v and 24v, so my question is: how can I measure that in % as you proposed in one comment bellow:

Let us say my RC needs at least 6V to run its Arduino and motors and every modules in it. I am using a 7.4V battery that is fully charged to 8.4V. Do some math here :
8.4 - 6 = 2.4 (this is 100%)
2.4 / 100 = 0.024 (this is 1%)
then we can set in the code :
BatteryLife = 100 - ((8.4 - voltage ) / 2.4 * 100)
Now you got your BatteryLife in percentage before your device stop working.


Thank you for your answer! ;)

6 more answers

So you are going to measure your car's battery voltage. I won't say this device doesn't work but I will explain further regarding car's battery. Let's pick a normal family car or SUV which is rated 12V. It is fully charged at around 12.7V. When the engine is running you will get 13.7V - 14.7V, that is the output of the car's alternator.

When your battery dies? When it reaches 11.9V, but this is not telling you about time to replace your battery. You may still charge it with external charger to working voltages, or you may not, there are a lot of possibilities regarding to car's battery such as abnormal discharging.

The range of working voltage of a car battery is 11.9V to 12.7V, so you can calculate it like this :

100% = 12.7 - 11.9 = 0.8V
1% = 0.8 / 100 = 0.008V
Battery percentage = (ReadVoltage - 11.9) / 0.008

Assuming ReadVoltage = 12.4V

Battery percentage = (12.4 - 11.9) / 0.008 = 62.5%

Again, this is not telling you whether you can start your engine or not. Starting up a car needs a lot of amp, not voltage. Checking on the voltage regularly can give you a rough condition of your car's battery and help you in diagnosing a battery problem like if something in the car is discharging your battery when the engine is off.

In certain case you can jump start your car and let it run for 20 mins to charge the battery and if the battery is still good (it can store charges) then no replacement is needed. Someone may have leave the key in ACC and every electronic in the car is discharging the battery. That happens to old car. New family car will warn you with "beeps" when your your key is plugged, the engine is off and the driver's door is open (meaning the driver is leaving the car without the key).

Thank you for the great explanation! ;)

The batteries are not connected to the charger nor will be on vehicle's alternator, it will remain as stand alone power source. Do you know how can I implement periodic check in millis() (read every 10s) to get a rough estimate left over time in minutes until the battery dies?

Kind regards

If it is a standalone power source, then you should measure the voltage when it is fully charged and also when it dies (your project stop working/not working properly). The formula remain the same :

OnePercent = (FullyCharged - BatteryDies) / 100;
BatteryPercentage = (ReadVoltage - BatteryDies) / OnePercent;

The sketch above reads the battery voltage continuously. If you want to read every 10 seconds, the simplest way is adding delay(10000); in loop section rather than complicated but more accurate timer implementation.

You know about millis() then I assume you know how to write simple Arduino sketch just like me ^_^ then I can simply explain the algorithm I guess, because it needs testing phase to write working sketch and I think I don't have that time by now :D

For rough estimate remaining time you can add another parameter storing how long it spends 1 percent of the battery.

int TimeForOnePercent = 0;
float PrevPercentage = 0;

Put this variable out of any procedure or function. Make it global. Then add this to printVolts() function after float voltage = ... :

if (PrevPercentage == 0){ // first time reading.
PrevPercentage = Battery Percentage;
}
else if (PrevPercentage == BatteryPercentage){
// battery has not dropped, add another 10 seconds to record.
TimeForOnePercent += 10;
}
else if (PrevPercentage - BatteryPercentage == 1.00){
// battery drops 1%, time to count the estimate remaining time.
TimeForOnePercent += 10; // 10 secs has passed, we still need to add this.

// EstimateRemainingTime is in second, you can convert it to hour and minute
EstimateRemainingTime = Percentage * TimeForOnePercent;

// Reset the PrevPercentage
PrevPercentage = Battery Percentage;
}

Again you should recalibrate your BatteryDies time. Currently we are talking about car's battery that condidered dies at 11.9V but for other project it may die at any level depends on your project.

It's really a good one, but what about EstimateRemainingTime? Not defined? Need to put also as Global? Is it Int, Long or Double?

You can define it as long because we don't care about millisecond (decimal). You can define it in printVolt() or make it global if you want to use it outside printVolt()

^_^

0
None
HarisM26

Question 5 weeks ago

I have done my simulation on proteus but there is some problem. I'm using two resistor but in the code, multiplying by 3 then i can get the 7.51 v otherwise I'm getting 5v if i put three resistor is proteus.

Can you please help me in this matter?

thanks

Capture1.PNGCapture.PNGCapture3.PNG
1 more answer

The positive post of battery (+) should go to Vin, not 5V. Two resistors are good for 9V.

Thanks for the information using Proteus Simulator. I shall try it too ^_^

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

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

This project do the same purpose to me. I wanted to know if my battery can run my RC toy should I charge it before playing.

Let us say my RC needs at least 6V to run its Arduino and motors and every modules in it. I am using a 7.4V battery that is fully charged to 8.4V. Do some math here :

8.4 - 6 = 2.4 (this is 100%)

2.4 / 100 = 0.024 (this is 1%)

then we can set in the code :

BatteryLife = 100 - ((8.4 - voltage ) / 2.4 * 100)

Now you got your BatteryLife in percentage before your device stop working.

Thank you so much for the post. It is wonderful. However, I have a question, is there any way to give a 15V to Vin without damaging our Arduino. I'm planning to use this circuit with my solar panel battery. Such batteries generally have high currents and voltages, can I still use the same model effectively? Please help! Thank you in Advance :)

3 replies

Do not feed 15V to your Arduino Vin. Arduino can only take up to 12V and its I/O pin can only take up to 5V.
Use a voltage regulator for your Arduino (convert your 15V to 5V) and use voltage divider (4 resistors needed because your 15V could be fully charged up to 18V) before A0 pin because we need to read the real voltage.

I'm extremely sorry for the inconvenience @chienline, but I wanted to ask that, where would I connect the voltage regulator? Shall I connect it after the battery's terminal and right before Vin pin? in that way, our Vin would always read 5V, even if the actual voltage is 15V or more than that.. then how would my Arduino measure the actual voltage? And I'd be grateful if you could tell me.. what changes to do in code accordingly.

All you need is a DC-DC voltage step down module that cost $1 or less. Try to set it up as the picture below. Now your Arduino is powered by the same battery but with acceptable voltage.

If your battery is fully charged at around 18 volts then A0 will read 4.5 volts. You change the code :

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

to

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

because you have divided the voltage into four equals-- that is 4.5V read by your Arduino through A0 pin, then you need to calculate back to get the real voltage that is 18V.

DSC_0061.JPG

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.