# How to detect 220 VAC voltage using an opto-isolator

I created the following circuit.

I plugged it in and it works!!

Now I thought of adding an opto-isolator to the circuit in place of the LED. The value of C was the same.

$$X_c = {(({220volts-1.4volts})/0.01A))} = {(2*π*(50Hz)*C)}^{-1}$$

$$solve for :C=150nF$$

so I ended up with the circuit below.

The two diodes in series are actually present and on the first circuit to achieve a drop-down of 1.4 volts same as the external LED and for the internal LED.

The opto-isolator used was ln4n25

The diodes are proper voltage rating. Why a drop down of 0.7 volts

The problem is that gate of the transistor is not getting high when connected to mains. Meaning that the internal LED is off so GPIO is always HIGH.

Any thoughts? Is the internal LED even turning on? How can I check?

• The LED will not conduct for more than 50% of the time and it will conduct actually less time. So the GPIO's input is not a stable signal at best. Check the signal on GPIO input with an oscilloscope and check how much of the time it is low. Adding a capacitor on GPIO input will help keeping the signal low for as long as the 220V is live. The required value will depend on how long the GPIO input is driven low by Q1. If I need to suggest a value, I suggest 10uF. Jun 6, 2016 at 0:28
• If this is the same optocoupler you used in your previous version, you may well have killed it. Try driving the optocoupler LED with your 5 volt DC supply, and a 400 ohm resistor. Jun 6, 2016 at 0:32
• the optocoupler is dead, 10A (ballpark) surge current through the capacitor killed it when you turned the power on. Jun 6, 2016 at 6:24
• Although using the capacitor's reactance as a lossless resistance is a good idea, sometimes, it isn't a good fit for this application because of the transients generated by switching the mains ON and OFF. If you can afford the power loss (2.2 watts) and the heat, the simplest, least expensive way to do the job would be to use a 22k ohm resistor. Jun 6, 2016 at 10:57
• As for the surges on connection part-way through a Mains cycle I have seen this circuit with a series resistor in the opposite Mains lead to the capacitor. If this is used on a "plug-in" piece of equipment then you also want a high-value resistor across the capacitor terminals to bleed away the charge so that if you touch the wires that were connected to the Mains before you don't get the capacitor discharging through whatever touches the terminals...! =8-O Jan 4, 2017 at 20:44

You only need one 1N4148, and depending on the phase of the mains sine wave when it's initially connected to C1, C1 could easily pass enough current to wipe out the 1N4148 or the opto's LED. The reason is because C1's reactance will be too low to attenuate the switching transient's edge, so the transient will propagate into the circuitry downstream from the capacitor, as shown below.

I switched the mains ON at 90$^{\circ}$ in order to get worst case positive voltage out of C1, and the bottom three traces are the same as the top three except that I've zoomed into the left hand side to better show the transients.

This works perfect and is safe to use.

Note that when the 230 VAC is on, the transistor in the optocoupler is pulsing on the half of 230 VAC sinus, so use a buffer like C3 to smooth the signal on the 5V side.

Has the following issues:

1. 150 µF for C1 is way too high. At 50 Hz, that would draw 10 A! Your opto and your diodes are long gone.

This is particularly baffling since you had a nicely lit LED with 150 nF before. Why would you then make the capacitor 1000 times larger!?

2. At 150 µF, C1 would almost certainly be electrolytic. That means it is polarized, and can only be used with voltages of one polarity across it. Your circuit applies over ±300 V to the cap. Probably the only thing that saved the cap from blowing up and spewing flaming carcinogens into your face was that the diodes became fuses and died first.

3. Your D2,D3 multiple diodes in series don't make any sense. The forward drop of the combined diodes has nothing to do with the forward drop of the LED, since they occur at opposite polarities.

A single diode is all you need. That will clamp the reverse voltage across the LED to one diode drop. The forward voltage of the LED limits the reverse voltage across the external diode. Neither will see much voltage.

4. 1N4148 is a small signal diode. This is a case where a big, fact, knuckle dragging rectifier, like any 1N400x would do just fine. The slow response doesn't matter at line frequencies. However, the extra robustness to deal with power line spikes would be useful.

5. There is nothing protecting the LED from large current spikes due to voltage spikes on the power line. Note that the current thru the diodes is essentially the derivative of the power line voltage. Fast voltage spikes on the power line - and these thing definitely occur - will cause large current spikes thru the diodes.

A 1N400x in reverse across the LED might be able to ride out a spike, but the LED not so much.

1N4001 is only rated for 50V. Recommend at least 1N4003 or higher. Any particular reason for using a capacitor for power drop vs. more common approach using a resistor?

that gate of the transistor is not getting high

That base connection to the opto-transistor is an INPUT, not an OUTPUT. You should not expect to SEE anything on that pin. It is provided to change the behavior by adding bias to the device. The bottom line is whether the photo transistor is actually turning on and causing current to flow between emitter and collector.

You could try connecting your LTL-307EE in series with the LED in your 4N25 LED just as a visible "sanity check" to indicate when the opto-isolator LED is energized.

• forgive me wrong diode spec. its the proper one i just dont remember which. also the cap is chosen for power purposes more efficient. If the circuit works with an LED why not with an optoisolator? Jun 6, 2016 at 0:01
• If it works with the discrete LED, then it should work with the opto-isolator. However expecting to see ANYTHING on the base pin is not a valid test. Jun 6, 2016 at 0:06
• should the diodes be in series or parallel, in my case? does it matter? Jun 6, 2016 at 0:08
• @RichardCrowley - Your comment regarding the 1N4001 in the first circuit. In normal operating circumstances the max reverse voltage across the 1N4001 is equal to the forward voltage drop of the LED so a 50V rating on the diode is OK for nominal circumstances. Jun 6, 2016 at 0:23
• also is my initial assumption correct? is the opto-isolator's LED Vf=1.4 and If = 10mA ? or at least close to that? Jun 6, 2016 at 0:37

You assume phasor calculation(almost correct if Vf for both diodes are identical) for current and this 10 mA current means

I=0.01 sin(2*pi*50t) throwing through c1 and half of it (positive cycle ) into opto if we assume that opto is working at minimum of 5 mA which means from 30 degree to 150 degree (total 120 degree) the opto is on in just 1/3 of a complete cycle

this is some calculation you need is long run but this circuit is not proper for transient. you have to calculate initial current of c1 worst case when it is zero volt and line is in peak 320 volt

After looking around and trying out a few things i made this that maybe not perfect but works.

On a perforated board.

The Capacitor was simply lying around so i added it. Any other values for capacitance that would lead to a smaller (physically) sized capacitor.

Thank you for you inputs.

• I hope there is at least 6mm between the solder joints on the Live and Isolated sides of the opto-isolator - that is the absolute minimum needed for 250Vac (230 + ? %) in the CE (including UK) area... Jan 4, 2017 at 20:52
• Also note that the top of the capacitor should be considered live as it is connected to the mains supply. Jul 1, 2018 at 13:00