ACS712 Current Sensor Help

1968neil

Senior Member
Hi Folks,

This is driving me mad, can anyone tell me where im going wrong ?
I cant get an accurate reading across the whole range.

I am using an 18m2+ with The Current sensor on B.3 ADC input.
I think i have the maths right ?

All im trying to do is measure the current of a dc source from 10ma to 20 amps the output of the module(ebay version on PCB with header) and display on an AXE133 the display is fine and displays ok.
What i cant get my head round is if i drawer 270ma i get a reading of 3 and a 3.7amp gives me 37 on the lcd.
Has to be the maths ?
Any help greatly received.
Neil
Code:
Symbol ACS712=B.3
Symbol LCD=B.5
Symbol Baud=N2400

Pause 2000                      ' Pause 2 secs to initialise LCD

serout LCD,Baud,(254,1)         ' Clear LCD before starting Main Program
pause 1000                      ' Pause 1 sec


Main:
b0 = b0 MAX 127
READADC ACS712,b0
b1=b0-127*196/100

'Pause 800


serout LCD,Baud,(254,130,#b1)


Debug

Goto Main
 

hippy

Technical Support
Staff member
What values were you expecting, and what values are you seeing in b0 and b1 ?

What's the "b0 = b0 Max 127" doing ? Why is it there ?

What happens with trailing zeroes on the display ?
 

AllyCat

Senior Member
measure the current of a dc source from 10ma to 20 amps
Hi Neil,

That's a range of 2,000:1 and the PIC(axe) A/D converters only resolve to 1 part in 1,000 (10 bits). And you're not even using the "higher resolution" readadc10 command (into a word variable), so you're only resolving to a single byte (with a maximum of 1 part in 256).

It appears that you're working with a scale factor of 100mA/bit, so 270mA is rounded up to '3' (300mA) and 3.7A reads as '37', which IMHO are both correct.

If you really need a range of more than 1,000:1, then you either need better hardware (a different micro or a PICaxe with an external A/D converter), or I have a few (untried) ideas how one might use "dither" to increase the resolution (NOT the accuracy) of the PICaxe A/D converters.

Cheers, Alan.
 

1968neil

Senior Member
@ Hippy,
The Max 127 is there to stop the negative current readings, not interested in them at present.
I have tried to use the readadc10 command (for the 1st time) and ive attached my attempt below.
I must be getting old ! Think i have the maths right,

Step is 5v/1024
resolution is 100mv/Amp
The module outputs 2.54v at zero amps

Am i thinking about this right ?
Also how would i use a decimal point to get the 0.00A style reading ?

Regards
Neil
Code:
Symbol ACS712=B.3               ' Input from ACS712 
Symbol LCD=B.5                  ' Output to Axe133 LCD
Symbol Baud=N2400               ' Baud Rate for Axe133

'*************************************************************************


Symbol ADCValue=W0
Symbol Result=w1                ' Word variable for 1st read of ACS712 

Symbol Offset=512               ' 512=0 Amps half supply
Symbol Steps=4882               ' 5v/1024 = 0.000488vDC
Symbol Sensitivity=100          ' Sensitivity is 100mv/A
'*************************************************************************
Initialise:

Pause 2000                      ' Pause 2 secs to initialise LCD
serout LCD,Baud,(254,1)         ' Clear LCD before starting Main Program
pause 1000                      ' Pause 1 sec

'*************************************************************************
'*       Main Program to read results from ACS712 Current Sensor         *
'*                                                                       *
'*************************************************************************

Main:


READADC10 ACS712,ADCValue
pause 100
Result=ADCValue-Offset*Steps/Sensitivity
pause 100
serout LCD,Baud,(254,130,#Result)

Debug

Goto Main
 
Last edited:

hippy

Technical Support
Staff member
To ignore negatives and stop there being underflow, you need ...

Result = ADCValue Min Offset - Offset * Steps / Sensitivity

Also, because the ADCValue-Offset can be 0 to 511, multiplied by 4882 (Steps), that can create an overflow condition. You either need to limit the value being multiplied ....

Result = ADCValue Min Offset - Offset Max 13 * Steps / Sensitivity

Or do the maths differently to allow a larger range of values to be handled correctly.
 

Armp

Senior Member
Or do the maths differently to allow a larger range of values to be handled correctly.
I have used this "20A" board.

ADC reading for 0 amp is 512, for 20A its 922. So scale factor is (922-512) / 20 counts per Amp

so AMPS = ADCValue - Offset /410 *20

Better to multiply first:

AMPS = ADCValue - Offset * 20 /410

Or AMPS = ADCValue - Offset * 2 /41

And multiply by 10 to get one decimal place:

AMPS = ADCValue - Offset * 20 /41

Print it in 2 parts using AMPS/10 and AMPS//10


A word of warning: do NOT pass more that about 5 amps DC through the pcb - it WILL smoke! The 20A rating if for something like 1% duty cycle.
 

premelec

Senior Member
@Armp - thanks for the warning - I am thinking of using some of these - could you just solder a couple of 10ga wire to the terminal block to keep the pcb smoke down?
 

Armp

Senior Member
@Armp - thanks for the warning - I am thinking of using some of these - could you just solder a couple of 10ga wire to the terminal block to keep the pcb smoke down?
Basically yes, but copper braid/solder wick is probably easier to work with - I'll let 'mrburnette' provide details of how he did it.

Edit - he already did! http://www.picaxeforum.co.uk/showthread.php?23093-Need-help-choosing-right-op-amp-for-shunt-resistor/page2

But note that in "the internal IC conductor is 0.0012 mOhm, the total resistance including the cheap screw-down connectors and the PC foil made the overall resistance more like 0.006+ mOhm" they should be ohms, not mohm.
P=I*I*R so 20x20x0.006 so that's 2400mW on the card.
 
Last edited:

premelec

Senior Member
OK the IC is evidently good for .48 watts and the additional watts are in the pcb and connectors. I still wonder about MOSFETs in TO220 form that supposedly take 60 amps... :) [BTW 2.5 watts on that card likely would produce more smell than smoke - still bad practice! Did you actually get one to burn up? or heat enough to desolder the terminal strip... ]
 

Armp

Senior Member
[BTW 2.5 watts on that card likely would produce more smell than smoke - still bad practice! Did you actually get one to burn up? or heat enough to desolder the terminal strip... ]
No - but the card gets bloody hot, even at 5A, therefore not a good idea! There are also 2 plated through holes to carry the current from the bottom side of the pcb to the IC that won't last long at 20A IMHO.
 

1968neil

Senior Member
Back to this some time later (8 years !)

Can anyone assist with some simple code ?
I have a standard 9v regulator that i need to monitor current on and i have gone for ACS712 as i have quite a few old ones unused laying around in my parts draw.
I need to measure the current to around a amp as accurately as possible.......
Been a while since i did much in the way of software..... Need to re-stimulate the grey matter again :)
Any help much appreciated :)
Using a 40X2
Regards
Neil
 

