# Detecting Zero Crossing of Mains (Sine and Square Wave)

I need to design a circuit that can measure and output pulses for a MCU at zero crossing of 220VAC waveforms. The application requires that that both square wave waveforms and sine-waves should work. The square waves are important due to circuits being powered off cheap UPSes here.

I am familiar with the simple 'AC input' opto-isolators + resistors approach. That has worked very well for sine waves but in a square wave, the waveform crosses the zero point much more quickly and my circuit has very little time to react, it seems. Here's the circuit:

Based on this circuit I thought that slowing down the square-wave might work. So I designed a low-pass filter in addiion to replacing R1 and R2 with some capacitors to reduce power loss. Resultant circuit:

I don't quite remember the values for the LPF so the values in the schematic are just a guess. Note that this circuit does work as the square-wave is basically slowed down. It does have some phase shift but as long as that's constant, I'll just program it out into the MCU. I hope capacitor tolerances won't bother that too much.

My question is: is there a better way? The circuit has a switch mode power supply so no traditional transformer where I could have done zero-cross detection with a comparator - I do require isolation here. I'm also OK with the idea of a dedicated transformer provided it's very small and is able to pass the sine and the square wave.

• I am not very familiar with opto-isolators, but if R2 is grounded with input on R1, how are the LEDs turning on the transistor? And with your second configuration, when R2 is grounded and input on R1 again, how is R1 and C3 forming a LPF with R2 in series? And wouldn't C1 and C2 affect the filter characteristics? I need to become more familiar with different filter configurations but I just don't see how this is working to slow down the square wave. Do you know of any write-ups that may clarify? Aug 15, 2014 at 15:40
• @sherrellbc: The capacitors will eventually settle at half-charge average (with respect to the waveform), which will remove the other side of the LEDs from ground. The filter becomes more complex with the addition of other elements, but doesn't neutralize entirely (unless you manage to break it very badly). Aug 15, 2014 at 15:46

How about using a dual opto-isolator and putting the input diodes in inverse parallel. No additional parts compared to your top diagram other than a second pull-up resistor.

You could then deal with the two crossing signals separately with two MCU input pins (it would be possible to reduce it back to one but in this day and age, probably not a worthwhile exercise).

simulate this circuit – Schematic created using CircuitLab

• I can't understand how using a dual optoisolator would be better where better means the ouput spike will be slow enough even when the input is a square wave. Isn't he using a sort of dual opto isolator in antiparallel already though? At least that's what I'd call so... I'm lost. Aug 15, 2014 at 16:14
• Ok now I got it and that's smart. Have my upvote! Maybe a little diagram or something would help. Aug 15, 2014 at 16:15
• Pardon the incomplete drawing, but is this what you mean? imgur.com/5oxqpEd