# Battery measurements

Posted on
• after reading http://forum.espruino.com/conversations/­350764/ , i did some digging and have some questions.

The pin used for voltage reading is pin 30. I used :

``````console.log(analogRead(30))
``````

after it being fully charged and got the value : 0.63110351562

``````/// get battery percentage
JsVarInt jswrap_banglejs_getBattery() {
JsVarFloat v = jshPinAnalog(BAT_PIN_VOLTAGE);
const JsVarFloat vlo = 0.51;
const JsVarFloat vhi = 0.62;
int pc = (v-vlo)*100/(vhi-vlo);
if (pc>100) pc=100;
if (pc<0) pc=0;
return pc;
}
``````

from jswrap_bangle.c. Notice the upper and lower limits hard coded into the formula.
Was this code aimed for a more weared down battery? average use case?
I'm thinking to use analogRead instead of E.getBattery. Is this a good idea?

Edit: I also discovered E.getAnalogVRef() which outputted a number close to 3.3 v
So ... 0.51 * 3.3V = 1.683V
and 0.62 * 3.3V = 2.046V

Why are the battery volt limits for 0-100 between these 2 numbers?

• The temperature of the battery (as well as the load being drawn from it at the time E.getBattery is called) will affect the readings.

It could be that my load is so tiny that these values are not good estimators for me. Thats fine.

• Why are the battery volt limits for 0-100 between these 2 numbers?

Because analog input can take only up to 3.3v and battery is up to 4.2V so there must be some resistor divider to reduce voltage below 3.3V. Resistors were chosen 'randomly' by manufacturer for this design so no reason to multiply by 3.3. Across some fitness trackers I've seen the value varied a lot for each manufacturer/device model.

individual resistor tolerancies can be even few % see e.g. https://www.electronics-tutorials.ws/res­istor/res_2.html , can make some difference in the voltage divider for each device

Application Dont's
As tempting as it may be to use a voltage divider to step down, say, a 12V power supply to 5V, voltage dividers should not be used to supply power to a load.

Any current that the load requires is also going to have to run through R1. The current and voltage across R1 produce power, which is dissipated in the form of heat. If that power exceeds the rating of the resistor (usually between ⅛W and 1W), the heat begins to become a major problem, potentially melting the poor resistor.

That doesn't even mention how inefficient a voltage-divider-power-supply would be. Basically, don't use a voltage divider as a voltage supply for anything that requires even a modest amount of power. If you need to drop down a voltage to use it as a power supply, look into voltage regulators or switching supplies.

• this is unrelated to this case, nothing is powered via resistor divider here, it is just input to analog pin

• I worked out the values when developing based on just running a watch with the screen and GPS on until it went flat, and logging the values received. As @fanoush says lots of things can change the reported voltage by a little bit.

You could in fact write your own replacement `getBattery` implementation in JS if you want to (`Bangle.getBattery = function() ...`)

IMO the thing that's the issue right now is not the values used, but the fact that the battery voltage does not go down linearly (google LiPo discharge curve for some examples) and yet getBattery just does a linear mapping