Page 1 of 3

### how to sense 220v input -solved- I hope

Posted: Thu Jun 28, 2012 11:24 am
Hi all

The rpi has captured all my interest. And I like to get fun developing some hobby projects with it, I think that it have a enormous potential for a really low price.

My first project would be a home alarm system, as long as I don't have my rpi yet (its comming, only 9 weeks more...) I begin predesigning some parts of the alarm. One of this parts is to sense a 220v input, in case the mains going down the rpi will advertise you through internet and a buzzer (for that the rpi alarm system and my modem will keep powered with a SAI).

So I need to check if in any moment the mains goes down. For that I design the next circuit:
Circuito-1.GIF (5.12 KiB) Viewed 43242 times
The usual way should be to use a transformer to drop down the voltage and get galvanic isolation, but I decided for this way to get the smallest - cheapest option with the less quantity of elements possible.The optocoupler gives the isolation and the power consumption on R1 is less than 0.5W and I think thay all the components will cost less than 3 euro.
The only limitation is the current on available on the 5v exit "TO RPI", between 0.5mA - 1mA, but for me is enough.I plan use an I2C I/O expander, and read this input with it.

I will be glad to receive any kind of tip of suggestion about this design, like other ways to do it (other ways to sense 220v input with rpi), and I hope that this schematic can be helpful.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Thu Jun 28, 2012 12:53 pm
You need to use a 3 volt line as input to the Rpi, 5 volts will harm the inputs. Also a 100 ohm resistor in series with the input to the Pi will stop the Pi being damaged if you make a mistake and program the input as an output.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Thu Jun 28, 2012 1:24 pm
I think that circuit will give you problems, as you are trying to operate at the very lowest margins of the 4N25.

As a start, I think this page : http://www.marcspages.co.uk/tech/6103.htm will give you some better ideas on how to drive the LED of the optoisolator harder from mains without dumping so much heat through a resistor.

This will give you a chopped output from the isolator, but this could be smoothed using an RC network, and will give you a better signal to work with on the low voltage side.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Thu Jun 28, 2012 2:54 pm
I wouldn't use that circuit, it could easily kill you or your Pi.
Presumably you're going to be running the Pi off a battery, and the battery will be charged from the mains? If so you've already got a safe isolated supply that you can monitor to see if the mains has gone down.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Thu Jun 28, 2012 2:59 pm

You're right, with my design I'm in the very low limit of the 4N25, and the only way to improve is wasting power on the resistor, that implies bigger and more expensive elements, a bad idea . Your suggestion is much better.
I leave here the redesign if anyone serves
Circuito-2.GIF (7.01 KiB) Viewed 43192 times
Now the optocoupler is drived with 10mA.

I'm won't connect the output directly to the RPI, I'll use an I2C I/O expander that works at 5v, this is the reason to I'm looking for a 5v output.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Thu Jun 28, 2012 3:30 pm
Maybe the OP wishes monitor a mains supply that is independent of the one his RPi is running from.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Thu Jun 28, 2012 4:29 pm
Shure, this is the point to work as an alarm. The supply power for the RPI will be give by a SAI, and the optocupler look for a different one, the general power, to detect a possible power off.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Thu Jun 28, 2012 6:14 pm
Pepepaco_82 wrote: I'm won't connect the output directly to the RPI, I'll use an I2C I/O expander that works at 5v, this is the reason to I'm looking for a 5v output.
I'm wondering what the reason for doing it that way is?

I don't think it will add in any more protection - if mains get through the opto isolator then it's going to fry anything in its path - unless there's another opto isolator in the I2C...

So I think it's just adding complexity. I'd be really tempted to simply wire the GPIO pin to the collector of that opto transistor, enable the internal pul-up and wire the emitter to ground. you could put a little RC network there to smooth the signal, but it's probably not needed - just poll the pin for a second or so until it changes state and you'll soon see if it changes - change means power, no change means no power...

Keep it simple - the less there is, then the less there is to go wrong.

-Gordon

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Fri Jun 29, 2012 8:16 am
Hi all,

Many thanks for all your replies and help. I really appreciate it.

So, what about add a fuse and a varistor to get some protection to the RPI? something like this:
Circuito4.GIF (6.94 KiB) Viewed 43143 times
I don't think it will add in any more protection - if mains get through the opto isolator then it's going to fry anything in its path - unless there's another opto isolator in the I2C...

So I think it's just adding complexity. I'd be really tempted to simply wire the GPIO pin to the collector of that opto transistor, enable the internal pul-up and wire the emitter to ground. you could put a little RC network there to smooth the signal, but it's probably not needed - just poll the pin for a second or so until it changes state and you'll soon see if it changes - change means power, no change means no power...
I explain my reason to use an i2c I/O expander:

I'm planning to use a keypad and a LCD 4x20 like HMI in mi "PI alarm" this LCD works over i2c, and is possible to plug also a 3x4 keypad connected to the LCD and read the button pressed over i2c.
I have this old LCD at home from from an older project. And I also like to learn to manage i2c bus with the rpi, to manage other devices in the future...
So, as long as I have to have and i2c interface for the LCD and Keypad, I also would like to use an i2c I/O expander to sense 3-4 inputs and drive 3-4 outputs leaving free at the moment the GPIO ports of the RPI.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Fri Jun 29, 2012 9:43 am
The mains supply will not get through the opto-isolator, that is the whole point of them in the first place. A Fairchild 4N25 states a minimum breakdown voltage of 7500V, and if you've got that coming down your mains supply, the opto-isolator breaking down will be the least of your worries I would expect.

Don't forget that R1 will get hot as it is being deliberately run at its maximum power rating, so mind where you place it. I wonder if using a 1/8W resistor would deal effectively with harmonics, and waste less power into the bargain.

I would go with the recommendation of using a bridge rectifier, like the Vishay W06G-E4. They're about £1.50 for a pack of 5 from RS, but you might find singles at somewhere like Maplins or CPC. Only 10mm across, half the solder joints and looks neater in my opinion.

Gordon's recommendation about polling an unsmoothed input, is sound too. The opto output will be pulsing at 100Hz, so you would need to wait long for a confirmed signal.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Fri Jun 29, 2012 6:26 pm
I don't like the title of this thread.
In my opinion, anybody who wants to work with mains and calls it 'playing' is obviously no suited to do so.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Sat Jun 30, 2012 6:46 pm
I don't like the title of this thread.
In my opinion, anybody who wants to work with mains and calls it 'playing' is obviously no suited to do so.
Yes, probably the title should be only "How to sense a 220v input". But if I had known how to do it I don't have to open this thread...
With the help of people from the forum I can improve my first solution (witch was not the right one), and when I get my RPI I try the solution and post the results.

I think that's the correct way to Work with the forums. Trying to solve problems, learn and teach yourself, and publish the results so others can also improve and learn...

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Thu Jul 05, 2012 11:29 am
One thing that the article in Mortimer's link doesn't mention is the need to use a capacitor that's expressly intended for connection to the mains. There's a bit of bumph on the difference between vanilla high voltage capacitors and mains (Class X and Y) caps at:

http://my.execpc.com/~endlr/line-filter.html
Class X is for applications where failure could not lead to electric shock (hot to neutral). Class X1 capacitors are intended to operate safely even in the presence of spikes on the mains supply of up to 4 kV (installation category 3 or overvoltage category 3 according to IEC60664), which are normally industrial supplies, but some standards call up class X1 capacitors if they are connected directly to the mains supply upstream of the equipment fuse, irrespective of the type of mains supply. Class X2 capacitors are intended to operate safely even in the presence of spikes on the mains supply of up to 2.5 kV (installation category 2 or overvoltage category 2 according to IEC60060), which are normally residential, commercial and light industrial supplies. X capacitors can be found from 0.001 uF to at least 10 uF and are only made in film.

