Shop OBEX P1 Docs P2 Docs Learn Events
Line Frequency Detection / Measurement — Parallax Forums

Line Frequency Detection / Measurement

JonnyMacJonnyMac Posts: 9,182
edited 2012-03-31 15:18 in Propeller 1
This should be easy -- I've done it before with the SX but with the Propeller I'm getting sporadic, unexpected results. What I would like to do is detect actual line frequency at start-up of my phase-dimmer object so that the same code will work world-wide. Again, should be easy.

Pics show the circuit (no-brainer, everyone uses it), output from collector/pull-up junction, and wacky sporadic results in test program.

All thoughts and suggestions are appreciated.
dat

                        org     0

entry                   mov     outa, #0
                        mov     dira, chdirs

loop                    waitpne zcmask, zcmask
                        waitpeq zcmask, zcmask
                        neg     period, cnt
                        waitpne zcmask, zcmask
                        waitpeq zcmask, zcmask
                        add     period, cnt
                        sub     period, #4

                        wrlong  period, par

                        rdlong  period, #0                      ' 1/4s delay
                        shr     period, #2
                        add     period, cnt
                        waitcnt period, #0
                        jmp     #loop
677 x 443 - 34K
1024 x 640 - 64K
1024 x 640 - 89K

Comments

  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2012-03-29 15:16
    I've only got a minute but I'd say that you have a combination of slow edges and very fast resolution. The Prop's counters are hooked up on caffeine and what it sees is the transition as going by like a river of molasses. It went by, no wait there it is, no wait?^*#$? What the circuit needs is either hysteresis or introduce a delay between pairs of waits. Just a quick thought. So wait till it goes high, wait 100us, wait till it goes low.
  • BeanBean Posts: 8,129
    edited 2012-03-29 15:17
    Jon,
    Seem like there may be some jitter on the signal when it crosses the threshold voltage. The SX may not have been fast enough to see the jitter, but the propeller is.

    I'd try putting a couple millisecond delay between all the WAITPNE and WAITPEQ.

    Just a guess...

    Bean

    P.S. Peter beat me to it...
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-03-29 15:26
    Jon, there could be noise on the power line, in which case the Propeller's speed is your enemy. Try a Schmitt trigger to de-glitch your input.

    -Phil
  • Beau SchwabeBeau Schwabe Posts: 6,568
    edited 2012-03-29 16:08
    Jon,

    I agree with the other posts... also if you have any X-10 devices connected anywhere in the house they could be causing this sort of behavior. Even some of the 'Smart Grid" equipment that communicates during the zero crossing might potentially cause this sort of behavior. Have you scoped your mains, to see just how clean it is?
  • JonnyMacJonnyMac Posts: 9,182
    edited 2012-03-29 16:59
    I wasn't willing to hook my PC-based 'scope to the mains so I assumed my friends' supposition about noise was correct. I don't have an X-10 or other devices operating in my apartment but I do live in a small building and I have no idea what my neighbors are doing. Turns out a bit of debouncing does the trick.

    Am I fooling myself?
    org     0
    
    entry                   mov     outa, #0
                            mov     dira, chdirs
    
    waitlo1                 mov     t1, #10
    :loop                   test    zcmask, ina             wc
            if_c            jmp     #waitlo1
                            djnz    t1, #:loop
    
    waithi1                 mov     t1, #10
    :loop                   test    zcmask, ina             wc
            if_nc           jmp     #waithi1
                            djnz    t1, #:loop
    
                            neg     period, cnt
    
    waitlo2                 mov     t1, #10
    :loop                   test    zcmask, ina             wc
            if_c            jmp     #waitlo2
                            djnz    t1, #:loop
    
    waithi2                 mov     t1, #10
    :loop                   test    zcmask, ina             wc
            if_nc           jmp     #waithi2
                            djnz    t1, #:loop
    
                            add     period, cnt
                            sub     period, #4
    
                            wrlong  period, par
    
                            rdlong  period, #0
                            shr     period, #2
                            add     period, cnt
                            waitcnt period, #0
                            jmp     #waitlo1
    


    Update: Well... that seems to have solved the problem and having installed this into my 4-channel AC dimmer object everything seems to be working well. Of course, I will spend a lot of time wringing it out before going public. The attached image is the board I'm working with (prototype of the EFX-TEK FC-4+).
    640 x 780 - 144K
  • jmgjmg Posts: 15,182
    edited 2012-03-29 18:18
    JonnyMac wrote: »
    All thoughts and suggestions are appreciated.

    Getting reliable Zero crossing on mains, is non trivial.

    If you just want to make a Frequency decision, you can do a voting system, and discard the 'silly answers'
    - ie a form of simple digital filtering.

    If you want a more reliable phase-zero, for phase control, then you should make a bandpass filter, that removes spikes but does not phase shift too much at mains frequencies.

    eg a series RC gives a phase-lead, and then a shunt RC gives a phase lag.

    The toughest test situation is with three phase power, where another phase switches real power, right where you hope zero is.
    JonnyMac wrote: »
    Turns out a bit of does the trick.
    A bit of ... what ?
  • JonnyMacJonnyMac Posts: 9,182
    edited 2012-03-29 18:52
    Turns out a bit DEBOUNCING of does the trick. Sometimes my fingers work faster than my mind. We are going to put a bit of filtering on the input to the Propeller but I'm hoping that the code trick is enough for bad situations. I'm happily running a set of incandescent lights up and down now.
  • jmgjmg Posts: 15,182
    edited 2012-03-29 19:14
    JonnyMac wrote: »
    The attached image is the board I'm working with (prototype of the EFX-TEK FC-4+).

    I'd be cautious about those metal tabs just waving up in the air... anyone can slip..

    Can you not get the devices in the TO220F or equivalent package ?
  • JonnyMacJonnyMac Posts: 9,182
    edited 2012-03-29 19:37
    We're using triacs with insulated tabs.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-03-29 19:41
    Jon,

    Looking at your board, I'm concerned about your mains isolation. It's not enough just to use optoisolators. You also have to maintain wide separations between conductors on the isolated vs. non-isolated sides of the circuit. In particular, you have a groundplane that surrounds the mains signals that really should not be there. Also, there's a diagonal trace emanating from the H11AA1 chip that runs perilously close to the hot side of the isolator and one of the 15K power resistors. In extreme cases, I've even seen slots cut into PCBs between the two sides to eliminate arc paths on the surface. That's probably not necessary here, but I mention it just to underline the importance of conductor separation distances.

    Here's an article that might help:

    -Phil
  • Duane C. JohnsonDuane C. Johnson Posts: 955
    edited 2012-03-29 20:07
    Hi JonnyMac;

    Filtering greatly helps, but then the rise-time is even slower.

    The Schmidt trigger, along with filtering is the best solution.

    Usually we use a hardware gate with Schmidt trigger gates.
    However, with the Prop one can make a Schmidt trigger using 2 pins.
    Forgive me but I don't remember what the obex is but it was describer here recently.

    Basically one pin is the input and immediately the value is copied to a second output pin.
    There is a resister from the output pin back to the input pin which changes the input
    bias trip point. This greatly decreases the likelihood of false triggers.

    Duane J
  • JonnyMacJonnyMac Posts: 9,182
    edited 2012-03-29 20:09
    @Phil: John Barrowman (also a former Parallax employee) did the layout and we've done UL approved HV boards in the past for clients. Still, I appreciate the feedback and will forward to JB. This is our first prototype and we have already identified some adjustments. Gotta love GoldPhoenixPCB for quick-turn, high-quality, low-cost prototypes.
  • JonnyMacJonnyMac Posts: 9,182
    edited 2012-03-29 20:11
    @Duane: I will have a look for that in ObEx; I have a spare pin and as this is a first prototype, we can make adjustments.
  • jmgjmg Posts: 15,182
    edited 2012-03-29 20:45
    Also, there's a diagonal trace emanating from the H11AA1 chip that runs perilously close to the hot side of the isolator and one of the 15K power resistors.

    Yes that is certainly marginal, perhaps the Layout designer thought the resistors were isolation ?

    8mm is the creepage distance some use, but certainly you should maximize within practical reason.

    I'd also spread the 15K power resistors apart further; most are rated standing alone.
  • Duane C. JohnsonDuane C. Johnson Posts: 955
    edited 2012-03-29 20:46
    The Prop Schmidt trigger is directly related to the Sigma-Delta AtoD.
    Except, the driving pin output is in phase with the sensing pin deliberately deviating from the trigger point, as opposed to, in the D-S AtoD, which is always attempting to drive toward the trigger point. I.e, NON-INVERTING output as opposed to INVERTING output.

    Duane J
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2012-03-29 20:52
    Looking at the board again, I may have mistaken glare from the lighting for a groundplane. If that's the case, please forgive the misrepresentation. But that diagonal trace is still a concern.

    -Phil
  • Peter JakackiPeter Jakacki Posts: 10,193
    edited 2012-03-29 21:40
    That diagonal trace is certainly a big concern, it seems to compromise all the isolation that you have elsewhere. This would need to be fixed for the production version at least, I cannot see how this would pass compliance testing as moisture and dust and aging have to be taken into account as well as indirect lightning effects. But I notice the rather large 15K resistors used in the zero-cross detection and it seems to me that this is an area that could be improved. The grade of opto could be improved and the resistors increased in value and decreased in size or you could use a darlington output version (no need for speed) and far less current. The resistor on the output could be easily increased and a capacitor added for pre-filtering. Just some thoughts but we all know that whatever we come up with somebody always spots something that could do with improvement :)
  • lmclarenlmclaren Posts: 104
    edited 2012-03-30 02:06
    I use a propeller to phase dim 40amps mains with 100 step resolution. It controlls a heating element and a universal motor. The trick with zero crossing is to use two opto couplers with the inputs reversed on one.

    Connect both outputs together and to a pullup resistor. Now the either opto is turned on all the time except for the very short interval between -0.6 and + 0.6. Works like a charm and is not sensitive to load or change.
    As you can imagine, I am generating a lot of motor noise and the dimmer is rock solid.

    DONT FORGET THE CURRENT LIMITING ON THE INPUT! (I know you wont but need to say it)

    Lee
  • PaulPaul Posts: 263
    edited 2012-03-30 06:34
    @lmclaren, Could you use a PC814 with dual (but opposite) LEDS for this purpose? Or do I have your circuitry backwards? We currently use the PC814 to detect a phase loss in a 3-phase system.

    EDIT: Just noticed the PC814 and H11AA1 are similar. ...never mind.
  • jmgjmg Posts: 15,182
    edited 2012-03-30 13:45
    Certainly the 15K can be increased, and the Secondary load can also go up, it just needs to be safe relative to the leakage currents. Somewhere in 100K-1M region ?

    Interesting is the CTR symmetry (skew) on these parts, and not all are equal. Some say 2:1, or even 3:1! and I see an Everlight 814A says 0.7<1< 1.3;
    If this is only for AC frequency that does not matter, but for true zero cross as Phase Zero, you really need a known balance, and probably a Centre of Two edges design to take OptoGain out of eqn.
    Least skew would be from a single LED driven from a bridge, but I've not found a vendor doing that.

    My prize (by far) goes to Everlight EL814SATA 16c/500 at Mouser, and the best symmetry value.
  • JonnyMacJonnyMac Posts: 9,182
    edited 2012-03-30 15:28
    Bear in mind that I want this to work in Europe and Australia where our customers could not use the previous version of the FC-4. We set the circuitry to work at 220v, and the AC dimmer code detects line frequency on start-up of the object.
  • jmgjmg Posts: 15,182
    edited 2012-03-30 15:52
    JonnyMac wrote: »
    Bear in mind that I want this to work in Europe and Australia where our customers could not use the previous version of the FC-4. We set the circuitry to work at 220v, and the AC dimmer code detects line frequency on start-up of the object.

    I was assuming that, which is why the 15K is a little on the low side, and also why a better spec Opto coupler matters.
    I'd flip to SMD couplers as they remove voltage from more of the board area, and likely have better long term availability.
  • JonnyMacJonnyMac Posts: 9,182
    edited 2012-03-30 17:21
    All the optos on the board are SMD -- "gull wing" versions of the DIP packages the manufacturers offer.

    The board shown is a prototype (we try to do those nicely, saving work later) and have identified some changes (in addition to the comments in this thread). I will try the EL814SATA for the next run.
  • waltsailingwaltsailing Posts: 40
    edited 2012-03-30 19:00
    Hi

    Why not do it a different way, using the following, make an array, 1024 points, make an a/d out of a pin, sample the signal into the array, for say 1024 points... Set the sample rate so that you can easily resolve 50 or 60 hz, Maybe a 1000 Hz Sample rate... Then do an FFT, go and take the magnitude, Set a threshold, or search the array for the peak in the bins near 50 or 60 hz, and decide which is largest. The harmonics, and other parts of the signal are not that important, because you will effectively filter out the junk and will be able to decide what the line frequency is by looking at the fundemental and knowing about where to search. Right selection of sample rate, makes the index = the frequency. The fundemental of the line frequency will be clearly visible. You dont need to do this all the time, just once in the beginning. Perhaps you could verify that the line freq does not change much, either by running this more than once. I have not written this in Spin, but there are objects that do most of it. and the prop should be able to do this pretty easy. There is no need to really worry about real time, provided the sampling is done at a known fixed frequency. I dont think it matters much that that the signal is not a nice sine wave, the fundemental will show up just fine.

    Just a thought, *** this would eliminate all the timing issues.

    This is an interesting thing to read. Good luck with your project,

    Regards,
    Walt,
  • jmgjmg Posts: 15,182
    edited 2012-03-30 19:16
    FFT is possible, but an over-kill - here you have only one primary frequency, for which you already know the two main possible candidates.
    A design usually wants a (stable) Zero Cross time reference anyway to base the Phase calcs on.
    So it is easier to work from this base.
  • pjvpjv Posts: 1,903
    edited 2012-03-31 11:49
    Hi Jon;

    I make a product that is targetted for 50/60 Hz and 110/240 volt operation around the world. It (still) happens to use an SX chip, but the same approach could be used for probably any micro.

    The mains powers the micro directly through a "line rated" 1 uF capacitor into a power limiting 12 volt zenering transzorb, from which point it is series dioded to a 100 uF bulk storage capacitor that feeds a 5 volt regulator.

    Beauty of this scheme is that the 1uF capacitor is a reactive limiter, and hence no heat is generated in dropping the 110/220 line voltage to the 12 volt zener.

    Then I simply connect the hot side of the line through a couple of 1 meg resistors to a micro input pin as my phase reference. The input threshold is 2.5 volts, and the slew rate of the mains at that point is very fast, yielding wonderfully reliable results. On bootup, I first count and average the number of clocks between transitions on the phase input, and then adjust my master processor timing delay to get 128 time slots per half cycle, and sequence everything from that.

    Works great.... thousands sold!

    Cheers,

    Peter (pjv)
  • JonnyMacJonnyMac Posts: 9,182
    edited 2012-03-31 12:55
    My [timing] code is doing the same, thing, Peter; though I'm dividing each half cycle into 256 slots.
  • jmgjmg Posts: 15,182
    edited 2012-03-31 15:18
    pjv wrote: »
    Beauty of this scheme is that the 1uF capacitor is a reactive limiter, and hence no heat is generated in dropping the 110/220 line voltage to the 12 volt zener.

    No series surge resistor ? It may avoid any heat, but the power factor is lousy, and it will cost more to operate this over its life.
    I guess if it is in kW dimmers, the control cost is in the noise floor, but for other applications, this does need care.

    You also should provide a discharge path for the 1uF, if it is plug-able, otherwise it can sit hot waiting to bite someone...
    pjv wrote: »
    Then I simply connect the hot side of the line through a couple of 1 meg resistors to a micro input pin as my phase reference. The input threshold is 2.5 volts, and the slew rate of the mains at that point is very fast, yielding wonderfully reliable results.

    We have used the VR37 series of high voltage resistors for this, but usually no less than 4M7, as you need to keep well clear of Earth-leakage tests of 250uA. Digikey shows 10M and 3M3 and 4M7 as stocked in tens of thousands.
    Some designs use two VR37 in series, for better spike (lightning) protection.

    I see the Prop specs +/- 1uA MAX Leakage, and Clamp max of 500uA in IO pins.
Sign In or Register to comment.