A quick battery level percentage

oracacle

Senior Member
I was thinking about a battery level meter as well which would need a constant voltage to compare against. The project will hopefully be powered by 3AA batteries, so my initial thought was 3.3v regulator as a reference point (I know the 20x2 doesn't have external reference, or easy access internal FVR) and then compare the battery voltage to that I do some maths (that I haven't stopped to think about as yet) to get battery level. This seemed like a plan before I read the calibadc command.

I have not played with it as yet, but the end sum of Vpsu = 255/Nref, meaning that the battery level, in theory at least, could be obtained with no extra components providing a default is programmed to start with. you could even just compare the Nref to the programmed default I suppose. 1% battery charge would be equal to 2.55, battery level = Nref/2.55. multiply out the decimal Nref*100/255 would give the battery level in percent. I have a bit of search around but and there are some thread showing voltage to 2 decimal places having a percentage is a little more helpful when in the field.

I can foresee that system not working below a certain level that isn't 0% being a problem which will need to be solved, but maybe a case of Nref-155=Battery level percentage, the 155 may vary depending on when thing start misbehaving. the final circuit maybe be able to control its own power to stop unreducible results as the project uses a comparator interrupt... That's a point, how much of an effect will calibADC have on the interrupt and the IVR?

This is more to make sure that my thing is correct more than anything else, I don't need anything super accurate. and make sure I don't too nut when thing don't work as expected when I go and experiment
 

westaust55

Moderator
You would want to select a voltage regulator with a low quiescent current. Some “standard” type can be round 5 mA quiescent current.
An LP2950 series voltage regulator with < 1mA load current would have a quiescent current of the order of 0.1 mA

A scheme I used on one occasion in the past with the older 14M parts was just a 10 kOhm resistor and IN4148 signal diode across the power source. So a current draw of around 0.4 mA from the power source and a diode VF of approx. 0.6 Volts.
Could use a higher series resistor to further limit the current through the resistor and diode.
By &#8220;measuring&#8221; the forward voltage across the diode with an ADC input I could determine the PICAXE supply voltage. As the PICAXE supply voltage falls, the ADC value for the voltage across the diode will rise.
 

AllyCat

Senior Member
Hi,

If by "battery level" you mean the "PICaxe supply voltage", then CALIBADC10 is the way to go. AFAIK it works on all PICAXEs except some of the "Narrow supply voltage range" X2s and a few very old devices. It will give a resolution to about 20 mV, which is better than 1%. The FVR is "officially" (data sheet) only accurate to around 5% but in practice it is (much) more precise. Anyway, it's easy to calibrate the program against a DVM if required.

A percentage value can be calculated if you know the "100%" voltage, or just calibrate the program constant against a "fresh" battery. I (and also "marks") have written several Code Snippets on the topic, but it appears that the "tricks" which I use for much higher resolution (sub 10mV) can't be used with the X2 chips, only with all of the M2s.

Cheers, Alan.
 

hippy

Technical Support
Staff member
Code:
#Picaxe 20X2
#Terminal 9600
#No_Data
#No_Table

Symbol Vpsu100 = 5000 ; 5000mV = 100%
Symbol Vpsu0   = 4000 ; 4000mV = 0%

Symbol Nref100 = 1047552 / Vpsu100 ; 1024 * 1023 / Vpsu100
Symbol Nref0   = 1047552 / Vpsu0   ; 1024 * 1023 / Vpsu0

Symbol NrefDif = Nref0 - Nref100

Symbol Nref    = w0 ; b1:b0
Symbol Vpsu    = w1 ; b3:b2
Symbol percent = b4

