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.
Andy aka is correct, but if you still want to give it a try I'd suggest the following modification

![schematic](https://i.stack.imgur.com/3nRcg.png)

[simulate this circuit](/plugins/schematics?image=http%3a%2f%2fi.stack.imgur.com%2f3nRcg.png) – Schematic created using [CircuitLab](https://www.circuitlab.com/)

ETA - Please ignore this paragraph - The reverse diode corrects the immediate flaw in your design - reverse voltage on the LED. The opto data sheet, oddly, has no spec on maximum reverse voltage, but I'll guarantee you that your LED would not survive a single cycle of the mains voltage.

By putting the optocoupler on the input of the bridge, you get a more positive zero indication than when you look at the output. It's not a panacea, though, since optos are famous for having different turn-on and turn-off times. To get around this, you can try

![schematic](https://i.stack.imgur.com/sk0QX.png)

[simulate this circuit](/plugins/schematics?image=http%3a%2f%2fi.stack.imgur.com%2fsk0QX.png)

with two optos back to back and the outputs wire-or'ed. Even this is not guaranteed, though, since if the turn-off time is too much greater than the turn-on time, you'll get no output pulses at all.

Actually, your idea has merit, but if you want to try it, you need to look at faster optocouplers. They do exist.