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.

  • 18
    Warning: Interfacing with mains electricity involves working with potentially lethal voltages. Due care and competence is required.
    – Mark Booth
    Commented Aug 20, 2012 at 11:13
  • This blog post is about a project to remotely switch mains ON and OFF using a long range (>100m) radio modules. It does need some skills to put together.
    – seggy
    Commented Jan 21, 2016 at 8:49

14 Answers 14


EDIT 2018

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.

enter image description here

# * * * 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.

enter image description here

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)

  • 8
    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. Commented Aug 17, 2012 at 17:10
  • Thanks! This looks not more difficult or dangerous than a simple screw terminal. The RaspPi could also switch of its own power supply ;-)
    – Jakob
    Commented Aug 17, 2012 at 18:44
  • 1
    @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. Commented Aug 21, 2012 at 5:53
  • 3
    I have seen catastrophic SSR failures (like, failing ON) from those cheap eBay ones. Run away from them. Commented May 10, 2016 at 4:24
  • 1
    Edit 2020 :) If you have an home automation system running (zigbee, hue) you can interface with quite cheap Ikea remote wall sockets like this: 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.
    – Ron
    Commented Feb 25, 2020 at 7:32

You could get one of these (RF) remote control power switches (Assuming you can find one suitable for your local power receptacles)

enter image description here

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.

  • 2
    +1. thats a really good idea, and a lot safer than interacting with the mains as a beginner Commented Aug 21, 2012 at 8:28
  • 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.
    Commented Aug 6, 2018 at 13:03
  • @Sz. Sure - but that would require someone to reverse engineer the RF protocol. Might not be too difficult though. Commented Aug 6, 2018 at 22:44
  • 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: instructables.com/id/…
    – Sz.
    Commented Aug 7, 2018 at 16:39

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.

  • 1
    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. Commented Nov 4, 2012 at 14:37
  • 1
    there is kit available for 220V, see here
    – dm76
    Commented Jan 4, 2013 at 20:38
  • out of stock on both sites. any alternatives?
    – avi
    Commented Apr 3, 2015 at 14:57

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.

enter image description here


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.

  • 2
    Cool - I am going to buy one and take it apart to see how it works :-) muhahahah
    – Piotr Kula
    Commented Aug 19, 2012 at 11:21
  • That's the evil cackle I love
    – Pitto
    Commented Feb 12, 2013 at 12:54

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).


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:


  • Yes, I also though about using an existing electricity usage monitor and switch, but it's difficult to find a device that can easy be controlled without having to install proprietary drivers.
    – Jakob
    Commented Aug 23, 2012 at 7:08

X-10 serial interfaces exist. That would have the advantage of controlling AC switches that aren't right there.


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

My 5-cents.......


A more complex but rewarding solution, would be to interface with RF devices directly. Check this out: http://rayshobby.net/?p=3381


you just need 2 things:

  1. a transmitter directly interfacing via RXD, TXD of the serial connector of your raspi:


  1. a receiver with a relais (toggle switch). This retains its state even during a power outage:


some further info is found here:

Cheapest way to controlling multiple power sockets (lights) through Wi-Fi


Here is a SAFE way!

I used an off the shelf wireless transmitter, a wireless powerswitch (both 220 or 110volt will work) and a 5v relay.

This high power switching was done using an arduino but raspberry would work just as well (no code required for the transmitter).



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.


Belkin's WeMo Switch + the Ouimeaux Python library = arbitrary control over an outlet without any soldering or mains power exposure/risk.

Downsides: costs $50 US per outlet at list price, only works when your wifi network is up, and may or may not be compatible with the plugs in your part of the world.

Not the answer you're looking for? Browse other questions tagged or ask your own question.