battery life/lcd


I need the battery life of my robot shown onto an lcd screen.
Anybody knows a link to a good code to do it or wants to help me out with this?

Thanks in advance!

What type of battery are you using?

Lipo cells have a good correspondence between state of charge and their voltage.

If you want to measure the used capacity (e.g. mAh used) then you will need to measure the current and integrate with time.

If you want a warning that the battery is low then a simpler voltage monitor technique might suffice.

LiPo 3.7V batteries + measure the used capacity (I want to display a 1-100% scale to keep track of the power available).

I've done the lcd template using Nextion editor.

OK, I suggest you use a simple voltage monitor and use this to estimate the remaining capacity in the battery. This is much simpler than using a current sensor, otherwise you would need to sample the current regularly at and integrate (sum) the current with time to calculate used capacity.

As a simple linear approximation you could assume the cell voltage in the range 4.1V to 3.7V represents the capacity range 100 to 20%.

Ok thank you!
And do you have any example of code I could use to do that?

Full charge voltage: more than 4.1V; shutdown device at 3.0~3.2V.
Set ADC reference to internal reference (1.1V):


but note that internal reference has a tolerance of +/-10%, then you will have to measure the reference voltage on the REF pin (not connected or connected only to a 100nF capacitor) with a multimeter (also a cheap one) and adjust scale factor in your sketch.

You have to connect battery + to analog input by a voltage divider made of two resistors, i.e. 2.7Mohm and 1Mohm, so that a fully charged battery gives a voltage less or equal to 1.1V (but it can't damage anything, because resistors are very high), then read the value by analogRead().


Please search for JOS Menu on the web, you will find a code there, in which he has designed a very good battery life with display.