sensing mains power

Hi,
I'm looking for the easiest (i.e. smallest) way of sensing whether mains power is on or off with an Arduino input pin.

My first thought was to use a circuit that drives a LED from 240V mains power and substitute the LED with an opto-coupler (a 4N25 for example).
There are some circuit diagrams on this page: Marc's Technical Pages: Mains LED

My worry with that is that the LED would "blink" at the mains 50Hz. This is not a problem with a visual-only LED indicator but will give false readings when read at intervals with the Arduino. I could add another capacitor parallel to the LED as shown in the diagram for the blinking LED on the website above. But I was wondering if there are any simpler and smaller ways of doing this?

The idea is to replace a ceiling lamp with an Arduino controlled LED lamp and to use the existing cabling and wall switch. There is a 3 core cable going to the ceiling lamp, so I could have permanent mains plus switched mains from the wall switch.

Add the capacitor not across the LED but across the arduino input pin. In other words on the transistor side of the opto isolator.

How will your Arduino be powered, can you use that somehow ?

The 470nF capacitor seems large to me. If the optocoupler is good with 10mA, use 150nF.
A capacitor over led/optocoupler is no problem. But it is saver on the low-voltage side.
However without the extra capacitor, the led/optocoupler is at 100/120Hz, and that is not hard to detect with the Arduino. Say with about 500Hz sampling rate in a timer interrupt.

A capacitor over led/optocoupler is no problem.

Except for the voltage rating it has to be.

The arduino will be powered from the permanent mains (it has to be always on) via a small 5V PSU (one of those USB chargers). The arduino will control the lamp / LED and will be hidden in the ceiling rose or similar. It will be sent commands via a RF module. The idea is that the light switch on the wall can also be used to switch the lamp on and off so that "she" does not notice the difference but I can still control the lamp remotely.. :wink:

I quickly made a schematic diagram of what I had in mind. Does this look OK?

mains_switch - Schematic.pdf (10.6 KB)

Oh, and these caps should be ok?
http://cgi.ebay.co.uk/ws/eBayISAPI.dll?ViewItem&item=160816947443&fromMakeTrack=true&ssPageName=VIP:watchlink:top:en

you can use an AC 230V (or 110V) microrelay to send a DC signal to your arduino. Simple!

Does this look OK?

Looks fine, appart from the fact I would also have a resistor in line with the LED. The only thing limiting the current is te 470R and the 0.15uF cap, I am not sure if that will pass too much current.

Capacitor should have class X dielectric implying ti is suitable for use as a mains "dropper"

Grumpy_Mike:

Does this look OK?

Looks fine, appart from the fact I would also have a resistor in line with the LED. The only thing limiting the current is te 470R and the 0.15uF cap, I am not sure if that will pass too much current.

How would you calculate the value for that resistor?

im_nikolaos:
you can use an AC 230V (or 110V) microrelay to send a DC signal to your arduino. Simple!

Not a bad idea. But I can't find one that is smaller than the LED circuit....

Grumpy_Mike:

A capacitor over led/optocoupler is no problem.

Except for the voltage rating it has to be.

The "flashing LED" circuit on the web page I linked to uses a 47uF 10V capacitor. Do you think the capacitor should be rated as high as the "mains dropper" capacitor?

No 10V is fine because the Zener and the LED means it dosn't get more than 10V across it.

There are 2 better Idea's I can come up with one (the simplest is a relay connected to the mains, 230V relays are common and cheap, the second and better one is an LDR and a neon light. About 10 years ago I designed a radio remote controller for a center pivot irrigation controller and I Tried several times to use Led's for 220 Vac sensing. After literally blowing SEVERAL OF THEM off of The board I tried to use wall warts to save the board design, I bit the bullet and shrink tubed a NE2 to an LDR snd Never had a problem again and the LDR NE2 combination was about $0.10 cheaper after the part price had been burdened by the assembly time my girls spent putting them together. If adequately being shielded from light totally I could measure a low on a pin 2 or 3 times in succession, I knew that the power was on at the point being monitored, didn't need filters, caps or special software just a completely light shielded LDR and 2 or 3 low measurements were more than enough... that and a .1 uF cap for the occasional transient. I didn't write the code for that job but it was written to my specifications and the "engineer" doing the code had no problems measuring the pin. Just a thought...

Doc

That's the kind of idea I was hoping for.. :wink:

So with the neon and LDR solution, all I need on the mains side is one of these:
http://cgi.ebay.co.uk/ws/eBayISAPI.dll?ViewItem&item=120842237010&fromMakeTrack=true&ssPageName=VIP:watchlink:top:en
and a 470k resistor?
On the Arduino side just an LDR pulling an input low, or even using an analogue input and another resistor forming a voltage divider with the LDR.

You mention a cap, where would that go in this case?
Would a normal 1/4W resistor be enough for the neon? Or does this need to be one with a higher wattage rating?

1/4 watt is more than sufficient since you'll only be pulling <1mA, but resistor must be rated for the supply voltage. From memory somewhere between 220k and 470k are often used. Note that, once the neon strikes the voltage across it will be fairly constant at somewhere between 60 and 90volts. We used to use such devices as HV voltage stabilisers in the days of valves (tubes to you yanks).

You should be able to buy a neon+resistor combination off the shelf at the likes of Tandy, RS, Maplins etc

Thanks a lot for all the replies...
I think the neon lamp + LDR is the most sensible and easiest option. I'll go for that. It will be a while yet before I get around to actually implementing that. I'll report back if there are any issues.

Thanks again for all the suggestions! :slight_smile: