1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Measure Mains AC current

Discussion in 'Electronic Projects Design/Ideas/Reviews' started by abicash, Jan 22, 2009.

  1. dougy83

    dougy83 Well-Known Member

    Joined:
    May 18, 2008
    Messages:
    2,672
    Likes:
    215
    Location:
    Brisbane, Australia
    I haven't seen what current your load draws, so have no idea of shunt resistance or power dissipation. The uC you're using has an on-board 1.2V reference that may be used with the ADC module; I suggest you could use it and feed the shunt voltage into the ADC.

    I recommend you translate the shunt voltage to reside within the 0-1.2V range, for the ADC (allows monitoring of both positive & negative shunt voltages). The example circuit does this for the Vshunt range of around -1V to +1V, with the centrepoint around 0.65V. Play around with the resistor values to get the range you require.

    Unfortunately, the above setup also attenuates (reduces amplitude of) the signal. If this is a problem, the shunt voltage may be offset using a foward biased diode.
     

    Attached Files:

  2. abicash

    abicash Member

    Joined:
    Feb 12, 2007
    Messages:
    427
    Likes:
    3
    Hello all

    I am now using ACS712 current sensor from Allegro after trying out various options.Since every thing else occupied a lot of space on the board.
    I am running into a new problem after this.
    The ACS712 works on 5V and I have 3.3V and no chance to build a dual power supply.
    How can i go about this?
    Please suggest something as usual.
    Awaiting valuable suggestions

    Thanks and regards
     
  3. abicash

    abicash Member

    Joined:
    Feb 12, 2007
    Messages:
    427
    Likes:
    3
    Hello

    I am still having some trouble using the ACS712 hall effect sensor.
    I am using it according to an app note where only rectification and filtering is done on the VIOUT corresponding voltage to the i/p current.
    This is within limits of my 3.3v ADC of my MCU.
    Here i am having a lot of trouble since i have designed a window comparator where the ADC checks a below-than & higher-than threshold and switches off an o/p & glows appropriate LEDs.
    Whenever i try to switch an o/p pin of the MCU active,the ADC thinks its reached a below-than threshold before the load is turned on.I tried adding sampling to the ADC value but no joy :confused:

    How can this be achieved w/o me breaking my mind..

    Please get back

    Thanks and regards
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. dknguyen

    dknguyen Well-Known Member

    Joined:
    May 6, 2006
    Messages:
    6,668
    Likes:
    64

    Can you please explain how the comparator and ADC are working together? I am not sure if you are saying that the comparator triggers the ADC sample, or if something else is going on and what the output pins are doing when they switch active? I don't want to get too specific without knowing what's going on because it might make things confusing.

    Without knowing anything more, all I can really say is why don't you just program a delay time before the ADC starts working? Or make it so that the ADC averages out a bunch of samples?
     
    Last edited: Jun 17, 2009
  6. abicash

    abicash Member

    Joined:
    Feb 12, 2007
    Messages:
    427
    Likes:
    3
    Hello dknguyen

    Thanks for replying :)

    Actually what i meant by saying 'window-comparator' was a software approach.
    I get the voltage in the ADC ,which is interrupt driven,and if the measured value is in between two threshold levels,the o/p is turned on.
    To turn on this TRIAC i have to pull the gate low.Its common terminal should be compulsorily tied to Vcc and Neutral.Now when the gate is 'high' the Triac is off and vice-versa.
    Now the ACS712 sensor is put on the high (output) side of the TRIAC.
    My MCU uses the same 5v power but its GND is lifted by 3-diode drops to get a 3.3v. (i have attached a schematic of my ckt)

    What now happens is,when i turn the TRIAC on ,the ADC starts sampling the current.The code for this sampling is like this
    Code (text):
    interrupt 19 void ADC_ISR(void)
      {
               
          ADC_Read=ADCR; //+Acknowledge the Read Value to disable COCO Flag
          {
       
           if ((( ADC_Read>=306) ) && (PTAD_PTAD1==0))//*********CHANGED FOR ACSTRIAC
     
              {  
                PTAD_PTAD1 = SET;     //************CHANGED FOR ACSTRIAC
                PTBD_PTBD1 = SET  ;     //red LED on
                while(1); //wait here until reset
              }
     
          if ((( ADC_Read<50)) && (PTAD_PTAD1==0) ) //************CHANGED FOR ACSTRIAC
     
            {
              PTAD_PTAD1 = SET;     //************CHANGED FOR ACSTRIAC
              PTBD_PTBD0 = SET  ;     //green LED on
                     
            }    
                 
         }
         
      }
     

    Now whenever this interrupt is invoked,the lower-than action takes place and the green led glows.
    I tried sampling the ADC_Read for a number of times so that a delay is generated.Still the same thing.
    For higher values also it doesn't work 100%...like if i measured a load current and respective voltage at the ADC pin i/p and set the value corresponding to this,it will take action on surge current.
    Is there some approach/algorithm for sampling?.maybe i am doing something wrong here

    Please get back

    Thanks and regards
     

    Attached Files:

  7. dknguyen

    dknguyen Well-Known Member

    Joined:
    May 6, 2006
    Messages:
    6,668
    Likes:
    64
    R4, D7, and C6. Are these all here to try and smooth out the AC current levels so you don't have to worry about whether you are sampling the a peak or something in between for your software comparator? THe cutoff frequency of 8Hz is very low relative to 60Hz or 120Hz which is good.

    WHen the frequency is this low, the ADC sampling is probably hundeds if not thousands of times faster. So sampling multiples times and using the last sample (or averaging them all) to produce a delay won't do much. Try programming an actual delay in (like 1 second, or 2 seconds) before sampling.

    But this method is probably more reliable:

    -Sample at high speed like you normally would
    -Have a counter
    -increment the counter by X whenever an ADC sample is taken that is not normal operation
    -decrement the counter by Y whenever an ADC sample is taken that is within normal operation
    -Whenever the counter passes a threshold, react to an overload condition, and afterwards, reset both counters to zero after reacting.

    [Counter threshold]*[ADC sample period]/X = Fastest possible reaction time to an overload (100% overload ADC samples)

    % of the time overloads must exist for to be reacted to (if they exist for long enough period of time) = Y/(X+Y)

    Two things must happen for your circuit to react to an overload- the overload readings must occur often enough and this must happen for a long enough period of time.

    Choose the threshold on your counter so that with your ADC sample frequency, the total number of samples required to trigger an overload shutdown is on the time-scale you need (probably on the order of seconds). The ADC sample frequency, counter threshold, and X and Y all decide how long an overload must exist for before your circuit to responds. THe difference between X and Y control how often an overload condition must be present before it is considered an overload (how sensitive your circuit is).

    This reaction time will be longer (sometimes much longer) if your safe ADC samples decrement the counter almost as fast as your overload ADC samples increment the counter. The difference between X and Y will influence this. That's the reason that X should not be less than Y, and why X should be larger than Y. For example, if you are getting overload readings 50% of the time and safe readings the other 50% of the time then something is probably wrong and you should shut it down. But if X = Y, then your system will not react to the overload. In another example, if X<Y, then the only overloads that would be caught are overload readings that are happening 100% of the time (continuously). Also remember that the frequency of overload readings must also be maintained (or increase) for a long enough period of time before the reaction will occur.

    I'd probably just keep Y = 1 and adjust X. I'd start with X somewhere between 2 to 4.
     
    Last edited: Jun 18, 2009
  8. abicash

    abicash Member

    Joined:
    Feb 12, 2007
    Messages:
    427
    Likes:
    3
    Hi dknguyen :)

    Thanks for replying and taking keen interest in my problem.

    The method you gave is beautiful. Thanks for that.

    Although x>y that you mentioned may not be pertinent to my application since i just need two threshold levels and generous errors in the time taken to react to the overload or underload conditions mayn't be harmful.

    I will check this approach though and get back with observations and/or more troublesome questions:eek:

    Please bear with me

    Thanks and regards

    [EDIT] : referring to the schematic i have few questions here.As you can see,i have 2 different grounds.One is the 5v ground and the other is the 3.3v ground (effectively,since lifted by 3 diode drops).The current sensor operates on 5v and my MCU is on 3.3v and fullscale ADC reading for 10bit operation is 3.3v=1023 steps.
    the current sensor o/ps a voltg wrt its GND which is 5v scaled.This i am feeding to the ADC which refers the MCU GND as 3.3v scaled.Is this approach corect or will it create some problem?..i dont know if i have put my question corectly..maybe u can understand..
     
    Last edited: Jun 18, 2009
  9. dknguyen

    dknguyen Well-Known Member

    Joined:
    May 6, 2006
    Messages:
    6,668
    Likes:
    64
    I've been going through my post and making corrections and clarifications. Please reread. THis is the same method used by CAN networks to monitor the number of transmissions with errors to determine whether a device is faulty and should be shut off so it stops interferring with network.


    X does have to be larger than Y. I added in explanations and examples about why. This method is basically a filter. X and Y are not voltage threshold levels- they adjust the filter cutoff frequencies. X and Y let you tune the filter and it's possible to tune the filter so that it filters out everything or nothing. Y = 0 filter out nothing, and X<Y filters out everything.

    Voltage doesn't work that way. A 5V chip does not need 5V at the +V pin. It needs a DIFFERENCE of 5V between it's +V and GND pins. Diodes also don't work that way. Diodes drop voltage, they don't raise voltage. You can use diodes to turn 5V into 3.3V, but you can't use them to turn 3.3V into 5V. If voltages and diodes worked that way, I could take a 1V battery and add 100 diodes to power a 100V device.

    You need to use a 5V regulator and a 3.3V regulator (or a 3.3V regulator that steps down the output of the 5V regulator). When you do this, make sure to add resistive dividers to scale down the ACS712 5Vmax output voltage to your 3.3Vmax ADC.

    I can't believe I didn't catch that the first time. BUt I went straight to the ACS712 part of your schematic and ignored the rest.
     
    Last edited: Jun 18, 2009
  10. abicash

    abicash Member

    Joined:
    Feb 12, 2007
    Messages:
    427
    Likes:
    3
    Hello again
    i think u misunderstood.The 5v is dropped in 3 diodes so that 3.3v is got for the MCU.The voltage at ADC pin wil be less by 0.7v*3 since it will 'see' this voltage wrt MCUGND & not 5v gnd.
     
  11. dknguyen

    dknguyen Well-Known Member

    Joined:
    May 6, 2006
    Messages:
    6,668
    Likes:
    64
    Oh I see. That is an uncommon way to do it. Normally you drop the voltage on the high-side so all all the devices in a circuit running off the same ground. I can see why you want to do it the way you have it so you can minimize the number of components needed to get the 5V sensor output to work with the 3.3V ADC.

    If I had to do it your way, I would put a capacitor in parallel with D6-D7 to stabilize the voltage. YOu should also have a decoupling capacitor in parallel with the power pins of the ACS712. THis will stabilize the voltages which could also be factors in the problems you are talking about.
     
    Last edited: Jun 18, 2009
  12. abicash

    abicash Member

    Joined:
    Feb 12, 2007
    Messages:
    427
    Likes:
    3
    I have attached the complete schematic of my board (earlier i had attached only the particular section in discussion)..I am in a pickle here since i have to achieve so many things.To begin with i have to generate an unisolated Vcc from mains.Now my MCU is max 3.6v and the ACS712 is min 4.5v.The TRIAC has to be turned on only (& only) in the way mentioned in the data sheet.Its common terminal has to be at a higher dc voltg level than its gate to turn it on.Also the common should be mandatorily connected to Neutral of mains (or return path) .So i have to create a -5v and consider GND as Vcc & vice-versa.So i cannot get common GND because of this since NEutral=Vcc=triac common.This doesn't permit use of another 5v to 3.3v regulator.This can ONLY be achieved by lifting the MCU GND by some 2v.
    [/QUOTE]
    You mean D4 to D6?.There is a decoup cap on the ACS712.

    Please study my schem closely and you can get in my shoes:)

    Thanks and regards
     

    Attached Files:

  13. dknguyen

    dknguyen Well-Known Member

    Joined:
    May 6, 2006
    Messages:
    6,668
    Likes:
    64
    Yeah, I meant D4-D6.
     
    Last edited: Jun 18, 2009
  14. abicash

    abicash Member

    Joined:
    Feb 12, 2007
    Messages:
    427
    Likes:
    3
    Hello dknguyen

    How much threshold value do you suggest?

    more than say 20 ?
     
  15. abicash

    abicash Member

    Joined:
    Feb 12, 2007
    Messages:
    427
    Likes:
    3
    Ok i understood what the threshold value should be..I missed your calculation

    [Counter threshold]*[ADC sample period]/X = Fastest possible reaction time to an overload (100% overload ADC samples)

    But i have a question here..Please forgive my ignorance..:eek:

    The counter is incremented by X on overload/underload (OL/UL) conditions and decremented by Y for normal conditions.If now there was no OL/UL then the counter will be always decremented by Y.Isn't there a need then to initialise the counter with some value and check for counter=0 and
    re-initialise it?

    Please get back
     
  16. dknguyen

    dknguyen Well-Known Member

    Joined:
    May 6, 2006
    Messages:
    6,668
    Likes:
    64
    THat's correct. For every normal ADC sample, the counter is decremented (to a minimum of zero). On start up counter is initially set to zero. It can actually be initialized to anything below the threshold. It won't make a real difference once the program starts running.

    I thought all that was obvious so I just didn't say it. Maybe it's not so obvious until you can visualize how the algorithm is supposed to work (or maybe it is since you did think of it).
     
    Last edited: Jun 19, 2009
  17. abicash

    abicash Member

    Joined:
    Feb 12, 2007
    Messages:
    427
    Likes:
    3
    Hi again

    I wrote the code with the X value=3 & Y=1.
    The counter was init at 4 and threshold was around 255 (all decimal)

    I do get a good response with transient OL suppressed and sustained OL serviced.
    But (there is a BUT :( ) The reading at the ADC pin is not stable.Maybe because of the 3 diodes i have there.The TRIAC takes around 25mA for firing and the LEDs take some 5mA.The IR detector another 10mA. The current is not more than 50mA.
    What i am observing is that whenever an OL condition occurs (i am toggling the LEDs on for demo indication of which situation is being handled Red=OL,Green=normal).
    I now got some 800mV for a 500watt load so the ADC_Read value i set was
    (1023*0.8v)/3v =273 decimal

    So now 273 corresponds to 500w.At this i expect the green LED to toggle always and (maybe) once in a while the red one too.
    I dont know if its the LOad (incandescent lamp) starts demanding more current or the voltage at Vcc drops,but now the OL is occuring and the red led starts blinking.And after the threshold is reached ,the o/p is turned off.

    Heres the code i wrote

    Code (text):
    interrupt 19 void ADC_ISR(void)
      {
         ADC_Read= ADCR; //Acknowledge the Read Value to disable COCO Flag

         if (( ADC_Read>=273) && (PTAD_PTAD1==0))
         
          { PTBD_PTBD1 = ~PTBD_PTBD1  ; //toggle red********TESTING
            ADC_CNT = ADC_CNT+3;
          }
         
          else
          {
            PTBD_PTBD0 = ~PTBD_PTBD0  ; //toggle green********TESTING
            ADC_CNT = ADC_CNT-1;
            if(ADC_CNT<=1)
              ADC_CNT=4;
           }
         
           if (ADC_CNT>255)
           
            {
               if (( ADC_Read>=273)  && (PTAD_PTAD1==0))//*********CHANGED FOR ACSTRIAC
     
              {  
                PTAD_PTAD1 = SET;     //************CHANGED FOR ACSTRIAC
                PTBD_PTBD1 = SET  ;     //red LED on
               
              }
     
           
           
             ADC_CNT=0;
           
            }
       }
    I am yet to check the UL condition.
    can you help?

    Thanks and regards
     
  18. dknguyen

    dknguyen Well-Known Member

    Joined:
    May 6, 2006
    Messages:
    6,668
    Likes:
    64
    DId you add a capacitor in parallel with those 3 diodes?

    Incadescent lamps consume more current when they first turn on. AS the filament heats up the resistance increases. ADjust your filter to account for surges like that (the counter threshold which controls the time period the condition must last for, not X/Y). That was the point of the filter- to not react to an overload just because it was a surge that was supposed to be part of normal operation.
     
    Last edited: Jun 19, 2009
  19. abicash

    abicash Member

    Joined:
    Feb 12, 2007
    Messages:
    427
    Likes:
    3
    Yes there is a cap across the diodes.The filter too is working i think since transients are definitely taken care of as i see with the leds.It is just that the sensor readings at adc i/p change with time.
     
  20. abicash

    abicash Member

    Joined:
    Feb 12, 2007
    Messages:
    427
    Likes:
    3
    Hello again

    I have almost sorted out most problems now.
    I am dumping the 3 diodes or using any lifting of the ground at the MCU.I am planning to use a boost converter (TPS61222) 3v to 5v for the ACS712.
    My MCU will operate on a fairly stable 3v and the ACS712 on 5v.
    In my schematic if you observe there was a diode D7 at the o/p of the ACS712.With the values i was measuring for an OL,the diode had almost a similar drop (0.6-0.7v) and also drifting with temperature.So i think there was one problem and the other at the Vss (GND) pin of the MCU.

    I have one question with the ACS712.
    It says in the datasheet that at zero current the o/p =2.5v and there is a positive and negative swing around 2.5v .with respect to i/p AC current.
    Page 6 of the Datasheet (O/p voltage Vs Sensed current)
    If i remove the diode then what capacitor will i need for averaging the o/p voltage?..or rather what should i do in this case to measure a linear increasing voltage w.r.t sensed current

    I am attaching the new modified schematic

    Please get back if you have any suggestions or advice.

    Thanks and regards
     

    Attached Files:

    Last edited: Jun 25, 2009
  21. MrAl

    MrAl Well-Known Member Most Helpful Member

    Joined:
    Sep 7, 2008
    Messages:
    11,029
    Likes:
    951
    Location:
    NJ
    Hello there,


    By modifying dknguyen's circuit a little we end up with a relatively simple
    way to measure current using a shunt and getting a dc output.

    A couple of notes though:
    1. The load has to be able to tolerate a small voltage drop around 2v peak
    plus any voltage drop across the shunt.
    2. The micro controller has to be able to average the pulsing voltage and
    also be sensitive enough to measure the required low level signal.
    3. The circuit wont be good enough if the voltage drop across the shunt is too high.
     

    Attached Files:

    Last edited: Jun 25, 2009

Share This Page