I am using this circuit for mains detection:

Mains detection

The output fluctuates a little, but capacitor and resistor values ensure that the output is logic HIGH when 220 V mains is present. Once mains is not available, I get a logic LOW.

This circuit has a quick rise and slow fall response. This allows quick detection when mains is connected. However, detection of mains removal takes around 100-150 ms due to discharging capacitor.

I want opposite results. I want to know quickly when there is a power failure. Here is an image showing current output and desired output curves:


Even if my logic gets inverted (HIGH when 220 V is absent and LOW when 220 V is present), I have no issues. I can program accordingly.

Decreasing discharge resistor will not work as it will discharge the capacitor too fast and I will get a pulsating output when mains is present.

  • \$\begingroup\$ use a monostable with a pulse input from your opto to keep the monostable triggered. \$\endgroup\$
    – Icy
    Commented Dec 11, 2015 at 9:33
  • \$\begingroup\$ I was looking for some simple solution like altering the position of R and C and maybe adding a couple more resistors to obtain the desired output. \$\endgroup\$ Commented Dec 11, 2015 at 9:48
  • \$\begingroup\$ you can use a resistor in series with the capacitor, and then you can use a lower value of capacitance. tc = 1/(2 * pi * R * C) \$\endgroup\$
    – Icy
    Commented Dec 11, 2015 at 9:51
  • \$\begingroup\$ First thing to do is to eliminate the capacitor. \$\endgroup\$ Commented Dec 11, 2015 at 9:51
  • \$\begingroup\$ @icy - Can you please explain with a circuit diagram? I was not able to understand where to take the output from. Thanks \$\endgroup\$ Commented Dec 11, 2015 at 10:11

4 Answers 4


Before you do anything, you have to fix the circuit driving the LED. At the peak of the 220 VAC sine, there is 311 V on the left end of R29. Figure about 2 V for the LED, so 1.6 mA thru the resistor and LED. That's good enough for many opto couplers, although I haven't looked up this one in particular.

However, the problem is during the negative half-cycle. The LED will be reverse biased way way beyond it's spec. Again, I haven't looked up your opto, but most likely the maximum LED reverse voltage is around 5 V. If you haven't broken the opto yet, you will shortly. One way to fix this is to put a ordinary diode in reverse across the LED, although that will double the dissipation in R29. At roughly 220 Vrms across R29, it will dissipate 1/4 Watt. It really should be a "1/2 Watt" or more resistor, or implemented with two 1/4 W resistors in series.

Now to the problem you asked about. The solution is to loose the filter. I'd also use a stiffer pulldown or pullup resistor. Many microcontrollers have internal pullups as options on at least some of their pins. The simplest is then to connect the opto output between a pin with internal pullup and ground. No other parts are needed.

The digital input will now be pulsed at the line frequency. This is easy to deal with in firmware, and I have done that a number of times. With the raw input showing you when the positive half of the power cycle is occurring, the rest is up to policy in the firmware.

One of many ways to do what you want is to create a internal signal that tells you as quickly as possible whether power is present. Whenever the opto output is activated, you set a timer to a little longer than the normal off time of the opto. During the on time, the timer will be effectively held at this value, and it will count down during the off time. AC input is present whenever the timer is not 0.

From the AC-present signal, you can easily make a delayed start signal via low pass filtering, a deliberate timer, etc.

  • \$\begingroup\$ Thanks Olin. I had totally overlooked the LED circuit. However it got me wondering why haven't I seen a failure yet. I made a few test board which have been running for a couple of weeks without any trouble. According to datasheet, it can withstand a reverse voltage of 6V. Can you please explain how is it could possibly survive so long? \$\endgroup\$ Commented Dec 12, 2015 at 7:22
  • \$\begingroup\$ Also, will it help if I replace my optocoupler with dual channel optocoupler like K814P. It has two LEDs inside connected in reverse parallel. I think overall two LEDs will be equivalent to what you suggested. \$\endgroup\$ Commented Dec 12, 2015 at 7:35
  • \$\begingroup\$ @Whis: Yes, a "AC" opto-coupler is a better idea if you want to know as quickly as possible when power goes away. Keep in mind that even that will have some dead time around the zero crossing. \$\endgroup\$ Commented Dec 12, 2015 at 12:31

