Sensing AC high voltage to microcontroller

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)


  • Little Dog Turpie & the Hobyahs & the 1N4001 & ....

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.