Electrical – Calculate battery percentage on LiPo battery

lipo

I have voltage measuring on my LiPo battery that is defined full at 4.2V and empty at 3.2V. This reading is very accurate but I want to convert this to percentage numbers. I know that the percentage of energy left in the LiPo is not linear to the voltage levels, but I would be fine with defining a linear approximation of the percentage left. Something like (4.2V-3.2V)/100 would give me 1% increments.

The issue I'm facing is how would I program this to show the percentage? It seems like ridiculously bad coding to have a switch statement with 100 cases, each for every percentage like the pseudocode under:

Switch(BattVoltage)
case: BattVoltage > 4.19                         "print 100%"
case: BattVoltage < 4.19 && BattVoltage > 4.18   "print 99%"
.
. 
...

How would one do this in a smart manner?

Best Answer

If you already have your voltage readings represented as voltage numbers (i.e. as you have shown 4.2, 3.9 or 3.2) you will want to convert these to integer numbers. Integer math is easily supported on almost all computing scenarios where you are taking the voltage readings. You would start by multiplying the voltage readings by 100 so you have numbers from 420 down to 320. Next you subtract 320 from the measurement value so you have numbers in the range of 100 down to 0. This leaves you with results which will be in direct units of percentage.

On the other hand you may very well be using the A/D converter on some microcontroller to take the voltage readings. A common example for consideration would be a 10-bit converter with a full scale reading of 5V. Such converter gives readings in a range from 1023 down to 0. The A/D value that would correspond to 4.2V would be 862 and for 3.2V would be 656. To convert numbers in this 862 to 656 range you subtract 656 from your A/D reading value to get numbers in the range of 206 down to 0. A simple divide by 2 comes very close to the integer percentage range of 100 to 0 so you may choose to just accept that a result over 100 is same as 100% and call things good. (Note that I would expect that on a real hardware setup that the A/D readings may not be exactly as shown above and you may have to tweak your calculations some. It may be required for example to multiply your A/D reading by 10 so then when you scale to the percentage reading that you can have more flexibility to divide by numbers around 20 rather than dividing by 2 as I suggested above to get closer to a full 100 down to 0 percentage range).

All of this of course is based upon a linear relationship of voltage to battery capacity. That may be good enough for what you are trying to do but will probably not represent the actual remaining capacity of your battery. There are chips available that are able to measure charge going into to the battery when it is being replenished and then measure charge leaving the battery when it is being depleted. Using one of these chips will give you a much better tracking of remaining battery capacity once you characterize the readings from such chip with whatever battery you are using. To learn more search for "battery metering chip" or "battery gas gauge chip".