Class Y capacitors are for applications where failure could lead to electric shock if the ground connection were lost. This includes hot/neutral to ground, and antenna isolation capacitors. Because Y capacitors shunt current to ground, leakage-current limitations limit their size to a maximum of about 4700 pF in many commercial and industrial applications (but refer to the relevant standard for definite information) and about 470 pF in medical applications. Larger ones are available however. Y caps are available from 1000 pF to 0.1 uF and are made in both film and ceramic.
As failure of your capacitor wouldn't pose a direct risk of electric shock then I'm suggesting a class X2 capacitor as X1 is really for industrial applications (although it wouldn't do any harm to use the heavier spec item). I'd be very happy if "proper" experts chime in here though btw!

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Thu Jul 05, 2012 11:40 am
How about you plug in another 5v power adaptor into the mains and sense when that goes off?
That way no dangerous playing with the mains required.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Thu Jul 05, 2012 11:47 am
felix123 wrote:How about you plug in another 5v power adaptor into the mains and sense when that goes off?
That way no dangerous playing with the mains required.
Given how cheap plug in power adapters are nowadays, that is probably quite a sensible solution. I'm sure 3.3V ones are available too, which perhaps be better in this case.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Thu Jul 05, 2012 4:35 pm
Going off in another direction, why not a photodiode / photo transistor / photoresistor glued to a mains neon?
Some experimentation might be needed - neons ain't that bright, but it has the advantage of being totally isolated.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Wed Jul 11, 2012 7:39 pm
Hi, I was going to suggest an old phone charger as a safe option, but saw felix123 had already covered that one.
Also liking Burngate idea, but need to match neon colour transmitter to receiver. For example, my understanding is that photo-diodes/transistors tend to work better in the infra-red range. Just looked up a data sheet for LDR (Light Dependant Resistor) and the optimum range is 500-600nm, which is yellow to green.
Another possible option is a relay that has a coil at the voltage you are testing and contacts designed for low voltage/current signals.

As for "Playing with it!" DON'T!

I'll have to look at a Siemens S5 PLC input card to see how they do their inputs.

REFERENCES:
(RS website quoted, but others are available!)
http://docs-europe.electrocomponents.co ... 01a9d6.pdf

http://en.wikipedia.org/wiki/Visible_spectrum

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Wed Dec 19, 2012 2:01 pm
hi Pepepaco_82

I am also trying to sense presence of mains supply.
I almost came to the conclusion of designing the circuit similar to this one.
Please confirm if the latest circuit provided works well.
TIA

PS: Actually i am working on this twitter bot @powerbot_tn, which detects & tracks mains availability.

### Re: Playing with mains, how to sense 220v input -solved- I

Posted: Wed Dec 19, 2012 2:47 pm
Pepepaco_82 wrote: Yes, probably the title should be only "How to sense a 220v input".
Fixed that for you..

### Re: how to sense 220v input -solved- I hope

Posted: Wed Dec 19, 2012 4:58 pm
In an old design I had for detecting the cheap rate electricity for my home network I just nailed a neon light on the mains using the usual resistors and small neon. Then a few cm's away I put in an ORP12 light sensitive resistor (LDR) and you can tie that up to a high impedance IO like the ones on the Pi with a suitable resistor so that it will cause the IO line to be fed with a nearly full voltage to nearly ground voltage depending on the light level. Then house the two together in a blackened box and it works great. You have physical isolation from the PI board to the mains circuit. It also has the bonus that if you do put a little hole in the case near to the neon then you can see whether its on or off even if the Pi board is powered down. Not all that useful for general mains to the house but very useful for low rate only mains supply.

Oh - still got the pic. This box worked for years and years with no problem. Mains cable going in at the back, my proprietry network going out the DIN on front.

### Re: how to sense 220v input -solved- I hope

Posted: Wed Dec 19, 2012 5:32 pm
Why bother with a bridge rectifier when a single diode will do, it just means that the choppy output will be a bit more so but you are making up for that later anyway.

Although my way would be to connect the output of a low voltage transformer to the mid point of a voltage divider between the PI power rails diode clamp the other terminal to the rails and feed straight into a GPIO (if Vdd+0.7 or Vss-0.7 will kill the input even with a high resistance voltage divider then use another divider to keep the input in range)

### Re: how to sense 220v input -solved- I hope

Posted: Wed Dec 19, 2012 5:59 pm
This "Optocoupler" is bigger than the RPi itself. I would use a solution similar to Mortimer at Thu Jun 28, 2012 1:24 pm, but still a little simpler. The LED is of course part of a ready made optocoupler.

The current through the LED is sufficient with the integrated Pull Up in The RasPi or an optional 10 k Pull Up.
You get a 50Hz pulsed output (active low), but that is sufficient to detect the presence of mains supply. If you dont want this, just put a capcitor with about 4µ7 or 10µ in parallel to the opto-transistor. If you need a phase synchronous signal for a dimmer, then a more complex design is necessary. I did this years ago, with very narrow pulses around the zero crossing, to reduce power consumption.

### Re: how to sense 220v input -solved- I hope

Posted: Mon Jan 07, 2013 9:25 am
http://www.simple-electronics.com/2012/ ... rface.html

I used the resistor values from that site, threw away the 4N25 and transistor for a H11AA1, and voila!

Works really well on my Arduino, to use it on RPI just feed it 3.3v instead of 5v.

### Re: how to sense 220v input -solved- I hope

Posted: Wed Jan 09, 2013 7:26 am
I used C1 and R1, when using H11A1 you must drop D1 as the chip has it internally.

### Re: how to sense 220v input -solved- I hope

Posted: Wed Jan 09, 2013 7:48 am
aTao wrote:Although my way would be to connect the output of a low voltage transformer to the mid point of a voltage divider between the PI power rails diode clamp the other terminal to the rails and feed straight into a GPIO (if Vdd+0.7 or Vss-0.7 will kill the input even with a high resistance voltage divider then use another divider to keep the input in range)
That's got to be the simplest and cheapest alternative, and it keeps the "don't play with mains" people quiet.