# Zero-crossing detection

I'm doing a circuit that controls a charge for TRIAC, with zero detection. Whenever I tested the circuit, regardless of what I was sending from the microcontroller, TRIAC always sent all the current to the load.

When I looked at an oscilloscope for the detection of zero, I realized that it only detected zero once in a complete wave (not two). I researched the component I was using and noticed that the 4N35 only has one LED, so since I was not rectifying my wave before, it was only detecting the positive part of the wave.

I can not find any integrated circuit that has two LEDs in my city, so I decided to change the software. This is the function I have been using when it picks up the interrupt:

 int dimtime = (65*dimming);    // For 60Hz => 65
usleep(dimtime);    // Wait for start TRIAC
digitalWrite(AC_LOAD1, HIGH);   // Start TRIAC
usleep(8.33);       // Delay TRIAC
digitalWrite(AC_LOAD1, LOW);    // Turn off


This 65 came from: 1 full 60Hz wave = 1/60 = 16.6ms It will reach the zero point in: (60Hz) -> 8.3ms (1/2 cycle) 8,3ms = 8300us (8300us - 8.33us) / 128 = 65 (Approx) (and 128 was the amount of steps I split)

In my country, i have 220V and 60Hz for AC.

My question is: how can I change my code to control TRIAC by detecting zero only on the positive side of the wave? It is possible?

With this code by now, when i send 128, it simply lets the wave pass completely, and when I say 2, for example, the output wave goes out all "wrong", without following the AC wave.

• if mcu supports both rising and falling edge detection, no additional mods are necessary. at rise would detect first half-phase, at falling, the 2nd one. May 20 '18 at 22:41
• if both edge detection is not possible, start a timer at first zcd, while overflowing at 8.3ms the 2nd half-phase software zcd will occur May 20 '18 at 23:29

There is no need for tricky software.

simulate this circuit – Schematic created using CircuitLab

Figure 1. Replacing D2 with a bridge rectifier will turn on the opto-isolator on both positive and negative mains half-cycles.

Your 59k value for the current limiting resistor, R5, seems suspiciously low. You should check your calculations for this and the power rating. Most carbon or metal-film resistors are only rated for 200 V or so. Your 220 V mains will peak at $220 \sqrt 2 \ \text V$ so it is usual to use two series resistors to make up the required value.

simulate this circuit

Figure 2. A second 4N35 could be piggy-backed onto the existing one. Note that the LED terminals require a cross-over.

• Yes, this is totally the best solution.. But believe it or not, this circuit is already on PCB (I dont know how, it worked for this first time in protoboard, but when i passed to pcb, didnt work anymore) So thats why im trying the "easiest" way possible to fix this. May 20 '18 at 19:48
• See the update. May 20 '18 at 19:56
• This is a great ideia! Thank you! I'll try there software one first, but if didnt work, this is the best one. Thank you very much! May 20 '18 at 20:21

Triacs cannot be turned off through the gate. You can turn off the gate current but the Triac will only turn off itself as soon the holding current through the MT1-MT2 terminals passes the (current-)zero-crossing.

What you have to do in your interrupt routine is:

--- We are now at the zero-crossing from negative to positive

1. Wait for the angle time
2. Turn on the gate current
3. Wait 100µs so the Triac could react to the gate current.
4. Turn off the gate current
5. Wait for the 8.33ms-angle time-100µs

--- We are now at the zero-crossing from positive to negative

1. Wait for the angle time
2. Turn on the gate current
3. Wait 100µs so the Triac could react to the gate current.
4. Turn off the gate current
5. End interrupt.
• @Janka There are Gate turnoff Triacs, they have existed for a long time. May 20 '18 at 20:22
• Gate-turn-off Triacs? Which ones? Gate-turn-off thyristors, sure. May 20 '18 at 20:29

Firstly there is nothing conceptually wrong with the circuit you have designed even though it only signals during the positive half cycle (there are however lots of practical problems with it).

The leading edge of your MCU signal is close to the zero crossing (x volts after it) on the positive half cycle and the trailing edge is close to the zero crossing (x volts before it) for the negative half cycle.

You could set up an interrupt to allow you to respond to either the 0-1 transition or the 1-0 transition into your MCU.

The biggest problem with your design is the very low operating currents you have chosen to set, which makes the design less than ideal.

Consider that for your design, Q1 needs to sink 500 uA (plus some unknown current to pull the MCU signal low, which I'll ignore), and that will require about 5 uA base current at the minimum Hfe of 100. The C-E current for the 4N35 is therefore around 6 uA. At this low current the CTR of the 4N35 is very low and likely only of the order of 0.1, so about 60uA of LED current required. This will mean the AC input voltage will be at about 4.5 V. This might be an acceptable voltage level for detection, but it will be very temperature sensitive and corrupted by the time it takes to charge C4 at these insanely low currents.

I'd challenge you to go through your design again and try to predict the trigger voltage required on the AC line over a temperature range of just 0-30 degC and with a range of BC547 and 4N35 characteristics. It won't be a nice picture.

I prefer a schematic like this based on a depletion mode FET to detect zero crossings reliably:

simulate this circuit – Schematic created using CircuitLab

With this type of detection you get the same trigger level for both positive and negative half cycles, and the trigger is always just after the zero crossing. You then only need to detect a single edge if you use interrupt detection.