PIC 16f877 A/D conversion Allegro ACS712

# Thread: PIC 16f877 A/D conversion Allegro ACS712

1. ## 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 = DIV32 1000

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

2. 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>
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 ...

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

3. 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
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.

4. ## 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. ## 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 02:06. Reason: Stupid image did not show up

6. 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>

7. Originally Posted by Darrel Taylor
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>
You can also take many readings (~100 x the frequency or 6000Hz) and average them but I don't think a PIC can do this fast enough for accuracy. This is how digital wattmeters work.

If an oscilloscope is not available, it might be possible to get an idea of the waveform using a PC or laptop with a LineIn port for the soundcard. The sampling rate should be adequate unless there are sharp spikes. Since the Allegro output is 0-5V, you can use the same technique I use for IR & RF. See...

8. Not knowing more about the project a simple current transformer might be good enough. You would still need to calibrate it though.

There are pros and cons to every option, but CTs are good enough for power companies to sell with...

Let us know which way you want to go.

9. ## Good notes and advice as usual.

Darryl, Your right I should have mentioned that I was trying to measure an AC current right off the bat my mistake.

So I've got a couple differnt methods and since I want to learn I'm looking at both of these and am leaning more towards the LTC1966.

Dave no I did not forget you and I have been taking many samples in my code but that does not seem to be working well for me but I do like your example of how to read this with the line in on the sound card and kind of simulate having an oscilliscope that is pretty cool and I am going to be doing that this evening.

So to the RMS to DC converter. I'm reading the datasheet for the LTC1966 on it's functionality it sounds like as you said run the output of the 712 through and it is going to give me the DC voltage proportionate to the amps that I am trying to read on the pic correct? I'm going to get one and try it out so I can see this for myself.

Thanks
David

10. Originally Posted by DavyJones
I'm going to get one and try it out so I can see this for myself.
Then get yourself some of the adapters shown below.

The other picture is a 3-phase current sense board a did for a customer. You can see how small they are. They don't come in any larger packages.

I was using current transformers instead of hall-effect sensors, but the idea (and RMS requirement) are the same for either type.

I also had to use a TLV324 op-amp (not shown) to bring it up to levels the PIC could read accurately.

11. ## A few more thoughts... That is small

Yes that package is pretty small... I've been thinking as Mr. Houston pointed out this is what I was initially doing, taking many readings, I realize that because the reading from the ACS712 is going to go along with the sine wave and at any given point in time it could give me a reading anywhere along the wave I'd been taking hundreds of samples and keeping track of the MAX. then after a couple of seconds display this value. I just got an email from Allegro and they pointed out when reading for AC current that the + reading will be 2.5V to 4.5V and the minus side reading will be from .5V to 2.5V Could I not keep track of the MIN as well since the reading will never go negative and add that into my equation averageing the min and max? Where a reading of 169 on the + side of the scale would be a 94 on the minus side of the scale right? If I grab enough of these readings....

Is this completely wrong to go this route? It seems like it has been working. According to the franklin motor company and my clamp meter it seems like I was really close but my math, in particuliar, my multiplier thats why I started this thread in the first place. It's not imperative that this be a real time reading as long as I can get a reading every couple of seconds then for this application it is going to be fine. What I am doing is basically monitoring the current the submersible pumps are drawing and when the well runs dry the current drops about 1 to 2 amps and when I sense this I can shut the pump down.

David

12. Nope, can't go that route.

You ARE measuring currents from inductive loads (submersible pumps).

So you have to get an RMS reading.
You would be surprised just how far off the numbers will be. >100%

I know this too well, because I went down that same road my first time too.
It was horrible.
Down right embarrassing actually, since the customer saw it too.
<br>

13. ## I trust your experience.

Originally Posted by Darrel Taylor
Nope, can't go that route.

You ARE measuring currents from inductive loads (submersible pumps).

So you have to get an RMS reading.
You would be surprised just how far off the numbers will be. >100%

I know this too well, because I went down that same road my first time too.
It was horrible.
Down right embarrassing actually, since the customer saw it too.
<br>
I trust you.

