## detecting Zero-Crossing

37 posts / 0 new
Author
Message

I need to detect zero-crossing of 220V 50Hz mains lines. I thought of using ATmega8 comparator by connecting the comparator as shown in the attached figure.

I don't know how to calculate the 1M resistor, it is just a suggestion for the value. I am going to use analog comparator interrupt to detect zero crossing, Is it going to work this way and detect the minus wave as well? If not, any suggestions? I am planning not to use transformers here.

## Attachment(s):

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

Right after you have detected zero crossing, check for rising or falling edge?

RES

Whilst your proposed circuit will work, its not really suited to the real world. Firstly you need some protection and some filtering. The down side is that the filtering will cause some phase shift.

How to calculate the 1M resistor? If we assume the AVR is runnning at 5V so the max volts on the low side of the resistor is 5v, then we get the peak voltage on the high side, 220*1.4142 = 311V. So we get the voltage dropped across the resistor as 311 - 5 = 306. If we assume 1M, the current will be i= 306/1M = 306uA.
You're not going to saturate the AVR's input protection diodes, so 1M is probably quite reasonable. The power disipation is around 1/10th of a watt. You'll want to use a number of smaller value resistors in series, or one large resistor in order to cope with the voltage drop across it. The voltage gradient causes electromigration and the resistor will eventually fail. Use a number of resistors, the voltage acrosss each is much smaller, thus the circuit will last longer.

You'll have to add some transient protection otherwsir you circuit won't last long. Some capacitance and a varistor should help here.

As for rising and falling edge detection - you can detect one edge and use a dpll (digital phase locked loop) technique using a timer to synthesize the other edge and compensate for any phase shift due to filtering. This technique also serves to further filter your zero cross signal from disturbances on the mains.

Please suggest transient protection circuit. For the -ve side of the wave, I still think that it's detectable using this technique; because as long as mains voltage is going to drop below 0.1 volts, it will detect it. same goes for +ve side of the wave, when it reaches 0 it will detect it. The problem now appears to be with transient protection and noise filtering?

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

But this app note only detects pos to neg.

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

ok and??? you can adapt the code to toggle edge?????
so small change must be in your capability...
Thierry

Thierry Pottier

damn it... I forgot there is a rising edge for the interrupt! It's much easier now...

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

Quote:
I am planning not to use transformers here.

There are quite a few threads on "live" circuitry, connected directly to the Mains. You may wish to use the Search feature at the top of this page, and review a few threads. Doing so is conducive to one's long term health.

JC

What I found was useless for me, I've already searched the forum...

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

Quote:
You'll have to add some transient protection otherwsir you circuit won't last long. Some capacitance and a varistor should help here.

At the comparator input? The 1 meg resistor in combination with the chip's clamp diodes is already sufficient. As for noise from the comparator, it only will be noticed near zero, which is where you want to trigger anyway. Simply block interrupts from retriggering for a brief time once the first zero crossing interrupt has triggered.

For resistors that have line voltage across them, I suggest using "metal oxide film" styles, of an appropriate voltage rating, as these cannot catch fire. For capacitors that you might have in a snubber or as a power supply ballast, I suggest metalized polypropylene film. These are compact, available with high voltage ratings, and will not fail shorted/catch fire.

Tom Pappano
Tulsa, Oklahoma

This schematic do not try it in practice.A good approach for zero crossing detection in 220V is to use an optocoupler.Search a little in the web and dont hurry up to finish because the use of the main 220V directly in a project has many risks.Once you find the appropriate circuit that isolates fully you,your proccessor and your equipment from mains then writing the code is the easiest part.

@Metal: I think DocJC suggestions were more directed in your safety side instead of your project side. For some strange reason, I will encourage you to forget to connect directly your electronics to mains without any transfomer. And in my advice, there is only safety and health involved, and not only yours.

Guillem.
"Common sense is the least common of the senses" Anonymous.

I understand your concerns. But this is not the first time I work with mains connected to my electronics, I started with 4061 as a timer and it has been working for 4 years now without any problems, I have to admit that it took me a while to figure out how to work with transformer-less PSU, but it was a good experiment especially after being shocked a couple of times by the capacitor charge. I don't know, may be my vision is not seeing things as you see, explain more to me please. The topic has taken many paths and I am getting more and more confused..

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

