Question: I have a 10W SMPS circuit powering a home appliance like this:

![vanellope_smps](https://i.stack.imgur.com/hNyXG.jpg)

I need to extract a zero-crossing pulse from this circuit which would then be fed to a micro-controller. To this end, I am considering the following modification to the circuit:  

![vanellope_zc](https://i.stack.imgur.com/8kQij.jpg)

Specifically, I have added an opto-coupler [PC817](http://www.sharpsme.com/download/pc817x-eJpdf) whose input LED would be driven from the rectified ~340V (0 to peak) through R16 (330k, 1/2W resistor). The maximum input current to the opto-coupler would thus be ~1mA. Considering the CTR to be 0.8, from the datasheet, the collector current would be ~0.8mA. Accordingly, I am considering a 33k resistor for R17. Have also added D10 ([1N4007](http://www.diodes.com/datasheets/ds28002.pdf)) to isolate it from the smoothing capacitor. 

1. Are the calculations correct? Will the circuit work?
2. Is it okay to drive the opto-coupler LED from ~340V? Would it damage the components or make the circuit less reliable?
3. How to estimate and minimize the pulse width at the zero-cross output so that it is closer to the zero-crossing?

Answer: 
The speed of the phototransistor optocoupler is possibly a problem, as Andy has already flagged. 

Also consider what threshold you are trying to achieve. If you want a zero-crossing pulse centered about the zero crossing and turning off at (say) +/-10V then you need to supply sufficient current to the LED to make that happen. Say that's 5mA. You now need to limit the current at the AC peaks to some reasonable value, and with a resistor it will be ~33 times higher, of course (\$\frac {230\sqrt{2}}{ 10}\$). 

To get the width (ignoring the speed of response of the circuit) we know that the voltage is about 325 sin(\$\omega\$t) where \$\omega\$ is the angular frequency of \$2\pi 50\$ radians/second. 

So the width of the pulse will be pw = (2/\$\omega\$)\$sin^{-1}\$(Vzc/325), 

or, in general 

pw = (\$1\over\pi f\$)\$ sin^{-1}(\frac{Vzc}{Vline \sqrt 2})\$

For example, if the voltage is +/-10V, then the pw will be about 200usec.
For small voltages it will be approximately proportional since the rate of rise of a sine wave is almost constant near the zero crossing. So, a +/-5V voltage will give you a 100usec pulse, ideally preceding the zero crossing by 50usec and following the zero crossing by 50usec. 

To make a precision zero crossing detector, you would be best to derive a power supply from the mains (maybe the TNY has some voltage you could borrow) and use a comparator of some kind (I've often used discretes because they're more immune to nasty stuff on the mains) to actuate a high speed logic-output coupler, so the LED in the opto sees only 5mA or 0mA.
The rise/fall time of the opto is about 20 microseconds (1000 ohm load) and the time when the rectified voltage is close to zero is maybe 2 degrees of one cycle. At a 20ms period of AC, a couple of degrees is about 100us. It's getting to close to not working is my impression.

Looking at the frequency response graph on page 8, with a collector resistor of 33k you'll be lucky to get 1kHz through this device so I reckon you won't see much of a signal. Even with a 100 ohm load the bandwidth is only going to be about 50kHz and will probably just about work. This would mean a rehash to your optical receiver circuit.

None of this takes into account the reverse recovery time of the bridge rectifier and 1N400x diode or any stray capacitance that might slug the shape of the full-wave rectified signal you hope to have.