Math help

lewisg

Senior Member
I have been working on a UPS tester using a CS-50A Hall-Effect Current Sensor from panucatt.com. The sensor is working well and I can compute observed current measurements is a spreadsheet. However the math is throwing me.

This works in a spreadsheet: current = ((rawCurrent*(5/1024))-(5/2))/(4/100)

How can I compute this on a PICAXE?

Code:
#PICAXE08M2

symbol rawCur  = w0
symbol rawVolt = w1
symbol current = w3
symbol voltage = w4

symbol logger  = C.1
symbol LCDpin  = C.0
symbol cSense  = C.2
symbol vSense  = C.4

start:
    pause 500
    low LCDpin
    high logger
    pause 100
    serout logger,t2400,("time", 9, "rawCur", 9, "rawVolt", 13,10)

main:

    readadc10 cSense, rawCur
    readadc10 vSense, rawVolt

'   current = ((rawCur*(5/1024))-(5/2))/(4/100)

    serout LCDpin, n2400,(12, "time:",#time)
    serout LCDpin, n2400,(16,80, "c:",#rawCur," v:", #rawVolt)

    serout logger,t2400,(#time, 9, #rawCur, 9, #rawVolt, 13,10)

    pause 5000

goto main
(BTW(I love (parentheses)!))
 

westaust55

Moderator
Firstly you can simplify the forumula somewhat to:

current = (rawcurrent * 125 / 1024) - 62.5

If you can then accept some error (around 0.08 Amps at full scale) we can change this to:

current = (rawcurrent * 61 / 500) - 62.5

Now as the max value of raw current from the READADC10 is 1023 multiplying by 61 will not introduce overflow.

so try with:

current = (rawcurrent * 61 / 50) - 625

and the answer is 10 times the actual value within the mentioned error.

so whereas a READADC10 input of 1023 should give 62.3779
you will have 623 which equates to 62.3


If you have a negative current the READADC10 value will be approx <= 512
in which case you need to adjust accordingly and use a sign variable which would be " " or "+" for positive and "-" for negative.


trust that gives you sufficient clues to work it out for yourself.
 
Last edited:

marks

Senior Member
hi lewisg,
really this similiar to above
Code:
w0 = w0-512                           'minus 2.5v
IF w0 > 65120  THEN :  w0=-w0 : ENDIF 'if negative
w0 = w0 * 122/100                     'info w0=500 (50.0 amps)
this device does look interesting
just one note voltage sense seems to be connected directly to the supply that your current device is connected hope its not greater than 5 volts lol
hopefully youll let us all know how well these devices work.
 

cravenhaven

Senior Member
In these devices the measured circuit is completely isolated from the measuring circuit. The connection between the two is electromagnetic and sensed by a hall effect sensor which runs off a 0-5V supply. I am using a couple of the lower amperage versions of these for my camper battery monitoring project to monitor battery drain and charge respectively. They are really quite accurate though a bit difficult to connect too.
 

Svejk

Senior Member
Maybe is worth mentioning that the equation is copied wrong from the datasheet. The actual one says:

Current = (analogRead(1)*(5.00/1024))-2.5)/.02;

Using 5.12 V as reference would simplify the maths.
 
Last edited:

lewisg

Senior Member
Thanks! I have to think a bit more on the math.

From my first stab marks code seems to give a result closer to my earlier calculations. The result does seem to be x10 the true value. Please forgive me but my math skills are not up to par. If it wasn't for spreadsheets I'd never be able to accomplish anything!

Here is the current test code for this part of the system:
Code:
#PICAXE08M2

symbol rawCur  = w0
symbol rawVolt = w1
symbol current = w3
symbol voltage = w4

symbol logger  = C.0
symbol LCDpin  = C.1
symbol cSense  = C.2
symbol vSense  = C.4


pause 500
low LCDpin
high logger
pause 100
serout logger,t2400,("time", 9, "rawCur", 9, "rawVolt", 13,10)

main:

    readadc10 cSense, rawCur
    readadc10 vSense, rawVolt


'   works in Excel:
'   current = ((rawCur*(5/1024))-(5/2))/(4/100)

'   westaus55 suggestion:
'   current = (rawcurrent * 61 / 50) - 625

'   my interpretation of westaus55 suggestion:
'   current = rawCur * 61 / 50
'   current = current - 625
'   current = current / 10

'   mikes suggestion:
'   w0 = w0-512                           'minus 2.5v
'   IF w0 > 65120  THEN :  w0=-w0 : ENDIF 'if negative
'   w0 = w0 * 122/100                     'info w0=500 (50.0 amps)

'   my interpretation of mikes suggestion:
    current = rawCur - 511
    if current > 65120 then
        current = -current
    endif
    current = current * 122/100


    serout LCDpin, n2400,(12, "time:",#time)
    serout LCDpin, n2400,(16,80, "c:",#rawCur, ":", #current, " v:", #rawVolt)

    serout logger,t2400,(#time, 9, #rawCur, 9, #rawvolt, 13,10)

    debug

    pause 5000

goto main
What has been holding me up is noise on the sensor lines. During charge this is fairly minimal, around .113 VDC but during discharge it gets big:
SensorNoCaps.jpg
Adding a 100uF electrolytic between the Vout of the sensor and ground and a 47uF between the voltage divider tap and ground cleaned things up:
SensorWithCaps.jpg

Due to my mathlexia I ruled out EE school. Is there a better way to handle this sort of noise problem?

A few other things:

** While the current is isolated measuring voltage is not. All I know to do is a voltage divider. Is there something better?

** I'm using the 50 amp version. The .02 value on the second page of the data sheet is for the 100 amp version.

** Connecting any device in good sized current paths can be tricky. If this part of the project works out these current/voltage sensors will get Anderson Power Pole connectors. If you haven't used these wonderful genderless connectors you are likely working too hard!


Thanks again for all your help!
 

hippy

Ex-Staff (retired)
Firstly you can simplify the forumula somewhat to:

current = (rawcurrent * 125 / 1024) - 62.5
That 2.5/0.04 = 62.5 comes about because the zero current level is at 2.5V, half the ADC reading at 5V so it could be better simplified to ....

= ( READADC - 512 ) * 125 / 1024

And rather than divide by 1024, divide by 65536 (64*1024), and multiply the top by 64 ...

= ( READADC - 512 ) * 125 * 64 / 65536

= ( READADC - 512 ) * 8000 / 65536

Then that's easy with PICAXE math ...

ReadAdc <pin>, w0
w0 = w0 - 512 ** 8000
SerTxd( "Current = ", #w0, "A")

Handling negative currents would need to be added but the same maths would apply.
 

westaust55

Moderator
Maybe is worth mentioning that the equation is copied wrong from the datasheet.
The actual one says:

Current = (analogRead(1)*(5.00/1024))-2.5)/.02;
As lewisg has indicated in post 1 that he has a CS-50A current sensor the correct sensitivity and divisor is 0.04 as was stated in post 1.

The 0.02 sensitivity figure to be used as the divisor applies when the CS-100A current sensor module is used
 

marks

Senior Member
Thought i,d have some fun with numbers
had a go at improving the code a bit i wish i had the hardware
may hav to get one of these to try lol dont hav the 08m2 either so hope this works
anyway hope you give it a try!
Code:
#PICAXE08M2
symbol RawCur   = w0
symbol Current  = w3  : symbol AMPS = B7 : symbol AMPSdecimal = B6
symbol Sign     = b9
symbol Rounding = b10
symbol cSense   = C.2

main:
readadc10 cSense, RawCur 

LET Current = RawCur

Current = Current - 512 : Sign = " "                                                       'minus 2.5v (zero sensor)
IF Current > 65120  THEN :  Current = - Current : Sign = "-" :  ENDIF                      'if negative

Current = Current * 125 / 4                                                                'info b7 = 50 (50.0 Amps) b6 = decimal
AMPSdecimal = AMPSdecimal  * 5 / 4 * 5 / 16                                                'improve accuracy of our decimal
 let Rounding = AMPSdecimal // 10 :  IF Rounding > 4 then : Current = Current + 10 :endif  : AMPSdecimal=AMPSdecimal/10 'round up (.05-.09)

sertxd ("C Sense  " ,#RawCur,"   Current is  ",Sign,#AMPS,".",#AMPSdecimal," A",CR,LF)

 pause 1000
goto main
 
Top