I want detect a 220 VAC AC signal using an AVR.
I could convert 220 VAC to 5 VDC with an opto-coupler.
How can I detect the exact time of the signal?
What about blinking signal per half second?
-
1\$\begingroup\$ Could you please explain what you mean by "real time", as you have mentioned in some comments? Also by "exact time of signal", do you mean the exact frequency of the 220 Volt AC? \$\endgroup\$– Anindo GhoshCommented Jan 11, 2014 at 11:18
-
\$\begingroup\$ The meaning of the terms "the exact time of the signal" and "blinking signal per half second" are not clear. Would you please explain these terms in more detail. \$\endgroup\$– Russell McMahon ♦Commented Jan 11, 2014 at 11:32
-
\$\begingroup\$ Realtime is ? within 1 minute, 1 second, 1 milli second, 1 micro second or other... delete those that do not apply.... \$\endgroup\$– SpoonCommented Jan 11, 2014 at 12:49
-
1\$\begingroup\$ What is the negative? You want to detect 220VAC versus nothing? Versus 220 DC? Versus 120 AC? Versus 10kHz 1V signal? In other words what are the inputs you are trying to discriminate? \$\endgroup\$– angelatlargeCommented Jan 11, 2014 at 22:08
3 Answers
Assuming I get the requirement correctly you can use the following:
An optocoupler that outputs pulses to a digital pin for each zero cross of the mains voltage, you'll get 100 pulses for a 50Hz input frequency.

If you want to get the pulses only when the mains is above a specified level (for under-voltage detection) then you can use a zener in the opto diode side to introduce a voltage drop.
In the following example the zener used is 1N5281 which is a 200v diode, so in order turn on the optodiode the mains voltage need to raise above Vzener + Vfdiode + Vfoptodiode which results to about 203v.

Both these circuit offer the benefit of mains isolation.
The schematics have been update, the resistor values should be calculated per case. In both graphs the green trace is the mains input (left axis) and the red trace the output (right axis).
As jippie noted, care must be taken regarding the power dissipated on R1 and possibly replace it with two resistors in needed in order to operate them within voltage specs.
As Anindo Ghosh noted, the resistor value R1 needs to be selected based on the current transfer ratio of the used optocoupler and the output current requirement.
As JoeHass noted, in one of my previous circuits the diode was connected anti-parallel to the opto-diode which resulted in increased dissipation on R1. The diode has been moved in series with the opto-diode so that the current through R1 flows only for half cycle (in one polarity).
-
2\$\begingroup\$ @user31339 And why isn't an optocoupler real time ? I'm not sure I get what you mean. \$\endgroup\$– alexan_eCommented Jan 11, 2014 at 9:56
-
2\$\begingroup\$ Notice that the 22k resistor in your circuit is dissipating 2.5W and many resistors are not rated for 230 V(AC) or more. \$\endgroup\$– jippieCommented Jan 11, 2014 at 10:52
-
1\$\begingroup\$ @jippie Actually the 22K resistor will drive the opto-led with about 10mA which is high for the output requirement (of driving a digital input). A 100K - 220K resistor would provide more that enough current (1-2mA) with a low power dissipation (0.5W or less). \$\endgroup\$– alexan_eCommented Jan 11, 2014 at 11:42
-
1\$\begingroup\$ I like the anti-parallel diode anyway, LEDs are not very good with reverse bias. I realize that is the reason for the series diode, but I like the anti-parallel (or both) better to relieve the stress from the LED. Also I would probably replace the resistor with a (X-rated) capacitor, in series with a small (100 - 1kohm) resistor. \$\endgroup\$– jippieCommented Jan 11, 2014 at 21:42
-
2\$\begingroup\$ I expect the series diode and LED will act as a more or less evenly distributed voltage divider. If both are equivalent to about 100Mohm, then there will still be an unacceptable voltage build up across the LED. \$\endgroup\$– jippieCommented Jan 11, 2014 at 22:14
\$C = \dfrac{I}{2\pi f U} = \dfrac{10\text{mA}}{2\pi \cdot 50 \cdot 230} = 138 \text{nF} \Rightarrow 100\text{nF}\$

simulate this circuit – Schematic created using CircuitLab
- Assumed 50Hz mains frequecy.
- Chose 10mA to safely compensate for VRMS / VPK = √2, and under assumption of 20mA maximum current for the LED in the optocoupler.
- R1/R2 to discharge the capacitor when the device gets unplugged. Two resistors because most low power resistors are rated for 200V(DC) max.
- D1 to ensure C1 can (dis)charge every half cycle and to protect LED D2 against being reverse biased.
- R3 to protect against inrush current and low power resistor will blow when C1 fails short.
-
\$\begingroup\$ Note that the capacitor introduces a phase shift by \$ 90^o \$ which results to output pulse transitions at the (positive/negative) peaks of the input sine wave. \$\endgroup\$– alexan_eCommented Jan 12, 2014 at 1:29
-
\$\begingroup\$ I personally prefer to tap the AC voltage from a transformer rather than directly from mains. \$\endgroup\$– jippieCommented Jan 12, 2014 at 9:03
-
\$\begingroup\$ Yes that is a good idea but the transformer is not always accessible, for example when a sealed wall wart is used or a battery supply or USB supply etc. \$\endgroup\$– alexan_eCommented Jan 12, 2014 at 11:09
Atmel application note AVR182 gives an implementation of a zero crossing detector. Note very carefully the following on page 2:
It should be noted that this solution will not give any galvanic isolation for the microcontroller against the AC mains.
This means that you must take appropriate precautions before attempting to use the solution.