battery percentage correction

Dear Team,

Good Morning to all .

i have one question and need suggestion .

i dont required ready code but example or reference is fine for me because i want to learn and want to explore myself on it.

My question is im using arduino UNO with lcd display and want to display battery percentage on display.

I am using 5v battery want to display 0-100% level .

lowest point is 3.7v and highest point is 4.2v for displaying 0-100%.

digital value of 3.7v is 754 and 4.2v is 856. which i got from arduino when i connected my battery.

i did mapping with all this value. percent=map(voltReading,754,856,0,100);

and display the same its showing me result at display but problem its keeping varying very fast .

how can i make it gradually increasing and decreasing as per charging .

ex:how our mobile shows battery percentage value stable it gradually increase or decrease.

I hope i have made everyone understood , sorry if i have not done it well.

Looking for advice and suggestion.

Thanks ahve a wonderful day ahead.

1 Like

Varying very fast would usually be a hardware problem. However understanding whether the software is a problem is not possible unless you post your code.

how can i make it gradually increasing and decreasing as per charging .

Part of what your phone displays is a fiction. It makes a guess at what’s in the battery. That’s why it can suddenly jump from 20% to 0% without warning.

But what it’s really doing is counting the electrons going in and out of the battery. This is much more accurate than measuring voltage. You can buy battery-management chips which do this, pre-built modules or you can make your own with a current sensor.

Usually the best way to do it is to display the measured voltage and let the person looking at the display make their own estimate of the battery from that.

Thanks MorganS and aarg for suggestion.

i will post my code .

Is the battery whose percentage you want to measure also the battery that is powering your Arduino?


Thanks robin reply.

Yes im using same battery.

@ aarg - i will share the code very soon.

Yes im using same battery.

Are you properly taking account of the fact that the 100% as seen by the Arduino changes as the battery voltage falls?

It's explained in this Thread


Sorry Robin i went through the document but didnt understand it as im new into coding.

Suppose that you are measuring the amount of water coming out of a pipe. The ONLY thing you know is the amount of water coming out of the pipe now. How are you going to determine whether that is high (100%) or low (10%)?

The point is that you can measure the voltage of a battery and compare that to the voltage of the Arduino in a meaningful way ONLY if the Arduino voltage is constant, which will NOT be the case if the battery being measured is the one powering the Arduino.

The battery voltage measured will ALWAYS be 100% of the voltage powering the Arduino. Right up until the brown out detector shuts the Arduino off because the voltage is too low.

Thanks dear PaulS,

As i said i taken digital voltage output from arduino via battery.

with that reference im using digital value of 3.7v and 4.2v in mapping.

that is percent=map(voltageout,754,856,0,100);

and i am get it fine at output.

im struggling with percent varies continously like 85% 86% 85% 86% on display.

because when i seen digital voltage output that is also varies continously like 790 780 790 780.

how to make this value look stable on lcd display is challenge im facing.

with that reference im using digital value of 3.7v and 4.2v in mapping.

You are not getting it. If the absolute battery voltage, as measured with a DMM, is 4.1, and the Arduino is being powered by a separate 5.0V power supply, then the ratio between the voltage of the Arduino (5.0V) and the battery (4.1V) can be determined.

If the battery is also powering the Arduino, then the ratio between the voltage measured at the analog pin to the voltage at the VIN pin will be 1.0000000000000000000. The analogRead() value is 1023 times that ratio.

If you really are seeing different numbers, you MUST post a schematic showing EVERYTHING connected to the Arduino.

Sorry Robin i went through the document but didnt understand it as im new into coding.

First, I agree 100% with what @PaulS has said.

The way the code in the Thread I linked works is to turn the voltage reading on its head.

Inside the Arduino there is a 1.1v voltage reference which is independent of the power supply voltage.

By using the battery voltage (which varies) as the reference to “measure” the internal 1.1v voltage (which does not vary) the apparent variation in the value of the internal voltage can be used to infer the battery voltage that provided the reference for the measurement.

It can take a bit of time to get your head around the idea.


Thanks PaulS and Robin,

let me try whatever you said i will revert it back after i test it very soon.

Hello Everyone,

Sorry for late reply as i was out of town wont able to reply on it.

Coming on point as PaulS said i checked.

I am using battery of 4.2v and giving to analog input and one small ready made booster circuit i am using to power up arduino by 5v from 4.2v to 5v.

yes i am using same battery to power up arduino and analog input.

is it a reason i am not getting stable output as battery percentage ?.

waiting for valuable reply ..

waiting for valuable reply ..

You need to make a pencil drawing showing how everything is connected and then post a photo of the drawing.

Also post a link to the datasheet for the voltage booster you are using.


Thanks Robin for your reply.

Sorry its ready made voltage booster taken from mobile charger .

please find the attached schematic.

I hope I have done the right connection.

ardunio ckt.PNG

Can any tell me how to solve this issue.

Image from Reply #15 so we don’t have to download it. See this Image Guide



Can any tell me how to solve this issue.

Bumping your post after an hour is very unreasonable. After 48 hours would be reasonable.

With the circuit you have provided the ordinary analogRead() should be fine.

However you need to take account of the fact that the range of voltage between fully charged and discharged is very small - maybe 4.0v to 3.5v or about 818 to 716 on the 0-1023 scale used by the ADC. The actual values will depend on the exact voltage produced by your step-up circuit.


Sorry robin I didn’t get you what I need to do I didn’t understand .

Sorry for inconvenience .

yes range is small 3.7 to 4.2 v .