With the current circuit - what is the problem with that for your application (of which we don't actually know). For a given mains voltage, the on/off points will be constant so you can just add an offset for the positive->negative transition. You have a choice - add extra hardware or a little software.

I've found I need to filter the zero cross signal as transients will cause a temporary upset by causing afalse trigger and if there is mains signalling used in your area that can really upset things. Thus, the dpll technique I mentioned previously does much to eliminate a lot of false triggering.

My application is a dimmer; simple dimmer for lights and soldering iron as a kick start for this project, I will also add IR support for the lights part.

The problem is that I can not use transformer in the lights part because I will install it inside the wall placing the old switches.

If I am going to use the interrupt, I saw in AVR182 how to filter noise and prevent false triggering, I can implement it to detect both edges by flipping the interrupt edge register and implement this filter for the other edge, which I think easier for me than the first proposed way that uses the comparator. I still have not much thought of how to implement DPLL technique as I have not searched on how to correctly implement it in code, and which is easier for me, also more effective.

If I use ATtiny85, I will have to use the comparator, because I need INT0 for IR.

BTW, what is the recommened resistor-capacitor filtering for zero-crossing hardware?

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

In response to safety advices, I decided to use this circuit for zero-crossing detection. At 90 degrees mains voltage can be as high as 311V. How can I improve on this zero-crossing detector when I use PC817 optocoupler?

## Attachment(s):

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

What do you want to improve? How does a PC817 differ from a PS250?

Wanting to do something like this , i was wondering if the schematic above wouldn't burn off a lot of heat in the 22K's ?
I mean the Ifwd of the Opto would be much more than for the AVR protection diodes.

/Bingo

Hi,

Is it not more simple to use a zero-crossing optotriac like MOC3031M?

then what's the use of AVR?

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

Quote:

then what's the use of AVR?

You tell us--it is >>your<< app.

I guess we assumed that you were going to do something besides zero-crossing detection. E.g. use that crossing point to kick off an A/D conversion on a current-sense channel.

Other than that, it is a good question. Why pick an AVR to do a job that has been solved already, in the "best" (or at least "better") fashion, costs less than US\$1 compared to a US\$2 Mega8, requires no external parts, and has isolation measured in kV?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

About the application: I am planning to make a dimmer, a multi-purpose dimmer actually. I will add an LCD to it as well. Devices controlled are mainly those with resistive loads. I am still looking and searching for nice ideas on how to such a project because there are many ways to do it and there must be a really good way to make it work without problems.

@ theusch:
I don't want to use a triac optocoupler with zero-crossing feature because I will not need it any way as the AVR will be sensing the zero-crossing event upon which controlling how long the triac will be conducting, when the time elapses it will shut off the triac. So when this solution was mentioned, I said what's the use of AVR if I am going to this particular optocoupler. As a matter of fact, even if I use it, I still need a device that calculates timing and controls how long the optopcoupler's LED will be on.

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

metal wrote:

I don't want to use a triac optocoupler with zero-crossing feature because I will not need it any way as the AVR will be sensing the zero-crossing event upon which controlling how long the triac will be conducting, when the time elapses it will shut off the triac. So when this solution was mentioned, I said what's the use of AVR if I am going to this particular optocoupler. As a matter of fact, even if I use it, I still need a device that calculates timing and controls how long the optopcoupler's LED will be on.

1) There isn't any "shutting off" triacs. They turn themselves off at the end of their conducting halfcycle. What a triac power control does is to vary the time between the zero crossing and when you trigger the triac to turn ON.
2) Related to #1, you pulse the optocoupler on, rather than keeping it on.

Hi,

Quote:
the AVR will be sensing the zero-crossing event upon which controlling how long the triac will be conducting

The purpose of zero cross switching is to restrict the interference caused by the triac.

But if you switch the triac on in zero crossing point, you cannot switch it off during a halfperiod, as Levenkay wrote.

Still you can use it as a power regulator.
E.g. use pwm with period say 300 ms to control on/off ratio.
Of course this method is not suitable for lights.

Edit:
And if you could switch the triac off during a period then the advantage of zero cross switching would be off.

You mean that switching a Triac off during a period causes interferences on mains lines, that's good to know, thanks.

I was also wondering about the period of switching for 50 Hz, is it really 10ms for half cycle and I can trust the timer to do it as 10ms, what if the frequency changes a little bit, what would happen, how can I prevent this?

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

You CAN'T switch a triac off. Once they conduct, the stay on until the voltage drops below there threshold voltage.

When you control power by turning a triac on in the MIDDLE of a cycle, you cause noise because the voltage and current are no longer balanced and properly phased. If you only trigger a triac at zero crossing, you maintain the power cycles symmetry, but you can't control the power to things light lights due to the HUGE flicker.

Clint

Quote:
When you control power by turning a triac on in the MIDDLE of a cycle, you cause noise because the voltage and current are no longer balanced and properly phased.

This is a new one! Could you explain this in a little more detail?

Tom Pappano
Tulsa, Oklahoma

The interference is caused by sudden transition of current to zero when triac is switched off, or from zero to full value when switched on.
At zero crossing there is no current and thus triac switches without interference.

Quote:
I was also wondering about the period of switching for 50 Hz, is it really 10ms for half cycle and I can trust the timer to do it as 10ms, what if the frequency changes a little bit, what would happen, how can I prevent this?

