16F877 ADC using the Allegro ACS754 Current Sensor


Closed Thread
Results 1 to 9 of 9
  1. #1
    Join Date
    Jan 2008
    Location
    Pennsylvania
    Posts
    113

    Default 16F877 ADC using the Allegro ACS754 Current Sensor

    Ok I am going to show my lack of electronic knowledge on this one... here goes. I'm using an 16F877 to read an Allegro ACS754 I've got it working to a degree using some simple code to get the ADVAL. That part is not a problem. The problem I am seeing is the same problem I read about in this thread Just so I could see some real current other then a light bulb I am using a 1600 watt hair dryer which I am attempting to read how much current it is using. When I turn it on the current follows the AC and my reading goes from 128 (This is equal to 2.5vdc which the ACS754 puts out with no current) to 161 which after calculations 12.8amps which if this thing is really putting 1600watts give or take a few it's probably right anyway.... The math seems right so far. What I am having a problem with is as I said the same as the other person. The current is rising and falling with the AC so I am not getting a steady reading. I have used the MAX command to get this far but I just can't seen to figure out how to get or keep track of a reading that is not fluctuating with the wave of the AC. I didn't see that the person who was trying to do the same thing with the 877 and 754 ever came up with a solution.
    As always any help would be appreciated.
    Thanks
    David

  2. #2
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    From http://en.wikipedia.org/wiki/Root_mean_square

    "Thus the peak value of the mains voltage in the USA is about 120 × √2, or about 170 volts."

    Working backwards from that, 1/√2*Vpk=Vavg or 1/1.414*170=120. Assuming a more or less pure sinewave, current follows the same pattern,

    However, for anything other than a pure resistive load, voltage and current will be out of phase and you may or may not have a sinewave. You have to take many instantaneous measurements of both voltage and current and average them over time to get true power.

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


    Did you find this post helpful? Yes | No

    Default You had me at thus....

    Quote Originally Posted by dhouston View Post
    From http://en.wikipedia.org/wiki/Root_mean_square

    "Thus the peak value of the mains voltage in the USA is about 120 × √2, or about 170 volts."

    Working backwards from that, 1/√2*Vpk=Vavg or 1/1.414*170=120. Assuming a more or less pure sinewave, current follows the same pattern,

    However, for anything other than a pure resistive load, voltage and current will be out of phase and you may or may not have a sinewave. You have to take many instantaneous measurements of both voltage and current and average them over time to get true power.
    Dave, Thanks. I think I understand this caclulation but it's been allot of years you know the saying if you don't use it you loose it... well I think what you are trying to get at here is getting that peak current reading. (I think) I realize they might get out of sync I believe the sensor is going to report back the correct current being used that is what I am ultimately trying to get not the voltage.

    In a simplified version of my program I am:

    Code:
    mainloop:
           Gosub   getx            ' x returned with ad value from current sensor
           x = x - 128
           vmax1 = vmax1 max x
           resv = vmax1 * 3906
           resv = div32 1000
           Serout2 so,baud,[27,"[2;7H", dec5 resv]
           pause 100
           goto mainloop
           end
    using the MAX command over a period of one or more cycles get's the correct current being used (close enough as I said I doubt this hair dryer is right on at 1600 watts it says on the side of it) It's figuring out how to get that peak and know when it's been reached to reset the value of vmax1 and/or display the value my calculated current usage, "resv".

    I've tried a couple of things last night like trying to determine when the wave is going up or down and only displaying on the first pass through on the downside then resetting vmax1 to 0 when I hit zero. I would set vmax1 to MAX x on the upside but that sometimes doesn't work because it looks like in some cases when it starts back the downside it didn't get a chance to get the peak value during the upswing of the wave. For example let's say the peak of the wave is 200 I would set vmax1 at 180 but the next pass through mainloop I've already started the down side and get a value of 170 for the very next pass. Well that would be on the downside and I would display a current based on 180 not what the peak really made it to before the downside, do you follow that? I hope so.

    It appears the sensor is working correctly and the current is following the same pattern I'm just having difficulty trying to figure out when I've hit the peak so I can display a constant reading not one moving about with the wave.

    I'm going to try a simple counter and just keep reseting vmax1 after say 10 cycles through the loop and see how that looks. If anyone has any other suggestions or solutions I'm all ears.

    Thanks again
    David

  4. #4
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    With a purely resistive load, you should be able to establish the peak through timing. At 60Hz it will occur 4.17mS after ZC while at 50Hz it will occur 5mS after ZC.

    However, this will change with reactive loads because of the voltage/current phase difference. It will also change with non-linear loads which most electronic gear presents. The only way to do this accurately is with many nearly simultaneous samples of current and averaging over time. Digital power meters sample voltage and current several thousand times per second. Your PAUSE 100 means you are only sampling a few times per second and so are unlikely to get the peak.
    Last edited by dhouston; - 3rd May 2009 at 16:14.

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


    Did you find this post helpful? Yes | No

    Default In a perfect world....

    Quote Originally Posted by dhouston View Post
    With a purely resistive load, you should be able to establish the peak through timing. At 60Hz it will occur 4.17mS after ZC while at 50Hz it will occur 5mS after ZC.

    However, this will change with reactive loads because of the voltage/current phase difference. It will also change with non-linear loads which most electronic gear presents. The only way to do this accurately is with many nearly simultaneous samples of current and averaging over time. Digital power meters sample voltage and current several thousand times per second. Your PAUSE 100 means you are only sampling a few times per second and so are unlikely to get the peak.
    The problem I am seeing is that sometimes between coming down and back up again sometimes it does not always hit 0 so figuring out exactly when to start and stop I'm losing my mind. I've got darryl taylors averiging routine in my equation but still unless I must get vmax = vmax MAX x were x is my read value and display it no matter when I have choosen to reset vmax I get a tremendous fluctuating value. I'm starting to think perhaps there is something wrong with this sensor or my timing is just so completely out of sync...

    The other problem I see as well is coming down the wave I have the same issue between the pic reading and the wave itself between readings it will have gone past the peek completely.

    With this said I understand what you mean about taking many readings and as the MAX command proves I do eventually get a peak value my problem is knowing when exactly to reset that peak value and get my next reading. No matter what I try it seems to make my reading flucuate allot. I try to reset it when I hit 0 or after 50 times through the loop.

    I do appreciate the help. I am trying so many things it is making me crazy.

    David

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


    Did you find this post helpful? Yes | No

    Default Trial and error and possible solution

    I've been trying allot of different approaches to my problem not wanting to give up and certainly not going to wait for someone to drop code in my lap. I came up with this routine that seems to be pretty stable.

    Code:
    mainloop:
            Gosub   getx            ' Get x value read ADC return value in 'x'
            x = x - 128
            vmin = vmin min x     ' Get min value
            WHILE VMIN <> 0    
                GOTO MAINLOOP  'stay here until the cross road of 0
            WEND
            vmax = vmax max x  
            resv = vmax * 3906
            resv = div32 1000
            READING = 1
            VALUE = resv
            gosub average         'send calculated amps to darryl's average routine
            resv = value
            cnt = cnt + 1
            if cnt >= 120 then    'get 120 averaged readings then display the amps
                Serout2 so,baud,[27,"[2;7H", dec5 resv]
                cnt = 0               'reset the counters and 
                vmax = 0
                VMIN = 1
            endif
            Pause   10             ' Do it about 10 times a second
            Goto    mainloop
    This seems pretty stable so far and switching between the hair dryers hi and low settings I can clearly see the amps switch between 5.8 ish and 12.8 ish give or tak an ish he he he. I've got a call into a friend to see if he's got a real accurate amp meter that I can test and see how accurate this ACS754 is. I'll keep you posted.

    If anyone else has any ideas about this code or possibly see something wrong with my code or has a better idea please let me know.

    Thanks
    David

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


    Did you find this post helpful? Yes | No

    Default Allegro Support

    Just got off the phone with Allegro support who called me back after I emailed them just to confirm I wasn't going crazy and I didn't hook up something incorrectly and sure enough the Vout on the ACS754 will produce a sine wave proportionate to the current being used. So I simplified the heck out of my prorgam and just have it loop through as quickly as possible keeping track of the MAX and displaying that at every 300 count. I will experiment with that and watch it closely. I need to incorporate this into a circuit with another sensor, pressure transducer, hopefully I can run that pressure transducer and 2 of these Allegro ACS754's all on my 16F877.

    They did tell me that the ACS754 is going to go away and be replace by the ACS756 so... He also told me that the have an 8 pin version that for my application will read up to 30 amps and I should really be looking at that. He also told me the data sheet for the ACS712 has some peek voltage circuit examples which I am going to look at right now.

    Again if anyones worked with one of these current sensor and has any good code examples they found worked I would not mind taking a peek at that.

    Thanks
    David

  8. #8


    Did you find this post helpful? Yes | No

    Default current sense

    You could rectify the output and add filter (resister/capicator) and have a DC voltage related to the AC current. Still would have the offset voltage, about 2 volts which you subtract out anyway. 10 micro-farad and 100K ohms will smooth output.
    don
    Attached Images Attached Images  
    Last edited by amgen; - 5th May 2009 at 14:11. Reason: change filtering

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


    Did you find this post helpful? Yes | No

    Default Going to try the recified output...

    Quote Originally Posted by amgen View Post
    You could rectify the output and add filter (resister/capicator) and have a DC voltage related to the AC current. Still would have the offset voltage, about 2 volts which you subtract out anyway. 10 micro-farad and 100K ohms will smooth output.
    don

    Don, Thanks I will give that a try. I'll let everyone know how that turns out.

Similar Threads

  1. Need a cheap touch sensor idea.. here it is
    By mister_e in forum Code Examples
    Replies: 20
    Last Post: - 16th April 2016, 23:42
  2. PIC 16f877 A/D conversion Allegro ACS712
    By DavyJones in forum General
    Replies: 32
    Last Post: - 4th June 2011, 05:54
  3. ADC input using a current transformer?
    By jessey in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 12th May 2009, 23:13
  4. current sensor interfacing
    By hell_pk in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 29th January 2009, 05:55
  5. Reading (ADC) negative current
    By sougata in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 21st November 2006, 18:38

Members who have read this thread : 1

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