I need to detect the presence of mains on a set of wire through my Arduino.
I know that the common suggestion for this scenario is to use a 5V power supply, but I would like to avoid that since I need to monitor quite a few inputs (4) and there are space constraints.
So I used the circuit I found at PClaboratorio - led 220V. The page is in Italian, but the idea is to use the reactance of a 150 nF capacitor to bring mains level to safe levels for a led. I tested the circuit and it worked, so I replaced the led with a PC817 optocoupler and TA-DA! I could detect mains!
But I have an issue, where the opto basically dies after a few tests. It seems to die straight at the moment I turn the mains on. If it survives the power-on phase, then it works for long periods without issues. On that page, the guy has done calculations and measurements and, according to its datasheet, everything should be in the opto's safe zone, as far as I can understand.
Maybe I need to add some sort of protection just like with relays switching mains? I have some MOVs and bidirectional TVS diodes handy (both rated for mains), can they help?
That's because if you turn it on at the top of the sin you have (at least on paper) infinite current flowing. That's why a lot of these circuits have a series resistor as well. Try adding a 220Ohm series resistor as well.
And to make it saver, add a 1M parallel to the capacitor to discharge it is disconnected. Otherwise it can give you a nice zing when you touch it after disconnecting.
Hi,
Please..Please...Please... realise that although you have got a voltage that the arduino can use at its input, that 5V has the possibility to be at mains potential.
There is a reason in that link for the skull and crossbones and the;
The circuit is ONLY for lighting an LED which is hopefully insulated from the surrounding environment.
This circuit has the possibility of making your Arduino LIVE at mains voltage.
@TomGeorge, if you would have read the whole post you would have noticed he uses a opto. So the Arduino side of things is completely save Yes, it's still playing with mains. But the low voltage side of things is nice and isolated (not taking in account lose wires and solder bridges )
@septillion: Thanks, I will try that. What power rating should those resistor have? I'm currently taking the care of discharging the cap "manually" when I need to touch it, but I guess that the 1Mohm in parallel is a good idea.
@TomGeorge: Yes, I'm aware of that. That's why if I get this thing running reliably, my next question will be how can I improve its safety. BTW, I'm using a cap rated for 630V as suggested, so it should be adequate. First cap I ever saw with color code markings! Also I have no loose wires, I hand-soldered a circuit board, taking some care. Everything is even screwed firmly in a "plastic box for electrical stuff". I'm no electrician "officially", but I have some experience with mains, at least I know what I'd better avoid touching :).
Ow, and a quick note. You can indeed use this to detects mains but don't expect the output of the opto to be active all the time. It will give you a 50Hz (or 60Hz on the other side of the water) signal with a little over 50% (because active low) duty cycle. Just keep that in mind when sampling the signal.
X2 grade is indeed a save choice but a normal 630V film capacitor is fine as well. Because even if it fails short circuit the worst that can happen is it blows the opto. But it's still well under the rating of the opto so the Arduino is save. Although yeah, a X2 is even saver because it can never blow the opto.
@Normal 1/4W is fine. In normal operation it will only see 22mW (220Ohm x (10mA)2 = 22mW)
I dimensioned the resistors such that only the resistor brings down the current to the absolute maximum rating for the opto coupler, in my case 80mA (but with power rating for normal usage + safety margin).
I added a fuse, since X2 does not open the circuit when it fails (Y2 would, afaik, bot I do not know if those have other issues, they are required for connection between phase and PE)
I used carbon resistors, because those fail safer. A metal film resistor might decrease resistance in case of failure and become a dangerous heating element.
I did this to detect a switch, so nothing that runs for a long time, but it should work.
Not also that your signal will be phase shifted, that is, your off times will not be in sync with the zero-crossing of mains.
I added a leaky peak hold circuit on the MCU side to make reading easier.
@ElCaron, your second point does nothing (if the fuse >80mA) because if the cap turns into a short it will not overrate the fuse if it then just draws 80mA. In a lot of times the resistor also fulfills the role of fuse.
Mm, I thought X2 needed to fail open as well... Because they are common as line filter aka across the line and then a fail to short circuit would not be pretty
On X/Y: Stackoverflow
I am not exaclty sure if X always, sometimes or just on very rare occasions fails short, but I always read that it is Y caps that NEVER fail short.
I have no idea why you wouldn't always use Y caps, and I have also no idea why some X2 caps are not for use in series, despite the fact that this should put a LOWER voltage over them than a connection in parallel.
On the fuse: Yes, in theory, the worst thing that could happen is that 80mA will be drawn, the carbon resistor overheats (at 17W!) and, being a carbon resistor, SHOULD burn and increase resistance until it opens the circuit (a metal film resistor could actually decrease resistance).
But with mains and the chance of setting my house on fire, I'd rather not go for the poor man's fuse but put in a real one. There shouldn't actually be a reason not use a slow blowing 60mA fuse, since more than something like 15-20mA should never be drawn except when switching on at an extremum.
BTW, one could also consider putting in a MOV there, to protect against voltage spikes in the mains line. I would certainly do that if the circuit is hard to access.
But although it only states "never short circuit" for the Y caps it still states X need to be "self healing" and are okay directly over line voltage (don't need a fuse). That would still mean no short circuit failing in my book And I still think it's just a matter which situations they need to handle safe. (Because never short circuit doesn't exists. Put enough voltage over it and at some point it will just flash over.) But I guess the real class specs are behind a pay wall because I can't find the test conditions...
No series use may refer to the voltage dependency of a cap? Or because you might expect to deal with low voltage (and it's isolation) it you use it in series but actually need mains rating for everything?
Although the resistance of a metal film might decrease, that would only put it over the power rating more and blow it to pieces as well.
But a MOV is prone to wear again... Making a PTC desirable...
@dlloyd, but that's making wasting heat again (0,56W for 2,4mA) But I must say, just to detect voltage you don't need that much current trough the LED. I have done it with 2x 120k (which gives just 1,0mA but only uses 0,22W). But yeah, 150nF cap + 220Ohm only uses around 40mW...
Yeah, a little bit of heat, but its shared through 2 resistors (only about 0.28W each). Also, there's improved safety protection on both lines (much less than 5mA which is better than GFCI trip level). The resistors also limit any surge energy on each line.
It seems the X2 problem is that for burning the short circuit area they need enough current, then, used as a reactance, current flow would be very high in the moment of a short circuit. Before burning the short circuit area they would burn something else...
P.s.: for the series resistor I would chose 2k2, because the peak current would be 325/2k2=150mA (or more with spikes) and it already seems to be dangerous for the optocoupler.