Isolated mains zero crossing detector: how? - Page 1
Forum section: electronics / projects

author: technix
author_rank: Super Contributor
timestamp: November 02, 2018, 03:20:57 am
content: Since I got that big-ass TRIAC (and I also have a bunch smaller TO-220 and TO-92 ones) I am wondering how to build something with it. I have TRIAC-output optocoupler to drive the gate, now I need an isolated zero crossing detector to control the gate firing time.I wonder if this circuit would work, using a 6N137 optocoupler: (I just have a bunch of them in spare)* Put a 220 ohm resistor in series to the 6N137 transmitter diode,* Put a 5.1V 1W Zener antipatallel to the R-LED series* Put a high voltage high value resistor in series with the circuit above, I am looking at maybe 47k* Put the whole D-R assembly across the mains.Can this produce a square wave at the output of 6N137 with its logic edges corresponding to the zero crossing of the mains? If so I can feed this output to two timers on one microcontroller, with one timer measuring the mains frequency and the other generating the TRIAC firing pulses.

author: langwadt
author_rank: Super Contributor
timestamp: November 02, 2018, 03:36:10 am
content: could use two optos in anti parallel

author: technix
author_rank: Super Contributor
timestamp: November 02, 2018, 03:45:36 am
content: Quote from: langwadt on November 02, 2018, 03:36:10 amTechnically yes, but I am a bit troubled at how do I use the two outputs. 6N137 isn’t exactly a cheap component, and I don’t want to let one just go waste.

author: TJ232
author_rank: Frequent Contributor
timestamp: November 02, 2018, 05:56:06 am
content: Don't complicate your life, just use an AC opto likeSFH620Details about ZCD implementation, schematic & stuffHEREandHERE.

author: AllTheGearNoIdea
author_rank: Unknown
timestamp: November 02, 2018, 07:44:58 am
content: There are lots of opto coupled Diacs around with zeros crossing detectors

author: technix
author_rank: Super Contributor
timestamp: November 02, 2018, 07:46:16 am
content: Quote from: TJ232 on November 02, 2018, 05:56:06 amWhy is a DC opto + some R and D complicated? Also there would be no way for my MCU to find out the accurate phase for the mains.Quote from: AllTheGearNoIdea on November 02, 2018, 07:44:58 amWhat I need is to find out when mains crosses zero and tell that to a MCU.

author: AllTheGearNoIdea
author_rank: Unknown
timestamp: November 02, 2018, 09:25:18 am
content: You can detect the zero crossing easily using a large value resistor wired directly to the input of the MCU. This is done all the time using an external diode clamp or just the MCU esd diodes. Microchip pic have an application note on how to do this. Its still a bad idea you expose the MCU to potentially high voltages and and yourself . You could use a transformer but this going to be expensive. The safest way is the opto isolator or an opto isolator with built in zero crossing detector, or triac with zero crossing detector. Unless what you are doing is price sensitive to the penny there no reason to not do it the safe way.

author: langwadt
author_rank: Super Contributor
timestamp: November 02, 2018, 09:29:39 am
content: Quote from: technix on November 02, 2018, 03:45:36 amyou parallel the outputs too, that way you get pulse centered on every zero crossingedit: the 6N137 isn't open collector so you'll need two inputs or some way to OR the outputs

author: technix
author_rank: Super Contributor
timestamp: November 02, 2018, 10:18:18 am
content: Quote from: AllTheGearNoIdea on November 02, 2018, 09:25:18 amSafety: exactly why I am trying to implement a zero crossing detector using an optocoupler.Also all of you have mistook my use case: I am using two optocouplers in the HVFE: one non-zero-crossing opto-TRIAC as the precise-phase TRIAC firing optocoupler with TX at the MCU and RX at the HV side, and this zero-crossing and phase-detection 6N137 optocoupler with TX at HV and RX at the MCU.

author: Zero999
author_rank: Super Contributor
timestamp: November 02, 2018, 10:38:40 am
content: All that's needed is a standard opto-coupler and diode in reverse parallel with the emitter. The zero crossing point will be on the rising and falling edge of the opto-coupler's output.Another option is to power the MCU from an old fashioned transformer and linear regulator and monitor the voltage on the transformer's primary using a comparator.

author: TJ232
author_rank: Frequent Contributor
timestamp: November 02, 2018, 11:07:51 am
content: Quote from: Hero999 on November 02, 2018, 10:38:40 amThat's exactly how an AC opto looks, see also AC waveform crossing below, 2xFREQ for an AC OPTO:

author: TJ232
author_rank: Frequent Contributor
timestamp: November 02, 2018, 11:21:12 am
content: Quote from: technix on November 02, 2018, 07:46:16 amFrom a lot of reasons, from BOM to the fact that maybe you want to detect ALL the zero crossing of the AC wave.If you look at the links above you will find 2 solutions for your request.Are you using it to drive a triac for light or heating dimming ?