I just wanted to double check myself. I am just playing around now with the min/max on an 85w bulb and just this 85w bulb gives me a bad reading. I get for MAX 132 and MIN is 124 so it's a little off if I run this through the calculation you provided me I get
(132 - 127 ) * 196 / 66 = 1.48
(127 - 124 ) * 196 / 66 = .89
Even avergaing these 2 I come up with 1.18 that is that is double what the clamp meter is telling me is .66 which I tend to believe now because since it truly should be .7 the fact the clamp is telling me .66 and how can I really tell if this bulb is putting off truly 85w.

Here is something I am seeing that is strange. If I plug the 1600w hairdryer that has 2 settings I get 2 readings as you would expect on low I get 152 MAX and 103 MIN then for the high setting I get 179 MAX and 76 MIN but when I plug in the 10 amp hot air gun which also has 2 settings when I set it on low I get 172 MAX and 127 MIN with the reading I get from the hair dryer I would have expected the min reading on the hot air gun to go to 82 that is goofy why would the hot air gun get such a different result then the hair dryer?

The LTC is on it's way I found those at digikey looking for the little board for it now. Also getting the TLV324 would it be to much trouble to get a look at how you wired that up with both components to the pic?
Thanks
David

14. The LTC is on it's way I found those at digikey looking for the little board for it now.
OUCH!

You should have searched for 33108 at digikey before ordering.
http://search.digikey.com/scripts/Dk...keywords=33108

I'm betting UPS does well this quarter.
<br>

15. And no, I can't really show the schematics or anything, but It's no big secret ...

If you were to trace out the components in the picture I showed above ...
You would find the EXACT schematic shown in the datasheet under the heading "Single Supply RMS Current Measurement". The only difference is that I used a different current transformer, and changed the "burden" resistor to 51 ohms. Otherwise, identical.

Add a little gain for the PIC's A/D and you're good to go.

hth,

16. ## got it!

Originally Posted by Darrel Taylor
OUCH!

You should have searched for 33108 at digikey before ordering.
http://search.digikey.com/scripts/Dk...keywords=33108

I'm betting UPS does well this quarter.
<br>
I got the little boards from there as well. I meant on it's way meaning it was in my cart. I know better to wait until the end of the day to place my order and commit

I'm having trouble locating that op amp though...

17. ## Did I miss something?

Originally Posted by Darrel Taylor
And no, I can't really show the schematics or anything, but It's no big secret ...

If you were to trace out the components in the picture I showed above ...
You would find the EXACT schematic shown in the datasheet under the heading "Single Supply RMS Current Measurement". The only difference is that I used a different current transformer, and changed the "burden" resistor to 51 ohms. Otherwise, identical.

Add a little gain for the PIC's A/D and you're good to go.

hth,

Did I miss something or are you using the ACS712 in this circuit or not at all? It sounded like you were running this 712 into this but reviewing this schematic I don't think you are even using the 712 now.

David

18. It does not use an ACS712 in that circuit.

I'm showing the LTC1966 RMS convertor, which is needed whether the signal comes from a current transformer, a hall-effect sensor, or just a BIG honken shunt resistor that would heat your toaster oven.

It's the RMS conversion that matters.
<br>

19. ## understood.

Originally Posted by Darrel Taylor
It does not use an ACS712 in that circuit.

I'm showing the LTC1966 RMS convertor, which is needed whether the signal comes from a current transformer, a hall-effect sensor, or just a BIG honken resistor that would heat your toaster oven.

It's the RMS conversion that matters.
<br>
I am sorry I am not trying to tick you off or anything but I have to question this one last time. What is pumping the same signal through the LTC1966 going to give me that I can't figure out myself? I guess that is the part I am having trouble comprehending. The ACS712 data sheet says it's good for sensing current for motor control, load detection, etc... No matter what the ACS712 is going to output a voltage that is in direct proportion to the current it senses. Granted it's going to follow the wave form of the AC current it's still going to be proportinate to the peak current arghhhhh I need to go do something else for a while and stop thinking about this.
I'm not giving up on this I know I will get it.

20. I must sound terrible in my replies.
Everyone thinks I'm getting ticked off.

Tuff!

But when I see someone going down the same bad path I took, I'll just keep tugging on your belt trying to pull you back, saying ... Duuude, there's a cliff up there. Don't go that way.

Let's see here ...
The ACS712 data sheet says it's good for sensing current for motor control, load detection, etc...
It IS!
As long as you know how to compensate for the type of load you are measuring.