power outage has to be quicker. Power restore can take a few milliseconds.

Here's the first clue about how fast the system has to respond i.e. it can take a few milliseconds for power to be detected but, reading between the lines it has to be 1 millisecond or less to detect power has failed.

Unfortunately this cannot be achieved.

The cyclic nature of the AC power waveform is that it takes 5 milliseconds (at 50Hz) to shift from a peak voltage to zero voltage. Here's a picture of 50 Hz and 60 Hz waveforms: -

enter image description here

This means that to avoid the detector circuit indicating a "power fail" between cycles, you have to have a delay of at least 5 milliseconds.

My answer is "rethink your requirements".

  • \$\begingroup\$ You can reduce the "power fail" detect time by using a bridge rectifier before the optocoupler's LED - that way you will only have to wait half a cycle, rather than a full cycle, to determine that the power input has failed. \$\endgroup\$ Commented Dec 11, 2015 at 17:37
  • \$\begingroup\$ Thanks Andy. I think I can go with Olin's idea of doing it on the program side. It's clear that I can't do any better than 5ms. Using a filter will only introduce more delay. I had one more confusion. Olin pointed out that my LED side is not proper and during reverse peak, LED side can get damaged. However I have a few test boards running for around a couple weeks without any issues. Can you please explain how is it possible that LEDs haven't burned yet. Max reverse voltage of my opto is 6V. Thanks @PeterBennett. Alternatively I can also use dual channel opto coupler like K814P. \$\endgroup\$ Commented Dec 12, 2015 at 7:31

When I have done AC detection I go for the pulse solution. The stream of pulses can be managed by an MCU interrupt and/or an MCU timer. The timer solution on some microcontrollers can be setup to timeout at say 1.2 AC cycle times and be re-triggered at the occurrence of each detected pulse. This allows for detection that could be adjusted by the software to be trimmed as close to the earliest detect time as possible.

For example, if your device were expected to work at 50 or 60Hz you could first measure the pulse period to determine the frequency and then set the timer period accordingly.

Note that setting up the MCU to respond to the AC pulse detect interrupts at 50 or 60Hz places very low processing overhead on your device (provided you do not do stupid things in the service routine).


Change To:


simulate this circuit – Schematic created using CircuitLab

You will have to experiment a bit to get the right component values, but the following formula should help:

On Tc = 1 / (2 * pi * R2 * C1)

Off Tc = 1 / (2 * pi * (R1 + R2) * C1)



simulate this circuit

In this circuit, on Tc will be = 1 / (2 * pi * (R3 + R4) * C1)

and off Tc will be = 1 / (2 * pi * R4 * C1)

BUT you may well need an additional transistor at the output stage to raise the voltage level because of the effect of R3 and R4 acting as a voltage divider. VOut = 3.3 * R4 / (R4 + R3)

The BEST solution would be to use the pulse input as an input to a monostable timing element, but you discounted this :-)

  • \$\begingroup\$ I think this is opposite to what I require. R2 < (R1+R2). So charging will be quick. So, I will get a sharp HIGH when 220V is turned on. However discharge will occur through R1+R2 and thus LOW will not be sharp during 220V failure. \$\endgroup\$ Commented Dec 11, 2015 at 11:02
  • \$\begingroup\$ With this you cant make on time less than off, but you can get off to be much faster than your current design. The best way to improve it is with a monostable. though I suppose you could get away with another transistor to invert the output. \$\endgroup\$
    – Icy
    Commented Dec 11, 2015 at 11:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.