Zero-crossing detector according to an Atmel Application Not

Go To Last Post
25 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi,

I was reading trough an Application Note by Atmel:

http://www.atmel.com/dyn/resources/prod_documents/doc2508.pdf

This document describes a very simple and cheap way of implementing a non-galvanically isolated zero crossing detector by wiring 230VAC directly to the MCU pins via 1M resistors. The idea is that the MCU's integrated ESD diodes force the pin voltage to roughly between Gnd-0.5 and Vcc+0.5 V.

This looks so simple that I decided to try it. But I noted that the document uses the external interrupt pin for input, which means that it will trigger an interrupt when the input voltage exceeds ~1.5V. I was wondering why they did not use the analog comparator pins AIN0 and AIN1 (wired to signal and ground) to achieve a comparator interrupt accurately when the signal crosses zero? Can you think of any reason why this would not be better than using the external interrupt pin? I would appreciate having as precise timing for the zero crossing as possible, and if the timing depends on the input voltage amplitude, it will also vary due to the normal variations of the mains voltage on the electrical network.

-slintone

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Direct Mains wiring can be tricky, and dangerous. Be very careful! You would do well to run the Mains through an isolation transformer and a GFI before feedng your circuitry whle doing your testing.

There are many ways to solve problems. The Application Note took one approach, you have considered another.

You may want to consider putting a small RC filter on the input to eliminate some of the high frequency noise on the line, and hence reduce the jitter in the measurement, if you really need super-precise detection of the exact moment of zero crossing.

Interesting math question, what is the delta T for crossing the 1.5 V threshold, (or whatever), for a 230 V Mains sin if the input varies by +/- 20%. Note that at the zero crossing the sin wave is at its "steepest", and the delta amplitude at 90 degrees will have little impact on the time of the threshold crossing...

All comes down to "how accurate?"

JC

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, this sure is not the safest way. I have examined the possibility to use an optocoupler for zero-crossing detection, but to drive the opto LED at 230VAC means massive power losses (over 1 watt) in the LED series resistor in order to produce suitable current. Also, the optocoupler seems to be way more inaccurate in many ways (current tranfer ratio depends on temperature and aging etc...). So it seems that this direct-to-mains approach is the only accurate solution.

I have calculated that the necessary zero crossing timing accuracy for my purposes would be +/- 10 microseconds.

About your interesting math question: I calculated roughly that at +/-10% mains voltage fluctuation will present an error of about +/- 1.3 us in the case of 1.5V logic threshold. So this Atmel's approach would be accurate enough. Adding RC filtering reduces accuracy somewhat unless I use very accurate resistor and capacitor (NP0). At this point I was planning on using a varistor (MOV) between L1 and N lines. RC filtering is also possible, but as there is already a 1M series resistor, the capacitor will need to be very small and accurate (something like 10pF NP0).
I may also add a ferrite bead in series for higher frequencies. Starting to sound as enough?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Zero crossing compared to your ground reference is not necessarily the point of AC current reversal. For that reason and for isolation I like a small toroid with enough primary windings to saturate quickly; you get a pulse on the secondary when the flux flips and you can also adjust the phase with a separate DC winding. I seem to remember a PIN diode in the secondary gives lower jitter.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Interesting... You mean that the N line of mains has similar tolerance for being not precicely at 0V as the L lines have for voltage amplitude?

But wouldn't it be possible to detect and calculate out this error by measuring the "apparent" DC offset of the L voltage against N?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Another question: have you used custom made toroids, or can you recommend a part for this kind of isolated measurement?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The phases don't always have the same maximum voltage, e.g. if they have different loads to neutral somewhere else. And any power flow through the neutral will cause AC ripple with respect to ground which could be several volts on a long run.

Around 1cm toroids are the smallest I've used, salvaged from from switching supplies like computer motherboards. I like to think putting two layers of electrical tape over the primary brings them up to code ;)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or use a capacitive divider. The mains frequency is 50 or 60Hz so you can calculate what impedance the capacitor has. Usually a capacitor and a resistor is used. This way you won't have massive power dissipation.

>----||----+----/\/\/\----+
    100nF  |     100      |-----+
           |              --   --
           /         opto \/   /\ 1N4148
           \  1K          --   --
           /              |     |
           |              |     |
>----------+--------------+-----|
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jayjay, that looks very interesting. What about the capacitance tolerance and aging effects? I'm a bit worried that the optocoupler's triggering timing will drift because of the capacitance loss with aging. I quickly simulated with a 4N25 optocoupler and your schematics, that if the 100nF capacitance drops to 50nF, the triggering of the opto will drift over 200 microseconds. That is four times too much error for my purposes, but I'm not really sure how fast X2 rated capacitors derate in 24/7 use (if at all).

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you trigger on the "trailing edge" of the sine wave (just before ZC) rather than the "leading edge" (just after ZC) you can add whatever small delay you need to get the precise ZC point you desire.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmm, I fail to understand why the raising/falling edge would make much difference. My understanding is that the timing drift happens because the lowered capacitance lowers the current amplitude passing trough the opto's LED. As the current is a sine wave, the critical point where the opto's transistor becomes conducting enough to pull a logic low, will be delayed. I think this effect must be present in both directions?