No matter what the ACS712 is going to output a voltage that is in direct proportion to the current it senses. Granted it's going to follow the wave form of the AC current it's still going to be proportinate to the peak current arghhhhh
Not at all.
With RMS the peak doesn't matter anymore. The entire waveform is averaged over time, to represent the amount of current that flowed during a specified time period.

Small motors will generate fairly high peaks, but since there's not much load it can overcome each magnetic pole fairly quickly. So a short duration current flow over the course of 1 AC cycle will average out to maybe one fifth of what you would read from a "Peak value".

I'm not giving up on this I know I will get it.
I hope not. You've already ordered the parts.

When you get down to doing the math. Come back and describe the parameters. I got that part down.
<br>

21. ## Op Amp

No your posts don't sound horrible I'm just anticipating or hearing you in my head "What don't you get you idiot!" I'm just struggling with this my knowledge about electronics is below novice. I'm having trouble though locating the op am you mentioned TLV324 it looks like that comes up as a switch of some sort not an op amp.
Thanks
David

22. Oops, that would be a TLV274.

I knew I should have checked that.

TLV274 is a rail-rail version of the LM324.
<br>

23. ## just my luck

Originally Posted by Darrel Taylor
Oops, that would be a TLV274.

I knew I should have checked that.

TLV274 is a rail-rail version of the LM324.
<br>

Wouldn't you know it Digikey doesn't stock any.... argh

24. ## Alternate

Originally Posted by DavyJones
Wouldn't you know it Digikey doesn't stock any.... argh
How about the TLV271 I only need one for my test. I think I will ultimatley need the TLV272 because I am controlling and trying to read the current from 2 pumps.

25. Yeah, only available in the TSSOP package.

I agree, those are good alternates.
<br>

Darrel, I think I might understand now I know that we are off subject from a general pic discussion but I'm going to keep going in this thread it does pertain to working with a pic ultimately so....

I think I am picturing this correctly now. If I take a thousand readings from the PIC and just keep track of the max at some point I will get a peak let's say it's 12 amps. But like you are pointing out that is truly a peak if we were able to get a sample at the top of each waverform over time it would average out to 9 amps which is the true power. The fact that when I sample the A/D I have know idea were in the wave form I am at, at any given point in time, makes it impossible to do just sampling the ACS712 by itself. If I could get a reading form the ACS712 at the top/bottom of each wave then it could be done but then that is what the RMS to DC is going to do for me give me a nice straight DC line.

Did I get it?

27. Originally Posted by DavyJones
If I could get a reading form the ACS712 at the top/bottom of each wave then it could be done but then that is what the RMS to DC is going to do for me give me a nice straight DC line.
You are still making an unwarranted assumption that you have a sine wave. Darrell is suggesting the RMS chip and I suggested looking at the output of the Allegro chip with an oscilloscope (to see the waveform) because we don't think you have a sine wave. 0.707 x the peak will work with a sine wave and averaging ALL of the readings will work with any waveform (if the readings can be taken fast enough to get a good sampling of the wave form).

There are at least three ways to do this with non-sinusoidal waveforms. See the web page I referenced earlier (via email)...
I vaguely recalled a somewhat similar thread - it took me a while to find it.
Last edited by dhouston; - 4th August 2009 at 17:41.

28. ## Agreed

Dave,
I can see were I made it sound like I thought I still have a pure sine wave thats not what I meant. What I was actually trying to say was what you just stated that because it was not a pure sine wave if I took thousands of readings and averaged those it would work.

I'm looking at the document I saw that link in the email but it came acrossed somewhat broken I did not get the entire link as a complete link in the email.

Thanks
David

29. Originally Posted by DavyJones
I can see were I made it sound like I thought I still have a pure sine wave thats not what I meant. What I was actually trying to say was what you just stated that because it was not a pure sine wave if I took thousands of readings and averaged those it would work.
It's a bit more complicated - it requires some complex math. See Digital True RMS Converters on page 3 of the referenced pdf.

If you look at the waveform and see it is more or less sinusoidal, then 0.707 x the peak output of the Allegro should get you close enough for what you're trying to do.

30. ## I have some homework

Dave, Thanks I appreciate all you and Darrel are teaching me and not getting to stressed with me while I try to learn this. I'm going to work on capturing the wave this evening and take a look at what I see. I do have the components that Darrel suggested on order as well I want to hook those up in any event to help me understand. Things are so much easier when the device is DC like they were with the pressure transducer.