author: technix
author_rank: Super Contributor
timestamp: November 02, 2018, 11:45:43 am
content: Quote from: TJ232 on November 02, 2018, 11:07:51 amI can NOT tell if the mains is going positive or going negative for the specific pulse with an AC optocoupler. Given the TRIAC type, this information can be important.Quote from: TJ232 on November 02, 2018, 11:21:12 amFor the BOM it does not differ much. And I am lot losing out on edges even if I am using a traditional DC type one: a positive going edge is 0 degrees phase, and a negative going edge is 180 degrees. Now feed that into an MCU timer and I can get a pretty good idea where the mains phase is at for any given moment.Quote from: TJ232 on November 02, 2018, 11:21:12 amNot determined yet, likely lighting first.

author: TJ232
author_rank: Frequent Contributor
timestamp: November 02, 2018, 12:21:19 pm
content: Quote from: technix on November 02, 2018, 11:45:43 amWhat TRIAC are we talking about?For Heating you want to look at a PSM Mode implementation, not Phase-cut dimming. Because of AC lines load, EMI & stuff.

author: Zero999
author_rank: Super Contributor
timestamp: November 02, 2018, 01:06:08 pm
content: Quote from: TJ232 on November 02, 2018, 11:07:51 amNo it isn't. My description relates to the DC opto-coupler, on the right of that schematic, which can easily be used to detect AC zero crossing, just by looking at the rising/falling edge of the output pulse.Quote from: technix on November 02, 2018, 11:45:43 amThen use an ordinary DC opto-coupler with an anti-parallel diode, as I mentioned above. The positive and negative transitions of the mains can be determined by looking at the rising and falling edge of the output from the opto-coupler.The only issue with using it to trigger a TRIAC is the negative edge of the pulse will occur when the mains voltage is still slightly positive, but that can be easily overcome by delaying the firing for a bit to ensure the TRIAC is triggered when the AC has reversed.

author: technix
author_rank: Super Contributor
timestamp: November 02, 2018, 01:36:34 pm
content: Quote from: Hero999 on November 02, 2018, 01:06:08 pmThe basic design of that assembly is still DC-type optocoupler with an antiparallel diode. The whole Zener business is for the purpose of building with 100V mains in mind while still tolerating 250V mains.Quote from: Hero999 on November 02, 2018, 01:06:08 pmI actually don't really need to detect the falling edge of the mains. With just the rising edge, I can run what is effectively a software PLL with a timer in the MCU. After the timer-mains PLL locked the subsequent TRIAC firing can be done using that internal timer as the base.

author: langwadt
author_rank: Super Contributor
timestamp: November 02, 2018, 02:07:27 pm
content: Quote from: technix on November 02, 2018, 01:36:34 pmwith a single DC opto you get an edge alternating between being before and after the zero crossing, with and AC opto or two DC optos you get an edge before and after each zero crossing so the timing is always the same

author: technix
author_rank: Super Contributor
timestamp: November 02, 2018, 02:15:10 pm
content: Quote from: langwadt on November 02, 2018, 02:07:27 pmAC optocoupler and simply paralleled DC optocouplers will result in a loss of half of the phase information. Using a single DC optocoupler and trigger on only one edge I can create a software PLL using the optocoupler as phase reference input and internal timer as the digitally-controlled oscillator and divider. Once that PLL locked the subsequent timing control can all be implemented using the internal timer as a time base.

author: spec
author_rank: Frequent Contributor
timestamp: November 02, 2018, 02:54:51 pm
content: Quote from: technix on November 02, 2018, 03:20:57 am+ technixIs this what you still want?Using one 6N137, would you be happy with an isolated square wave, at 5V logic levels, representing the mains sine wave. The edges of the logic level square wave would equate to about 2V of the mains sine wave rising and the same voltage of the mains sine wave falling. Because the zero crossing point is at the maximum dv/dt of the sine wave there is not a load of difference in time between the two.

author: hussamaldean
author_rank: Unknown
timestamp: November 02, 2018, 03:34:26 pm
content: I built the circuit in the attached file and it does workhere is the arduino code#define zero 2#define triac 3#define pot A0int bright;void setup(){pinMode(triac, OUTPUT);pinMode(pot, INPUT);pinMode(zero,INPUT_PULLUP);attachInterrupt(digitalPinToInterrupt(2), angle, RISING);}void loop(){bright=map(analogRead(A0),0,1023,0,10000);}void angle(){digitalWrite(triac, LOW);delayMicroseconds(bright);digitalWrite(triac, HIGH);delay(1);}

author: technix
author_rank: Super Contributor
timestamp: November 02, 2018, 07:48:13 pm
content: Quote from: spec on November 02, 2018, 02:54:51 pmYes it is still what I want. If the application can tolerate the slight imbalanced duty cycle of the square wave it would trigger on both rising and falling edge of the square wave and have a constant delay; if not I can trigger on only the rising edge and implement a software PLL using the MCU timers for finer timing control.

author: spec
author_rank: Frequent Contributor
timestamp: November 02, 2018, 08:01:33 pm
content: GoodI have done a circuit: just got to put it in Eagle and will post for you to have a look at.

author: spec
author_rank: Frequent Contributor
timestamp: November 03, 2018, 11:15:51 am
content: Schematic attached.Hope it worksThe circuit switches at about 1V depending on the gate threshold voltage of the MOSFET.If you want a precise zero crossing detector, within a few millivolts, just say and I will post another circuit- basically just replace the MOSFET with an open drain comparator.