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 &lt;= 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 &amp; the Hobyahs &amp; the 1N4001 &amp; .... 



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