The halfperiod is 10 ms and you can relay on it in a short time interval. The deviation is never greater than 2% here.

You can detect the zero crossing, then count a delay and switch the triac on.
Actually 2 delays, one for positive and the other for negative halfperiod.

But in this case you only replace the potentiometer in the classic schematic with an AVR.
I do not see any advantage, unless there would be some feedback.

Edit:
I cannot help myself.
It would be most curious if you used a potentiometer to control the Avr.

Last Edited: Wed. Nov 11, 2009 - 06:46 PM

Quote:
I was also wondering about the period of switching for 50 Hz, is it really 10ms for half cycle and I can trust the timer to do it as 10ms, what if the frequency changes a little bit, what would happen, how can I prevent this?

If your Avr clock changes in frequency, it will also change your timers. Where this can be a problem in a dimmer is at *low* brightness settings. You delay until near the end of the cycle, which results in a brief triac on period. If the clock is slow, instead of triggering near the end, triggering could occur near the beginning of the next cycle resulting in a long on period. If not using a crystal, you can compensate for clock drift by measuring the line period during operation.

Tom Pappano
Tulsa, Oklahoma

Wait.. According to what said above:

I have to switch the Triac at zero crossing by giving a pulse to the gate, right ?

Okay, then I have to count the period of time I want the Triac to stay off after zero-crossing then give the Triac a pulse to switch on. Is this going to cause noise on the mains lines ? If so, then what advantage did I get when using zero-crossong techniques over the ordinary Resistor-Diac-Triac ?

Seems I misunderstood what the real idea behind using zer-crossing? Can some experienced shed light on this ? I remember from what I read about this issue long time ago is that if Triac switches on at 90 degree for example it will cause big noise on the mains, I still don't understand how it was solved using zero-crossing techniques.

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

lets no get confused with switching at zero crossing and phase control. You switch at zero crossing to minimise the generation of interference but this is not phase control. With phase control, you need the zero crossing signal as a reference in which to time the turning on of the triac. Obviously, switching the triac on during the phase will cause a fast change of current thus generating interference - one would normally use an inductor (choke) to limit the rate of change.

There are plenty of examples of microprocessor based dimmers on the web, some using AVRs. From what I've seen these are not commercial quality as their filtering leaves a lot to be desired. As I've previously mentioned, I use a DPLL technique to track the mains and to recover a filtered zero cross event. This involves getting a zero cross signal ( you only need a rising or falling edge) into the input capture of the AVR. A compare interrupt reads the capture event to correct its compare value for both frequency and phase as well as timing the triac firing.

A quick google with "avr light dimmer" found this:

http://domotica.homeip.net/dimme...

He he, zero crossing technique solve the noise problem but there will be no dimming at all.
A zero crossing triac, as mentioned above, will not turn ON right at the moment you energise the gate, but will wait for the first zero crossing to turn ON. In this way, there will be no noise on the line, but no dimming.

If you want dimming, you have to turn a regular triac ON, somewhere during the semiperiod. The triac needs a short pulse, you no need to keep the gate energised whole left part of that semiperiod. It will stay ON and switch OFF at the first zero crossing automatically.
In other words, if you turn it ON later, less time to stay ON, so less power to your load. If you turn it early, right after zero crossing, then more time to stay ON, so more power for the load.
Look on internet for UAA145 and in that data sheet you will see how should be done.

Regarding that schematic with two 22K resistors, you will have 1.1W dissipation only on those two resistors. Plus, near the zero crossing the current through the optocouplers diode will be so small, that will give you a false information. And it will survive the reverse voltage of 310V ?

I would connect the whole circuit directly to the power lines and forget about isolation. Does CFL lamp ballast is isolated? Does KillAWatt power metter is isolated ?

One thing to remember, when you program your microcontroller, disconnect it from the power lines. I burned an ATAVRISP mkII and the PC motherboard in this way. Since then, the test circuit I keep far from the PC, so I need to disconnect it in order to connect to the programmer.

George.

Then what? To use the 22K resistors method or not ?

The idea of capture and compare to correctly get the real values of cycle periods seems fair enough regardless of the mains frequency, nice idea indeed.

Signature: We need more peripherals in DIP packages. Namely, USB, 12-bit ADC, 16-bit timer, cheaper tool as a programmer/debugger coz STK600 is expensive! Atmel Studio 5/6 sucks! coz it brings MS visual studio crap to AVR world..

Quote:
A zero crossing triac, as mentioned above, will not turn ON right at the moment you energise the gate, but will wait for the first zero crossing to turn ON. In this way, there will be no noise on the line, but no dimming.
True, this triac is designed mainly as a plain switch without interference.

It can only control the power of heating devices like solder irons etc this way

start:
gate=1 (switch triac on)
wait 300 ms
gate=0 (switch triac off)
wait 300 ms
goto start

The power is controled with on/off ratio.