AC mains Power supply status

Thread Starter

sujith.g

Joined Jul 7, 2015
48
HI,

I am currently designing a circuit in which I need my microcontroller to detect if the AC mains power supply is ON or OFF. Initially I thought I could use a zero-crossing opto-coupler. But I will be using its interupt for dimming application of a lamp. So do I need to use another dedicated opto-coupler for detecting power supply ON-OFF status or can you please suggest an alternative solution. Please help.

Thanks in advance!!!
 

MikeML

Joined Oct 2, 2009
5,444
Why does the controller need to know that the AC is off?

Anyway, cant it be done with some code using the existing zero crossing detector, which generates two interrupts per cycle of the AC? Set up a timer that decrements at several tens to several hundred counts per 5ms (50Hz line) or 8.333ms (60Hz line). Reload the counter in the ISR. If the counter ever decrements to zero, then no zero crossing happened, ergo the line is off.
 
Last edited:

AnalogKid

Joined Aug 1, 2013
11,056
The output that drives the interrupt pin can drive multiple pins in parallel, so you can monitor another pin with a missing-pulse rouine, like setting an internal counter to twenty, restart it on each zero crossing, and have it generate a software interrupt when it downcounts to zero. Twenty zero crossings is 10 cycles of AC, so you don't get a false trip on a brownout. Or build it into the ISR as above.

ak
 

RichardO

Joined May 4, 2013
2,270
Be warned that the opto-isolator in your circuit will turn on and off very slowly by computer standards. The result is that the input circuit of the I/O pin may oscillate causing many, many interrupts for each zero crossing.
 

Thread Starter

sujith.g

Joined Jul 7, 2015
48
Why does the controller need to know that the AC is off?

Anyway, cant it be done with some code using the existing zero crossing detector, which generates two interrupts per cycle of the AC? Set up a timer that decrements at several tens to several hundred counts per 5ms (50Hz line) or 8.333ms (60Hz line). Reload the counter in the ISR. If the counter ever decrements to zero, then no zero crossing happened, ergo the line is off.
Hi Mike,

Thanks for your reply. Actually I need my MCU to keep track of Power cuts as part of the end application. If I add more tasks in interupt handler, it affects the dimming leading to flickering. So I need to keep the cpu usage low so that we sync at the right time with the interupt. Do I need to add external hardware?
 

Thread Starter

sujith.g

Joined Jul 7, 2015
48
Be warned that the opto-isolator in your circuit will turn on and off very slowly by computer standards. The result is that the input circuit of the I/O pin may oscillate causing many, many interrupts for each zero crossing.
The output that drives the interrupt pin can drive multiple pins in parallel, so you can monitor another pin with a missing-pulse rouine, like setting an internal counter to twenty, restart it on each zero crossing, and have it generate a software interrupt when it downcounts to zero. Twenty zero crossings is 10 cycles of AC, so you don't get a false trip on a brownout. Or build it into the ISR as above.

ak
Hi AK,
Thanks for your suggestion. I'll try it out.
 

AnalogKid

Joined Aug 1, 2013
11,056
Do I need to add external hardware?
The hardware effort / software latency tradeoff is up to you. If you want to go with a hardware missing-pulse detector, you already have part of it. You can connect a circuit to the output of the optocoupler in parallel with the interrupt pin, or add a 2nd optp by putting its input diode in series with the first. The circuit is an R-C timer that is continuously reset by the input AC, and times out when the AC is absent for a period of time set by you. If you don't need a fast edge to mark the transition from present to absent, the circuit is an opto coupler and pullup resistor (lime U3-R9) with a capacitor to ground. If you want a faster output edge, a dual comparator like an LM393 is very common.

ak
 
Last edited:
Top