PIC 16f877 A/D conversion Allegro ACS712


Closed Thread
Results 1 to 33 of 33

Hybrid View

  1. #1
    Join Date
    Jan 2008
    Location
    Pennsylvania
    Posts
    113

    Default PIC 16f877 A/D conversion Allegro ACS712

    Hello all... I posted some threads a while back and got some help with the 16F822 and it's A/D conversion process. I worked through the math with a pressure transducer and had thought I worked out the math with the Allegro current sensor ACS712 but I'm pretty sure I was originially wrong on that and now just want to get a second opinion or 2 to see if I finally got it. If anyone has any comments please let me know.

    I did find one thread dealing with what I am pretty sure is the ACS712 perhaps a typo on the name or it was called that before I started to use them. In this thread they do math on this device but I think the outcome of this math is going to be a little bit off if they are truly talking about the ACS712. It turns out the company I work for does allot of business with Allegro and the were more then happy to help me out. I did review the data sheet on the Acs712 but initially didn't spot the output voltage until just yesterday. I emailed them and they confirmed the output range of the 712 is 2Vdc which is 2.5Vdc to 4.5Vdc

    So my question/observation is using an 8bit number by my calculations the multiplier of the value returned by the pic would be .02272. From the PIC for a 0 to 30 amp reading from the 712 I could expect to see 128 to 216 which gives me 88 to work with divide 2/88 and get .02272 thus my calculation is
    amps = reading * 2272
    amps = DIV32 1000

    Did I get this right or is my math still off? Hope thats not to long winded.
    Thanks
    David

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    DavyJones ...
    Sorry ... but that one gets sent to the "Locker".

    Yup, we were talking about an ACS712 from Allegro in that thread.
    But, hell_pk had also specified 10-bit A/D with a 2.61V VREF-.
    You seem to be using 8-bit A/D without a reference, so yes, things are going to be different ...

    With VREF+ = 5V, and VREF- = 0V, at 8-bit ... each A/D step is 0.0196V. (5 / 255)

    The ACS712 idles at 2.5V, and with 8-bit resolution that's an A/D reading of 127.

    And assuming you are using the default 66mv/A sensitivity, the formula looks like this ...

    (ADvalue - 127) * 196 / 66

    Which should give 1 decimal of precision ... 10.0amps = 100
    With quite a bit of integer truncation due to the 8-bit resolution.<hr>
    ADD:
    That only works with positive currents, so to make sure it never sees negative numbers, you can add this before the formula to limit the A/D values ...

    ADvalue = ADvalue MAX 127

    hth,
    Last edited by Darrel Taylor; - 2nd August 2009 at 07:20. Reason: ADD:
    DT

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Here is a longer way of looking at what is happening. Not as nice as Darrel's but it may help you understand a bit. This works for sensors that do not start on zero.

    The old formula of
    y = mx-b
    y = output 'amps in your case
    m = slope
    x = input units
    b = offset

    You are wanting to read form 0 to 30 amps.

    Start off by looking at the units as being volts. You have a 2 volt span. This means that every 1 volt input will equal 15 amps. Slope will equal 30 / 2 = 15..
    m = 15

    The offset. Starting at 2.5 volts. 2.5 volts = 0 amps. the offset is 2.5 * 15 = 37.5
    b = 37.5

    Plug all this into the formula and lets say the input is 4.5 volts.
    y = (15 * 4.5) - 37.5
    y = 30 amps

    Now convert this to an 8 bit PIC resolution.
    Like Darrel did I will assume a 0 to 5 volt reference.

    2.5 volts = 127
    At 8 bit resolution each volt is 51 steps.
    Spanning 2 volts or 102 steps will have 4.5 volts equaling 229.

    New value for m is 30 / 102 = 0.2941
    m = 0.2941

    New offset value is 127 * 0.2941 = 37.35
    b = 37.35

    y = (ADC * 0.2941) - 37.35
    ADC = 229
    y = 29.99 amps

    Possible code...
    Code:
    ADC = 229
    z = ADC * 2941  ' equals 673489
    z1 = DIV32 10000  ' z1 = 67
    y = z1 - 37       ' Did you want precision???
    I am sure something is wrong but it may help you understand.
    Dave
    Always wear safety glasses while programming.

  4. #4
    Join Date
    Jan 2008
    Location
    Pennsylvania
    Posts
    113


    Did you find this post helpful? Yes | No

    Default I get it but must be doing something wrong.

    Darry and Mack thanks very much for your response. I undersatand the 2 different ways you came to your solution. Either my ACS712 is fried or I am doing something wrong. I am passing AC through the 712. No matter which math I use the current reading seems to high but 2 to 3 amps. That is according to my clamp meter I am using taking a reading at the same time and the device I am using to test this. I am running a heat gun which states on the side it's 1200 watt 10amp heat gun.

    Any suggestions? I can post my code it's a very simple program.

    I appreciate the help, I've got to sweep up this big pile of hair I just yanked from my head

    Thanks
    David

  5. #5
    Join Date
    Apr 2009
    Location
    Pittsburgh, PA
    Posts
    17


    Did you find this post helpful? Yes | No

    Default Diagram we use

    I drew this diagram to illustrate the different units involved.

    For a numeric value of 166 read from the analog-to-digital converter, Darrin's equation brings back 11.58 and Mackrackit's brings back 11.47.

    They are close enough given the low resolution arithmetic taking place especially if you just round up to no decimal places.

    I can attest to the annoyance of one meter showing 9 Amps when the equations bring back the numbers above.

    http://www.picbasic.co.uk/forum/albu...0&pictureid=25
    Last edited by ukemigrant; - 3rd August 2009 at 01:06. Reason: Stupid image did not show up

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by DavyJones
    I am passing AC through the 712. No matter which math I use the current reading seems to high but 2 to 3 amps
    What Dave and I both "assumed" was that you were measuring DC currents.
    The part about an A/C signal would have helped in the beginning.

    Being that the heat gun is mainly a resistive load, you could get a much closer reading by finding the highest (peak) voltage and multiply by 0.707, which gives the R-M-S value of a perfect sine wave. Then convert to current.

    The heat gun also has a fan, which is an inductive load, so you'll never get a perfectly "accurate" reading with the 0.707 multiplier. But since the fan is only a small portion of the load it would be closer.

    But I doubt the eventual use of your project will be measuring the current from heat guns. And the type of load makes a HUGE difference.

    If you'll be primarily measuring motor currents, the wave forms will NOT be sine, and .707 will not even come close. In that case, there is only one way to go.

    Run the signal from the current sensor into an R-M-S converter. Don't bother trying to do it in software. I like the LTC1966, but it's a really tiny chip and isn't great for hobby use cause they're too hard to solder.

    There are several other chips available that do the same thing. But for sure you will need an R-M-S (Root Mean Square) reading for any inductive loads.
    <br>
    DT

Similar Threads

  1. A/D conversion with PIC18F67J50
    By ScaleRobotics in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 8th May 2009, 01:48
  2. A/D conversion problem in 18F2520, 2523, 2550 etc.
    By selimkara in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 10th March 2008, 16:26
  3. 16f877 A/D help
    By 3adam in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 25th January 2008, 04:06
  4. A/D converter fails?
    By egberttheone in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 13th February 2006, 18:57
  5. Strange A/D conversion...
    By Christos_K in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th June 2005, 01:35

Members who have read this thread : 2

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts