Is it possible to switch mains-powered devices (such as a coffee machine) on and off using the Pi? If so, what extra components do I need?
5 Answers
Yes this is possible, but you will need a relay to control the Mains power, and may need to shift levels (to properly control the relay and protect your RasPi). You may want to look at this add-on board which will both shift levels and contains two 10 Amp relays.
The easiest way is to use a 2 stage driver. You need to use a high voltage relay driver such as ULN2003 ULN2803 and then connect the output to drive a relay to control your desired component.
You can buy all those components on Mouser or DigiKey or any other major site. Just be warned that these components draw a lot of current, so you can not use the 5V that is available on the expansion connector, you need to have an external power supply with at least 1A power @5V.
Note: The reason for suggesting that a relay is used is that the Raspberry PI does not have enough power to drive that many transistor outputs, unless you use a FET transistors. A driver circuit makes sure you don't damage your precious RasPi even if you short the output or have a power surge due to the rush current of the relay switching.
-
May I ask why you don't drive the relay using a simple transistor circuit? Jun 25, 2012 at 9:30
-
1Raspberry PI does not have enough power to drive that many transistor outputs, unless you use a FET transistors. a driver circuit makes sure you don't damage your precious RasPi even if you short the output or have a power surge due to the rush current of the relay switching.– FarhadAJun 25, 2012 at 9:47
-
1Generally speaking, on stack exchange it is better to update your answer in response to requests for clarification in comments, that way all of the information needed is in the answer itself, not spread out in the comments. Also, it means that people can tidy up (delete) their comments when their request has been dealt with. Ideally answers should end up with few or no comments to distract from the answer. Jun 26, 2012 at 10:14
There's an American product called a PowerSwitch Tail which is basically an optically isolated relay in a box. They make a complete unit for 120V, but only kits for 240V supplies.
The kit assembly looks pretty straightforward (even for a beginner). The benefit of this route is that (once completed) all the high voltage connections are safely inside the case and that the board has been designed by a professional engineer with due regard for proper isolation between the high and low voltages and between the high voltage connections.
http://www.powerswitchtail.com/Pages/PSTIIU.aspx
While it is more expensive ($18) than a cobbled together circuit with a relay, it's a lot more convenient and (most likely) a lot safer too. If you were to roll your own device like this (with a proper board, case etc and all done safely), you probably wouldn't beat the price anyway, especially in hobbyist volumes.
I have interfaced a USB driven mains power switch from AVIOSYS - the USB NetPower 8800. I needed to use a powered hub as the RPi cannot generate enough power through the USB port to switch.
The AVIOSYS unit is supplied only with Windows drivers. I asked for Linux drivers and /or a protocol specification, but they refused to release any specification as they said that it was under non disclosure from Prolific who make the USB-serial converter this device used.
So I had to use a USB port capture utility to analyse the dialogue, and then reproduce it in a Python program on the RPi. There was a dialogue of over 50 messages to generate - I don't know whether they are all needed, but once initialised a single message is needed to turn on and another for off.
If you're interested the program is working on the RPi and you can download it at:
https://skydrive.live.com/#cid=500667A62B4F909A&id=500667A62B4F909A%21294
You need to also install the python-USB library on the RPi