premelec

Senior Member
well... the unit is a V+/- and out pin module... you might start measuring the output with 1 amp running through the current loop and the module powered... then note that you can use READADC command on PICAXE pin connected to output... what have you tried??
 

1968neil

Senior Member
Hi Premelec,

If you look at the top of this thread, the code i am using is pretty much the same as that, ive returned to it almost 8 years later and ive done little programming in between.....
Ideally i need to know what value to put in the Offset -Steps and sensitivity fields so i can read the current passing thru a 7809 Regulator.
from the data sheet my module is 185mv output per ampere if that helps.
This is one of those projects that i wish id never started !! :)

Regards
Neil
 

AllyCat

Senior Member
Hi,
I need to measure the current to around a amp as accurately as possible.......
There are listings in both #1 and #4, but I haven't bothered to check the difference. Also, it's not very significant where the current is flowing from (since the main benefit of the ACS712 is that it gives full isolation) but it is important what voltage is being supplied to the PICaxe and to the sensor. Are they both being fed by the same 7805 ? It appears that the output from the current sensor is "ratiometric", so the PICaxe ADC needs to be operated in the same mode and ideally from the same supply rail. That means that the resolution of the READADC10 command will be just under 5 mV (theoretically 5000 / 1.024 = 4.88 mV steps).

The nominal 185 mV/Amp represents 5.405 mA / mV and thus the resolution of the overall measurement should be 5 * 5.4 = 26.4 mA/mV approximately. Not very "accurate", particularly as you need to take into account the half-supply offset (nominally 512); perhaps an overall result of "Give or Take 50 mA". Personally, I'd probably make the measurement across a 1 ohm resistor in series with the input to the 7809 regulator, but it's not a trivial design. Anyway, the simple calculation is:
Code:
READADC10 ACS712 , ADCValue
Result = ADCValue MIN 512 - 512 * 26     ; Avoid negative value and scale up
SEROUTLCD , Baud , (254 , 130 , #Result , " mA   ")
You might be able to improve the resolution a little by accumulating (i.e. adding together) two or three READADC10 values and then multiplying by 13 or 9 respectively.

Cheers. Alan.
 
Last edited:

1968neil

Senior Member
Thanks Alan,
I'll give it a go when i get back to this project, Time is always a problem nowadays :)
Thanks for you help, much appreciated

Kind Regards
Neil
 
Top