I tried to simulate this method, and it seems to be working. But what are the resistors for and how to calculate optimum sizes for them? EMC? Also, can anyone recommend an optimal optocoupler for this use? I used 4N25 just because it was available in the simulator software. I would need only one pulse per 360 degrees, so an AC-style dual-LED opto would not do.

Attachment(s): 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What exactly is the application that needs exact phase information?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

slintone wrote:
Hmm, I fail to understand why the raising/falling edge would make much difference. My understanding is that the timing drift happens because the lowered capacitance lowers the current amplitude passing trough the opto's LED. As the current is a sine wave, the critical point where the opto's transistor becomes conducting enough to pull a logic low, will be delayed. I think this effect must be present in both directions?

Your OP never talked about capacitors, only resistors and 1.5V thresholds. Thus my comment that if you trigger as the limited and squared-off sine wave comes down from Vcc+ to GND, that 1.5V transition will happen slightly before the sine wave ZC.

EDIT - I just looked at the appnote you cited and what I'm saying is illustrated quite clearly in Fig. 2. You can see that the trailing edge of the squared-off sine wave happens before the ZC point. The appnote says "The rising edge of the square wave is slightly after the crossing and the falling edge is slightly before the crossing."

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The application would be a monitoring system for a three-phase powered industrial appliance. I need to measure certain machine parameters very accurately at specific input voltage angles in order to present information about it's condition. I could just measure voltages and trigger the measurement at a certain voltage, but as the peak voltage of a 230vac network can fluctuate quite a much, I'll rather trust the frequency (which should be very accurate) and take the measurements by timing them from the zero-cross.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

10us of phase change can be visibly seen in an incandescent lightbulb. If you try to detect zero cross near the actual zero cross point you will get a lot of jitter. You are much better off using a phase locked loop to recover the actual zero cross and in the process you end up filtering out a lot of jitter and get get both zero crossings. If you use phototriacs you need to allow some turn off time before zero crossing and not to fire too early after zero crossing as the voltage may not be great enough to meet the holding current for the triac.

For my professional light dimmers I recover the mains on the secondary of the power transformer and compensate for any phase shift in software - again the phase locked loop technique makes this all quite easy.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kk6gm wrote:
slintone wrote:
Hmm, I fail to understand why the raising/falling edge would make much difference. My understanding is that the timing drift happens because the lowered capacitance lowers the current amplitude passing trough the opto's LED. As the current is a sine wave, the critical point where the opto's transistor becomes conducting enough to pull a logic low, will be delayed. I think this effect must be present in both directions?

Your OP never talked about capacitors, only resistors and 1.5V thresholds. Thus my comment that if you trigger as the limited and squared-off sine wave comes down from Vcc+ to GND, that 1.5V transition will happen slightly before the sine wave ZC.

EDIT - I just looked at the appnote you cited and what I'm saying is illustrated quite clearly in Fig. 2. You can see that the trailing edge of the squared-off sine wave happens before the ZC point. The appnote says "The rising edge of the square wave is slightly after the crossing and the falling edge is slightly before the crossing."

Sorry for the confusion, I thought we were talking about the capacitor+resistor+optocouplet idea. I completely understand what the app note is saying, and we all can agree that it's method is accurate enough (but not perferct: the mains voltage amplitude fluctuations will cause a small delay error here too as discussed before). We were just searching for an alternate way that would be galvanically isolated and still almost as accurate...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
10us of phase change can be visibly seen in an incandescent lightbulb. If you try to detect zero cross near the actual zero cross point you will get a lot of jitter. You are much better off using a phase locked loop to recover the actual zero cross and in the process you end up filtering out a lot of jitter and get get both zero crossings. If you use phototriacs you need to allow some turn off time before zero crossing and not to fire too early after zero crossing as the voltage may not be great enough to meet the holding current for the triac.

For my professional light dimmers I recover the mains on the secondary of the power transformer and compensate for any phase shift in software - again the phase locked loop technique makes this all quite easy.