thanks again
David

31. ## Oops

I work with someone called Darrin and Darryl and I always get them mixed up. Sorry for getting your name wrong Darrel - lord knows I've seen it often enough .

32. ## Re: PIC 16f690 Allegro ACS712 + -30Amp

Is this the way to get negative reading? i am new to this, so sorry if i mixed it up.

I use Allegro ACS714 but with a PIF16F690 with A/D set to 8bit, should not be any difference.

It works and can read +30Amp to -30Amp.

The problem is that i use 0V and 5V as reference. I cant figure out to get 0.52V to read 0 and 4.48V to read 255? Is it a math problem or must i add an external ref?

The ACS gives 66mV/A:
Amp Vin Bit
+30A 4.480 255
+10A 3.160
+1A 2.566
0A 2.500 127
-1A 2,434
-10A 1.840
-30A 0.520 0

See CODE below and attached DRAWING

Code:
```'Define OSC and ADC
DEFINE OSC 4             ' Set internal Oschillator to 4Mhz
DEFINE    ADC_BITS 8       ' Set number of bits in result
DEFINE    ADC_CLOCK 2      ' Set clock source (3=rc)
DEFINE    ADC_SAMPLEUS 50  ' Set sampling time in uS
' Define LCD pins
Define LCD_DREG  PORTC   'LCD data port
Define LCD_DBIT  0       'LCD data starting bit 0 or 4
Define LCD_RSREG PORTC   'LCD register select port
Define LCD_RSBIT 4       'LCD register select bit
Define LCD_EREG  PORTC   'LCD enable port
Define LCD_EBIT  5       'LCD enable bit

TRISA = %00001001                         ' RA0 = A/D input
ADCON1.7 = 0                                  ' RA.1 = +Vref, Set PORTA analog and left justify result
PORTb.6  =0                                     ' Prepare RB0 for high-going pulseout

ANSEL = %00000101                         ' Set PORTA.2 analog, rest digital
ANSELH = %00000000

' Variables
outpuls VAR WORD                           ' Variable for the calculated puls out in mS
POT_POS VAR BYTE                          ' Potentiometer position CC=0, CCW=255
amp var word                                   ' Ampere 30A-0-30A (0-127-255)

Pause 500                                 ' Wait for LCD to start

MainLoop:                                 ' The Loop start here!

'Check if negative or positive Ampere
IF POT_POS=>127 then
amp=(235 * (POT_POS-127))
else
amp=(235 * (128-POT_POS))
endif

Lcdout \$fe, 1, "POT_POS= ", #POT_POS      ' Display POT Valu between 0-255 on line 1

' Check if negtive or positive Ampere, only for - minus sign on LCD display
IF POT_POS=>127 then
LCDOut \$fe,\$C0, "AMP= ",DEC (amp/1000),".", DEC1 amp' Display +Amp on line 2
else
LCDOut \$fe,\$C0, "AMP= -",DEC (amp/1000),".", DEC1 amp' Display -Amp on line 2
endif

PAUSE 20                                  ' Constant 20mS pulses(low) between outpuls
GOTO MainLoop                             ' Forever
End
```

Last edited by Archangel; - 7th June 2011 at 19:21. Reason: non;add code tags

33. ## Re: PIC 16f877 A/D conversion Allegro ACS712

Hello Darrel,
I know the topic is old , but i need help as i am new to analog. Will you be so kind to post the schematic, mostly i am looking at the use of the TLV324. I do not have a clue about op amp, and if i do not find a solution fast to our plant, my boss will send me fishing and get somebody else to replace me. In my case is simpler than the post. I have a machine with 100 heaters, and i would like to use acs712 to detect any burned out heaters, meaning 0 amps. Also i can leave with detecting any heater which is less than one amp. What i want to achieve is to have an optocoupler for each output of ltc1966, and if the current is over or equal with 1 amps to turn the opto ON. the opto will feed a digital input to my micro. If it is simpler i am happy with 0 amps too. what ever it is simpler, no need for calibration, and less components to adjust .
If you would like you can also write me at [email protected]
As the issue stands right now, i am in the hot water because i have 10 days to implement a solution.The maximum current on the heaters (110 VAC) it is 4.5 amps ac.
Thank you