Do
  CalibAdc10 Nref
  percent = Nref Min Nref100 - Nref100 * 100 / NrefDif Max 100
  percent = 100 - percent
  Vpsu = 52378 / Nref * 2
  SerTxd( #Vpsu, "0mV", TAB, #percent, "%", CR, LF )
  Pause 1000 
Loop
 

oracacle

Senior Member
Thanks guys.
AllyCat, I think its going to more of an amount over a given voltage. So if the lowest effective voltage is 3.3v and 3xAA batteries give around 4.5v 12mv wold be around 1%, but that sort of resolution isn't needed. I would be more than happy with 5% resolution.

Hippy I will have a look at that code and see if I can be implemented in the project.
 

newplumber

Senior Member
Hi not to interfere with this post but
westaust55 can you explain "As the PICAXE supply voltage falls, the ADC value for the voltage across the diode will rise"
why that is ... or is it complicated ... I guess I am thinking the adc value should fall too
 

hippy

Technical Support
Staff member
can you explain "As the PICAXE supply voltage falls, the ADC value for the voltage across the diode will rise" why that is ...
Let's assume a 3.3V reference voltage; the PICAXE will read a value of 0 to 255 which represents what proportion of the supply voltage that input represents.

If the PICAXE were run from a 6.6V supply, then reading a 3.3V reference voltage input is half the supply voltage so it should return a 127 value.

If the PICAXE is run from a 3.3V supply, then reading that same 3.3V input matches the full supply voltage so it should return a 255 value.

So, we can see the READADC reading of a fixed voltage reference the reading increases as the power supply voltage falls, decreases as the power supply rises.

An alternative perspective is; an item of a fixed height will seem to get shorter to an observer as the observer grows in height.
 

newplumber

Senior Member
Thanks Hippy for the explanation ... I do understand some what of your simple examples but
maybe if I am following right ... I am thinking of a teeter totter and the fixed voltage reference is the middle triangle
and the supply voltage is one end of the plank and readadc is on the opposite end there for once a person has set
fixed voltage point the readadc will always read higher as the supply voltage drops or am I like always ....confusing my self :)
I looked at your code but for how short it is has lots of crunching numbers so again if I was to use your code in post #4 and
any supply voltage above 5 volts would read 100% i.e. supply voltage 5.5 = 100% 6 volts = 100% so on
Sorry Oracacle lol but you have friendly short bus student (ME) trying to learn on your post :)
 

hippy

Technical Support
Staff member
Not sure if 'teeter totter' would be the a correct analogy, 'seesaw' in the UK, but it is correct in so far as supply voltage goes up, the read value of a fixed reference voltage goes down.

Yes, anything above 5V (Vpsu100) would be read as 100% in my code. One would need to adjust Vpsu100 to 6000 to show 6V as 100% and anything below as less.

I'm not sure what the maths would need to be to have 5V as 100% and voltages above that as a higher percentage.
 

newplumber

Senior Member
Okay thanks I will later on do some tests and try to make sense out of it ....
one thing I will do is use my variable power supply so I can speed up my battery draining simulation faster
 

hippy

Technical Support
Staff member
This code allows a 0% and 100% battery voltage to be selected, and shows above 100% when the battery is 'over charged'.

This example sets 3V3 as 0% and 4V5 as 100%, which means 5V7 is 200%.

It seems to work, tests correctly at extremes, but shows 100% at 4V2 rather than 4V5 when tested with a physical 20X2. Not quite sure why that is, whether it's just a rounding error in the maths or it's a linearity error.

Code:
#Picaxe 20X2
#Terminal 9600
#No_Data
#No_Table

Symbol Vpsu100 = 4500 ; 4500mV = 100%
Symbol Vpsu0   = 3300 ; 3300mV = 0%

Symbol VpsuDif = Vpsu100 - Vpsu0
Symbol Vpsu200 = Vpsu100 + VpsuDif

Symbol Nref200 = 1047552 / Vpsu200 ; 1024 * 1023 / Vpsu200
Symbol Nref0   = 1047552 / Vpsu0   ; 1024 * 1023 / Vpsu0

Symbol NrefDif = Nref0 - Nref200

Symbol Nref    = w0 ; b1:b0
Symbol Vpsu    = w1 ; b3:b2
Symbol percent = b4

Do
  CalibAdc10 Nref
  percent = Nref Min Nref200 - Nref200 * 200 / NrefDif Max 200
  percent = 200 - percent
  Vpsu = 52378 / Nref * 20
  SerTxd( #Vpsu, "mV", TAB, #percent, "%", CR, LF )
  Pause 1000 
Loop
 

newplumber

Senior Member
Thats cool ... well the reason it shows 100% at 4.2 rather then 4.5 is because... ( i wish i was smart)
I think it would be okay for me because a person could just use average of highest percentage when full charged then
see over time if it climbs or minuses which is very useful data for a battery.
 
Top