I will not be firing triacs, just taking some very timing-critical adc-measurements. This "phase locked loop" technique sounds very interesting, can you point me to more information about it?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can use a ic like a CD4046 to form a phase locked loop circuit that will track and lock to the mains frequency. You could then connect the output of this to the input capture or an external interrupt on the AVR. The other method is to do it in the digital domain using the timer compare and input capture features. Basically set up a compare to the period that you want (10ms if your mains is 50Hz). This compare has the interrupt enabled. In this ISR, read the capture flag to see if an input capture has occurred. Subtract this value from the previous capture value (which you stored earlier). This gives you the period which should be around 20ms for 50Hz. Do some checks to see if this period is within acceptable bounds and ignore if it is out of bounds. Filter this period value using a low pass filter ( new = (k * prev) + (1-k)* new) ) the output of this you divide by two since we're running what is effectively a 100hz oscillator and this value is the new delta value for the compare. This gives us the frequency tracking so our compare oscillator will track the input capture frequency but only within certain bounds and at a filtered rate. Next trick is to align the phase of our compare oscillator with the incoming frequency. Compare the capture time with the compare time and get the difference. If the value is too small (ie the error is small) do nothing, whereas if the error is too large, peg it at a max value as we don't want to correct the phase too quickly. The error is signed (+ or -) so add that to the next compare time. Basically we tweek the frequency to bring the phase inline but we want to do it gently. If we're too agressive we get overshoot and the control loop will oscillate. After a few cycles the phase will align and will 'lock in'. You can use the sound card on you PC as an oscilloscope to see what happens by comparing the input capture signal vs the timer compare signal. To compensate for phase errors in the input capture signal, we add an offset into our phase error calcs. You end up with a fairly busy compare isr but you end up with a zero cross signal that is immune to some mains interference and short term jitter.

For your measurements, a bit of extra code to add in an extra compare to fire off the adc at the required phase angle and your problem is solved.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman: thank you very much! I think that your suggestion to do phase locking in software looks great, and I will surely try to implement a similar system.

jayjay1974: I actually built the capacitor+resistors+optocoupler circuit that you suggested, and which is shown also in my simulation picture couple of messages back. It's working OK at the moment, the signal is a bit noisy but with some cycle-to-cycle averaging it might be accurate enough. I'm also getting a bit of a drift, the measured zero-cross point has shifted about 200 microseconds in the couple of hours that I have had the scope on. I need to continue monitoring to see what is really happening...

I have also been reading datasheets of X1 and X2 rated polypropylene film capacitors, as I reckon this is the only correct type for this kind of application. I'm used to calculating lifetime of aluminum electrolytics, but what is the situation with these film capacitors? There seems to be no lifetime hour ratings on their datasheets? How do these usually stand up in 24/7 use? I don't fancy the idea of coming back to rebuild the measurement system after one or two years...

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hmm, I left the test rig on for the night and it seems to be somewhat unstable. The detected zero-cross point keeps slowly shifting randomly by -/+ 200 microseconds. I guess this is a combination of the optocoupler's temperature sensitivity, mains voltage and frequency fluctuations and possibly capacitance unstability of my X2 capacitor. Some of this may be correctable in software, but having so many negative points (like questionable long-term reliability) I think I'll still consider going back to non-galvanically isolated solution.

Meanwhile I found a new type of solution candidate:
http://www.farnell.com/datasheets/92348.pdf

This is a magnetical "digital isolator", which claims much better performance than optocouplers. It provides only basic insulation, but that would be enough for me, as I can easily provide supplementary insulation between the board and touchable parts. This might be wort prototyping with.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If you need that precise a voltage measurement, I would simply measure the mains directly, maybe clamped by a zener diode, with a comparator that feeds the opticolpleur/magnetic isolator, or one of those fancy capacitively isolated opamps, like the AD202.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
Meanwhile I found a new type of solution candidate:
http://www.farnell.com/datasheet...

This is a magnetical "digital isolator", which claims much better performance than optocouplers. It provides only basic insulation, but that would be enough for me, as I can easily provide supplementary insulation between the board and touchable parts. This might be wort prototyping with.


For this part you need to power the IC, and this complicate your design.

I would go with Atmel's hardware solution and I would add what Kartman suggest.

I use something similar to synchronise one relay to turn it ON when the voltage cross zero. I used ADC. After 5-10 semi cycles it synchronise and turn the relay ON in advance with the time the contacts effectively close. The relay it needs around 4.5 - 5 ms to close its contacts. After this, I can measure the AC voltage permanently.

George.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am agree with angelu. it is complicated design without give you so great resultat.
If you do not need isolate cross detection a lot solution with 2 transistor make a sort of bridge and generat en pulse for 50-100us (depand of resistance value +++).
The trigger voltage with transistor is about 0,7 to 1V
Yes of course you can see some few us difference between main voltage from 180Vac to 260Vac but the pulse is centrert between the zero crossing and with SW you can calculat the rigth time.
Thierry

PS if you are interesting with this I can draw it but not to day, no time sorry

Thierry Pottier

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

done schema...
hope it is some help.
Thierry

Attachment(s): 

Thierry Pottier

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't understand why you're concerend about the capacitor. It is a paper dielectric and should be very stable unless it gets too many transients and starts to break down and heal itself - that's the X2 spec. Its failure mode is totally different to an aluminium electro that suffers from the electrolyte boiling off over time. If you're worried about the opto drift over time, use a transformer - there's little to drift or degrade.