# Sensing AC high voltage to microcontroller

I'd like to sense if I have AC 220V 50HZ available, and convert it to some logical level.

I made such circuit, will it work?

I use C1 as a resistor.

Any recommendations what can be improved?

# UPDATE:

Thanks for the great help.

I choose Russel's answer because he was first and the note about 1N4001 was very useful, calculations show that D1 will be stressed to up to 70 volts while C1 charging, so Turpie head will be kept in place. I will hold 4001 for lower voltage applications.

Olin's answer was like the same C: Olin 0.1 uF, Rusl 0.068 (or 0.05). Rseries Olin 1k. Russell 10k, but also helped me alot.

Here is the updated circuit with simulation.

As keen beginner sorry for noobish questions. I will add more details with datasheets next time.

• how much current does the led need? Jan 23, 2015 at 13:03
• Imagine you plug it in at the positive peak of the 220V mains- the momentary peak current will be 6.1A, of which about half will go through the TLP621. How does that compare to the rating? Jan 23, 2015 at 13:10
• Looks good, but 1) C1 is much too big, try 100 nF and work out the current. 2) consider the plug-in transient, R2 should be bigger and stronger. 3) no real need for R1. Jan 23, 2015 at 13:28
• If you're going to sell the circuit, it will need to pass all sorts of overvoltage and surge tests. Perhaps a zener in parallel with the opto isolator would allow it to withstand the surge. I don't know the values, do some homework, but it might be double voltage for a few hundred ms. Jan 23, 2015 at 13:31
• That cap will shift the zero cross, are you more interested in presence or do you also need a relevatively accurate zero-crossing
– user16222
Jan 23, 2015 at 13:45

Note: Below, where you see "=" read "~=" or "~~~="as appropriate.

You are providing FAR more current than you need and it will cause you problems.

Spehro notes the peak current that you might see.

Note that the optocoupler doe NOT NEED up to 50 mA -it can ACCEPT UP TO 50 mA continuous. If you can run it at less or much less than that it will be more pleased with you.

Impedance of the capacitor is 1/(2 xpi x f x c) At 50 Hz that's ~~ 4500 Ohms.
Current will vary ~= sinusoidally.
Peak 220 VAC voltage is 220 x sqrt(2) ~= 310V.
I = V/Xc = 310/4500 ~= 70 mA.

Specs at end.
Ifmax = 50 mA so even in ordinary use you have it at the top end of it's range. Or above. BUT your 2 x 100R will reduce it to less than 50% through the LEDs BUT makes you need a 2x+ sized cap.

As this is just (you said) for AC mains presence sensing then you can accept something that gives a pulse for part of each half cycle.

They say CTR min = 50% at I_LED = 5 mA. So a 10 k output resistor will give up to V = IR = 5 mA x 50% x 10K = 25 V swing. ie it will be easy to swing rail-rail on a lower voltage supply with 5 mA input.

So you could tolerate even less than 5mA mean current, so lets start with ~~~= 5mA at peak Vin of about 310V. So Cnew = Cold x Inew/I old
= 0.68 uF x 5/70 = 0.05 uF,
Xc 0.05 uF ~= 60,000 Ohms.

AT 300V continuous DC (which we have not got) power in a resistive load of 60K would be V^2/R = 300^2 / 60k = 1.5 Watt
So if you used a 10k resistor in series with C1 it would dissipate 10k/60k x 1.5 = 0.25W.
That is peak dissipation at 300V so mean will be less.
So a series 10k x 1/2 W resistor will probably survive, a 1W is better and more may be wise.

Now, remove R1, R2 is now set to 10K as above, C = say 0.068 uF.
C1 MUST be an X or Y rated mains cap. MUST. R2 (which you can rename R1) would very very ideally be peak mains rated too. (Resistors can fail under applied voltage even if dissipation is far below the rated value).

Try 10K in series with optocoupler output at whatever your secret-so-far Vdd_out is. You should get a series of close to square pulses on half of every mains cycle. Width of pulse will vary with opto CTR which varies by a factor of 12 (50% to 600%).

NOW: Check my figures, remove some of the broad assumptions I have made and/or simulate the circuit.

1N4001 - no no

Take your 1N4001 collection and give it to a keen beginner. Point out their shortcomings.

Find somebody selling 1N4007s at about the same prices as 1N4004s (or less) and make these your standard part for <= 1A 50 Hz work.

1N4001, which you may not really be using, are 50V rated if I recall correctly.
There are too many times when this will be too low and too many times when you see 1N40.... around the body of a rectifier and assume it is a real one OR see 1N4001 and assume it reads 1N4007. Any of these mistakes can cost you so much on the few occasions they happen that changing now is already too late.

