How can one control AC power (220V) with a Raspberry Pi?
I though about using the Raspberry Pi to switch on and off other electrical devices which unnecessary consume power in stand-by mode. In short, I'd like to control an AC socket or multiple sockets. How can one let the computer "push the button":
Image: CC-BY-SA 3.0 by Firstfreddy
The physical solution is a relay, but I don't want to build all on my own and play around with 220V and sparks when switching on and off ;-) By the way the Raspberry Pi requires 2 Watt in idle mode, so only using it as a switch to save energy might not make sense, so it should be usable for other purpose at the same time.
***Warning:*** Interfacing with *mains* electricity involves working with **potentially lethal voltages**. Due care and competence is required.
Years later and the hobbyist microelectronics community has exploded thanks to the like of cheap and power embedded computers, like Raspberry Pi. This caused mechanical relays that work direct of GPIO on 5/3.3V allot cheaper and easier to get.
You can get them as singles or premade (Bangood, Seeedstudio, Gearbest, eBay, etc) ranging from 4 to 48 "channels" I have even seen. These are much more compact size, very affordable, safe and easy to use.
# * * * WARNING * * * #
Switching mains involves interfacing with potentially lethal voltages. Due care and competence is required. Death is possible. YMMV. This paraphrases Russell McMahon's advice on Electrical Engineering
---Original Answer 2012---
Well you could use a Solid State Relay which is much smaller and easier to control than a mechanical relay (The big 12 volt ones used in automotive industry) , using an MCU or in this case Pi's GPIO pin.
You have to drive the input pin constantly to keep the relay on (just like a mechanical relay). So if something fails with that signal, then the power goes off. To avoid that you have to design another circuit that can sustain itself.
But you can get these pretty cheap on eBay and they are completely safe (isolated), so they won't blow up the Pi and do not require a lot of power to drive them, about 3~10mA. Just check the details before buying one. It is also worth noting they can heat up if you load them heavily (close to the maximum rating)
You don't always have to keep driving the pin for the relay to stay open. There's a special type of relay called bistable relay. It has two stable modes (on and off), you can switch between them and they stay like this until they are switched again. They are not so common as normal relays however.
Thanks! This looks not more difficult or dangerous than a simple screw terminal. The RaspPi could also switch of its own power supply ;-)
@ppumkin: I don't think it's possible to build bistable SSR. I was reffering to mechanical relays only. But your answer suggest that in mechanical relays you also have to always drive input pin to keep it's state. And it's not really true. In case you don't want that, bistable relays are better than SSR (if you can ignore their downsides).
Bistable relays are pretty expensive and they need 12v to power to the coil. Not really suitable just to wire into a Pi- as the op has asked. But still- Some people might find it interesting :-) Thanks @KrzysztofAdamski
@ppumkin: There are 5V bistable relays (zettler AZ850P1-5 for example) and I don't think they are that expensive (< 2$, just like mechanical ones). But if that's actually suitable depends on what you need to do. Sometimes energy savings coming from not having to drive the input pin all the are worth it. You are right that it may not be suitable for the OP as it would be hard to save energy with RPi by sleeping or something like that.
@ppumpkin: A bistable SSR doesn't exist. If you go for a bistable relay, then you're talking about an electromechanical switch, which needs a short pulse to "jump" from one position to another (from open to close, for instance) and needs another short pulse to do the opposite. Once in its stable situation, it doesn't need energy any more. Examples can be found at www.eltako.com: [http://www.eltako.com/en/switchgear-power-units/k-electromechanical-switching-relays-control-relays-coupling-relays-and-installation-contactors.html]
I have seen catastrophic SSR failures (like, failing ON) from those cheap eBay ones. Run away from them.
@ppumkin: "Bistable relays are pretty expensive and they need 12v to power to the coil. Not really suitable just to wire into a Pi- as the op has asked." -- I bought dozens of bistable (latching) mechanical relays (years ago), which can be directly driven by a MCU. (I used them with the meagre 3.3V GPIO of an MSP430G2.) And they were not "pretty expensive", but dirt cheap, too.
As comments pointed out the "cheaper" ones seem to have catastrophical break downs. For where I lived the ones NOT from ebay were not cheap..
Those comments were about SSRs, not mechanical relays. I've followed the subthread started by "In case you don't want that, bistable relays are better than SSR".
Oh right.. this was quite a few years ago. Agreed, I have seen those new relays that you can drive from MCU, like on Seeeeeeed? But I think back then they were not that popular or cheap and mostly used those big 12v mechanical ones, hence the solid state relay answer... probably not "that" relevant today any more
Edit 2020 :) If you have an home automation system running (zigbee, hue) you can interface with quite cheap Ikea remote wall sockets like this: https://www.ikea.com/gb/en/p/tradfri-wireless-control-outlet-00364477/ I am linking the english version (UK) but they are available in most countries now I think. I personally use it with node-red and domoticz to turn a 3d printer on and off remotely. Works quite well.
You could get one of these (RF) remote control power switches (Assuming you can find one suitable for your local power receptacles)
and hardwire the RPi to the remote. This has the advantage of being isolated and not requiring any mains wiring. You'd have to take into consideration the power consumption of the device if the goal is to save power.
+1. thats a really good idea, and a lot safer than interacting with the mains as a beginner
Even better is to add a 433MHz transmitter module to the Pi instead, so you a) don't need to sacrifice the remote, b) can extend it arbitrarily (e.g. to also talk to products not readily compatible with that particular remote; etc.).
@Sz. Sure - but that would require someone to reverse engineer the RF protocol. Might not be too difficult though.
True, indeed, I forgot about that added difficulty. My mind was set by reading about a lib recently that supposedly made it very easy. (Never tried though.) I can't seem to find that one now, but e.g. this post appears to make it even simpler than that: https://www.instructables.com/id/Super-Simple-Raspberry-Pi-433MHz-Home-Automation/
Both Adafruit and Spark fun sell an assembled device exactly for this purpose: The Power Switch Tail exists in several variants, fully assembled or as kit. The kit's assembly instruction (PDF) include schematics.
The poster does not mention where he lives (I suspect he is not in N. America - based on the need for 220V). The device you mention is designed for the US (plug type) and does not handle 220V.
SainSmart sells Arduino relay modules (shields), they could also be used on Raspberries. There are different models (higher amperage, number of outputs, etc). For example SKU:20-018-100-FBA can be used for "equipment with a large current". And a useful article discussing Using the Raspberry Pi to Control AC Electric Power that mentions the SainSmart.
You can get a certain power suply with a USB-Connection and control it with this http://sispmctl.sourceforge.net/ Debian/Raspian has the package sispmctl by default.
Cool - I am going to buy one and take it apart to see how it works :-) `muhahahah`
Use a Telldus Tellstick!
It has many applications and supports sensors as well.
Code samples in many languages: https://github.com/telldus/telldus
A router which has an HTTP API to control the TellStick if you dont want to have it directly connected to the PI: http://www.dovado.com/index.php?option=com_content&view=article&id=13&Itemid=20
You can also buy a TellStick Net device and send API requests to Telldus Live from the PI (internet access needed).
I LOVE it LOVE it LOVE it and then LOVE it agan. Simply can't wait for mono to work to use my lovely switchking server.
you could also use an existing device. I have interfaced an Aviosys NetPower 8800 switch which connects via USB.
It didn't come with Windows drivers and they weren't willing to nrelease an interface spec so I could build a Linux driver.
But I've written a Python program to control it:
totally different approach:
as you like your server to be up, you probably like the idea of an UPS. You can use an APC and control is by the raspberry. This will let you powercycle the ups. The smallest UPS can be used, either new or secondhand. The secondhand option will set you back 50euro's/dollar or even less and has extra benefits (and extra joy in makeing a webinterface for even extra monitoring).
This way you can hook up more devices and let the raspberry send shutdown commands during powerfailure etc
For things that switch relatively occasionally mechanical relays are hard to beat. "solid state relays" have better cycle life but much higher cost and much higher operating losses.
The problem is that a lot of relay boards on the market are badly designed, all too often when I look at such boards I see inadequate creepage and clearance distances. I would not buy such a board for controlling mains without being able to see the layout of the power traces.
It's not helped by the design of the relays themselves, a common design of relay has one of the contact pins in-between the two coil pins. This makes it much harder to maintain creepage/clearance with these relays than it would be with a relay that has the coil pins at one end and the contacts at the other.
https://www.sainsmart.com/collections/internet-of-things/products/4-channel-5v-relay-module appears to be a sensible design. If you look at the picture of the bottom you can see they have slotted the PCB around the common terminal of the relay to control creepage distances.
You should obviously also make sure you mount the board securely in an enclosure that either well insulated or earthed and make sure all wiring is adequately restrained.