Help with maths for simple battery meter

JPU

Senior Member
Hi All

I am using a 24V battery and its connected to a PiCAXE 14M2. The battery powers a PCB via a LM7815 AND LM7805 and so I get 5V into the PICAXE. I have also created a voltage divider with R1 22K AND R2 2.2K at 26.5V on the battery I get 2.4V out of my VD and this is then connected to c.0 on the 14M2. I have a LCD connected to pin b.1.


The results aren't as expected and I am struggling to see why. I do know that the voltage measured at the pin is 2.4V when the battery is at 26.5 Any help would be appreciated as all I get is 2.00V displayed and I cant figure out where I am loosing the .4V in the maths.
The code is

Code:
readadc10 c.0,adcvalu
  	
   	
  	
  	sourcev=adcvalu*10
  	
  	touchpad=sourcev/2046*100
  	
    	
  	
  	bintoascii touchpad, b20, b19, b18, b17, b16			
  		
	serout B.1,n2400,("BATT VOLTS:", b20,b19,b18,".", b17, b16)
Its probably simple, like me!

Thanks for your help.
 

Buzby

Senior Member
First question, are your variables bytes or words ?.

Second question, have you read Manual 2, page 22, 'Variables - Mathematics' ?

Consider these two questions, and all will become clear.

Cheers,

Buzby
 

marks

Senior Member
Hi JPU,
your've cleverly done the maths for a ratio of 10:1
with those resistor values 22k and 2.2k your infact have a ratio of 11:1
you can also try using a 100k and 10k to give you the same ratio you wont need any protection diodes.
Code:
readadc10 c.0,adcvalu	
  		
  	touchpad=adcvalu *50 /93  
	
  	bintoascii touchpad,  b18, b17, b16			
  		
	serout B.1,n2400,("BATT VOLTS:", b18, b17,".", b16)
some more examples here
http://www.picaxeforum.co.uk/showthread.php?23377-Voltage-Divders-Made-Easy-!
 

Goeytex

Senior Member
The Picaxe cannot do decimal math, so if you divide 5000 / 2046 the result will be 2 not 2.44.

Here's how I do it with ADC10.

With the ADC range of 0 to 1023 there are 1024 steps from 0 to 5V in
that means each step represents 5 / 1024 or .004882 volts

I like to first scale the ADC to 1000 steps where 0 = 0V and 1000 = 5 volts

This is done by muliplying the input by 44 and then dividing by 45
readadc10 c.0, adcvalu
adcvalue = adcvalu * 44 / 45


Now there are 5mv per step where 0v = 0 and 5v = 1000
if we divide by 2 then an input of 0 = 0 and an input of 5v = 500

So add the last step and we get

readadc10 c.0, adcvalu
adcvalue = adcvalu * 44 / 45 / 2

This can be simplified to
touchpad = adcvalu * 22 / 45

We now have a simple Picaxe Voltmeter with a range from 0 to 5V with 2 decimal places of resolution

2.4 volts should give an initial ADC value of 491 so check the math ...

result = 491 * 22 / 45
result = 240
 
Last edited:

JPU

Senior Member
Hi

Thanks all for your help. Thanks Goeytex for the detailed answer as it really helps to have it explained. I've tried that and it works perfectly.
 

JPU

Senior Member
OK, I have now come against another problem in that when I multiply the result by 11 (VD ratio), I get a result which represents my 24V battery level, however its inaccurate by roughly 0.2v. I assume this is due to the variance in the resistor value accuracy??

To counter this I do the maths which I worked out by supplying exactly 24.96V(my power supply said 25V but the VoltMeter read 24.96V) and measuring the output from the VoltageDivider, (2.281V) and therefore 24.96/2.281 = 10.94

I now assume I need to multiply the result of touchpad*1094=??????. If we take readadc10 to be the max possible value of 1023 we would get

1023 * 22 / 45 * 1094 = 547145 which I know is bigger than is allowed in PICAXE maths but I am only interested in the first 4 digits.

So the question is how to I get the first 4 digits as I have tried touchpad ** 1094 but the result was meaningless. If I am completely up the wrong track then any guidance appreciated.

Many thanks
 

Goeytex

Senior Member
IMO Your best bet is to correct the the voltage divider to a precise 10:1. I would suggest using a pot in the voltage divider so it can be trimmed / calibrated . A 22k Fixed resistor over a 5K multi-turn pot should work OK. Adjust the pot so that the voltage at the center tap is precisely 1/10 of the supply voltage.
 

JPU

Senior Member
Hi Goeytex

Thanks again, yes I agree as I will have to calibrate each one individually and a small POT would probably be quickest in the long run. However for this prototype I have now improved things slightly with the following code. The voltage calculated and displayed on the LCD is within .05V of the actual supply voltage throughout the range of 24V - 29V. Excellent! Thanks again for your help with this.

Code:
readadc10 c.0,adcvalu
  	
  
  	adcvalu=adcvalu*22/45
  	
  	adcvalu=adcvalu*109/10
  	
  	
  	bintoascii adcvalu, b20, b19, b18, b17, b16			
  	
	
	serout B.1,n2400,("BATT VOLT:", b19,b18,".", b17, b16)
 
Top