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:

case: BattVoltage > 4.19                         "print 100%"
case: BattVoltage < 4.19 && BattVoltage > 4.18   "print 99%"

How would one do this in a smart manner?

  • \$\begingroup\$ That's really more of a programming than an electrical engineering problem, and what's smart and/or elegant depends on your programming language of choice, and while I'd usually assume C here, your syntax is nowhere related to C's switch-case statement, soooo I'm afraid this might be either not formulated as the electrical engineering problem that you'd solve on this site, or missing the language tag that it'd need on stackoverflow.com \$\endgroup\$ Apr 28 '19 at 9:28
  • \$\begingroup\$ I know it is a programming issue, but I considered the issue at hand which is a LiPo battery and that it is useful to understand the workings of a LiPo battery to solve this equation. There might even be an equation directly related to the LiPo battery. Regarding the language it isn't important really, only the method by which to solve it. \$\endgroup\$
    – C. K.
    Apr 28 '19 at 9:30
  • \$\begingroup\$ In this case a simple expression like round((Vbat-3.2)*100) would give you a percentage you could print. But be aware that the percentage of charge remaining is not even remotely close to the voltage at the battery terminals. \$\endgroup\$
    – Finbarr
    Apr 28 '19 at 9:33
  • \$\begingroup\$ The SOC/voltage curve for LiIon cells is relatively well defined at a given discharge rate. Battery university provides information on how curves change with discharge rate and temperature. \$\endgroup\$
    – Russell McMahon
    Apr 30 '19 at 1:47

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".


Either you can try to find a mathematical model or (which is mostlikely much more ressources efficient) you implement a lookup table which in the end boils down to something similar to your case thingy but is more elegant code wise. Depending on the used env/language you should find enough ressources explaining the implementation of lookup tables.


According to some empirical values I found on the *net, this seems to be a quite accurate estimation of remaining energy (in percent) of a typical lipo (rated 3.7V, fully charged at 4.2V):

$$123 - {123\over(1 + (v/3.7)^{80})^{0.165}}$$

The least you should do is square the measured voltage, because power is proportional to the square of voltage. This would be accurate if the battery was a linear capacitor, which it unfortunately is not.

  • 3
    \$\begingroup\$ Hi roho, welcome, would be nice if you could cite a source for it \$\endgroup\$
    – diegogmx
    Mar 6 at 22:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.