Even in this application, if you hit a mains peak when you plug this in and D1 is reverse biased it may give D1 'a bit of exercise' (and may pull down its hemp stalks*) if it was a 1N4001 - you'd have to do the calculations. If it was a 1N4007 it would toss ever so slightly in its sleep and mutter about doing something about little dog Turpie* in the morning.

Get rid of the 1N4001's (if they exist)

Crummy spec from crummy spec sheet:

Low quality spec list here

Current - DC Forward (If) (Max):50mA
Current Transfer Ratio (Max):600% @ 5mA
Current Transfer Ratio (Min):50% @ 5mA
Input Type:DC

Operating Temperature:-55°C ~ 100°C
Output Type:Transistor
Rise / Fall Time (Typ):2µs, 3µs
Turn On / Turn Off Time (Typ):3µs, 3µs
Vce Saturation (Max):400mV
Voltage - Forward (Vf) (Typ):1.15V
Voltage - Isolation:5300Vrms
Voltage - Output (Max):55V

You sortof have the right idea, but the implementation needs work.

The LED only needs a tiny voltage to operate compared to the power line input. But, it still needs some current, like let's say 10 mA at the peak of the power cycle (I just made that up, it's your job to pick a good value after consulting the datasheet). So, you need to drop 310 V at 10 mA. The resistance would be (310 V)/(10 mA) = 31 kΩ. That would work fine, but would also dissipate (218)²/(31 kΩ) = 1.5 W. If you don't care about the power and dealing with the heat, then a 31 kΩ 2 W resistor would do it. You still need the reverse diode across the LED to keep if from getting damaged during the negative half-cycle.

Using capacitors as the impedance to drop the voltage works without dissipating power. Capacitors will add a phase shift, but if you just want to know whether the line power is present of not, you don't care about that. 100 nF gives you about the same impedance to get 10 mA peak thru the LED. However, capacitors will also pass short term spikes, so I'd still put some resistance in series with the LED. The peak current the LED can actually take should be 2-3 times higher than what you plan to give it, so we can tolerate a few short term spikes.

I'd want at least 1 kΩ in series with the LED. That will drop a additional 10 V at the 10 mA peak current and dissipate 100 mW. With the extra voltage drop, we won't actually get 10 mA. However, that was somewhat arbitrarily chosen to be 2-3 times less than what the LED is rated for, but still several times more than what is needed to reasonably turn on the output transistors. Most optos have such wide latitude. Since your frequency is so low, you can afford a rather large pullup resistor on the opto output, so should actually need very little LED current.

So, starting with your circuit, loose R1, change C1 to 100 nF, and change R2 to 1 kΩ. Remember that these values are based on example LED current I made up. Read the opto datasheet carefully to see what a good peak LED current is.

• We arrive, as you'd expect at well inside an order of magnitude difference in components overall with the same general reasoning and both making the same provisos re assumptions. Sometimes we both seem to manage quite well to sing out if the same hymnal. Other times ... :-). | C: Olin 0.1 uF, Rusl 0.068 (or 0.05). Rseries Olin 1k. Russell 10k - comments account for differences). ... Jan 23, 2015 at 13:56

If you would allow me, the solution is actually much simpler. For a typical 3.3v micro non-5v tolerant I/O pins there are internal high and low side protection diodes. In the electrical section you should find a spec call either I/O clamping current or more commonly injection current. There it will tell you maximum I/O pin injection/clamp current allowed when the input voltage exceeds the micros VDD or VSS. If you simply put a 500k-1meg resistor in series with the Micro's input pin to the AC line you can easily monitor the AC line voltage for duty cycle and frequency using SW or better a common Micro controller peripheral that can measure time duration like a Microchip peripheral called TC in the ATSAM products or the INPUT CAPTURE in PIC32MX family of products.

• Important Caveat: This means that your circuit is no longer isolated from the mains, which means that the whole thing is subject to additional safety requirements (double insulation, etc.). Jun 29, 2019 at 14:54

You updated circuit is BAD. If that circuit is plugged into a mains circuit when the voltage is at a peak, the instantaneous current will be primarily limited by wiring resistance and you will be highly likely to thermally fail the opto LED. Here is a better suggestion

simulate this circuit – Schematic created using CircuitLab

In this case, the D1 acts as a proper transient limiting device and the 100R as current limiter for microsecond length transients. The Opto diode is then safely fed from the limited voltage of D1 via the 470R current limit resistor.