Greetings, fellow Raspberry Pi enthusiasts.  We are about to create a multi-purpose gadget that is sure to impress!  My primary reason for building this to to control my Christmas lights, but that is only one of many possible uses.  Basically you can plug in up to 8 appliance and turn them on and off independantly from your smart phone!  If you want to setup port-forwarding on your wireless router, you can control your appliances from anywhere in the world.  (Although why you would want to turn on your blender from over-seas is a bit of a mystery.) Please leave me a comment letting me know how you use your Web Controlled Powerstrip!

Before we get started, I want to give credit where credit is due: TheFreeElectron wrote an excellent instructable on controlling the Raspberry Pi GPIO pins from a web-browser. In fact, in-order to complete this instructable, you will need to follow almost all of the steps from his instructable including using his awesome web application for controlling your power-strip.

Secondly a reminder that working with mains power can be extremely dangerous. Please be very careful. Those of us that have had the experience of touching a live 120 volt wire do not EVER want to repeat the experience.

Assuming you already have your raspberry pi with the adapter and SD card, you should be able to round up the rest of the ingredients with $40-$50 dollars.

Here's what you're gonna need for this project:
  • Raspberry Pi with SD Card
  • Micro USB Power supply (Make sure your power supply can supply a full 1 amp)
  • Project Box - Available at you local Target ($14.99) - Hurry these are seasonal!
  • 8-Channel 5V Relay board - EBay (About $10 shipped, I found mine here.)
  • 4 (qty) 15-Amp Power Receptacles (Lowe's or Home-Depot, get the cheap ones about 80 cents each)
  • 18AWG Solid Hookup Wire (3 colors) - Radio Shack (About $8.50)
  • Short length of 14-Gauge wire. (Only need about 15 inches of white, I found some scrap)
  • Large wire-nuts (Red or bigger)
  • Colored jumper wires - Female to Female buy them on EBay here.
  • Power cord (I used an old computer power cord)
  • Scrap wood pieces
  • Double Stik tape
  • 1/4" wood screws
  • Optional (but handy):  A female end of an extension cord or broken holiday-light set.


  • Trim or plunge-type router with 1/8" straight cutting bit.
  • Wire strippers
  • Drill & drill bits (various sizes)
  • Screwdrivers (various sizes)
  • Multi-meter (for testing circuitry)
  • Utility knife

Step 1: A Faceplate to Be Proud Of

We'll start by modifying our project box to expose the face of our power receptacles. Take your time on this step, you want it to look nice since you're going to be impressing all of your friends with it.

We will use a spare wall plate as your template to draw the shapes you will be cutting out with the router.

Start by using a ruler and straight edge to draw a straight line centered across the long side.
Next locate the exact center of that line and make a center-mark.
Now lay your ruler along the line with the center-mark at the 10 cm point on your ruler. Carefully create a mark on the center line at positions 4, 8, 12, and 16 cm. We should end up with four center-points spaced 4 cm apart and centered along the line. These center-points will line up with the screw-hole of the wall plate and allow you to trace each of the large face-plate openings as shown. I also recommend drawing another reference line across the top or bottom to help make sure the wall-plate is parallel with the box edges when you trace the face holes.

Next use your router to carefully rout-out each of the 8 shapes to allow the receptacles to slide into place from the back. It may take a few tries to get them to fit nicely through the holes, but you'll get it.

Tips: Routing MDF is messy with lots of fine dust. You may want to have someone blowing compressed air on the lid while you're routing to keep the dust from obscuring the lines. Keep one of your receptacles near by so you can continue to check the fit and shave off the edges that are preventing the fit. Once all of the holes are cut and fit, clean up the whole mess with a damp cloth.

<p>Thanks for the great writeup. I am currently about to use this in a build I am doing for a reef/aquarium controller. I also have this working with voxcommando and controlling the outputs using voice control. Just started recently and still have plenty to do on it. </p>
<p>What should i need to programme on raspberry pi??</p><p>anyone can help me please...</p>
<p>nice.......plan to have similar unit too....</p>
<p>For the electrician out there - Is this safe?</p>
<p>The issue I have beside the wooden box, is the number of receptacles running from a single power socket. If too many current heavy devices are plugged into this and turned on, it would easily exceed the rating of the wall power socket.</p>
<p>It is not safe. He's using an MDF/wooden electrical box to house his receptacles. Since this is flammable, an arc fault could easily start a fire. However, if the project was installed in a code-compliant metal electrical box, I expect it would be safe. I would also check to see if the relay us UL Listed, to protect myself from liability should it fail.</p>
<p>MDF in not the bestest choice but it's ok. It can not be in flame, can only be smoky. BTW there are electric fuses in every house!</p>
<p>Safe for low powered devices such as a modem, router, lamp, fan, etc. I wouldn't try to connect a space heater or refridgerator to these outlets.</p>
<p>do we need resistor or transistor to bridging our pi with relay? because i thought raspberry only support 3.3v</p>
<p>Made it. My preference for this project was only 4 outlets, so I used a 4 relay board and modified the web interface on my Pi. The 2 outlets on the left are always on and provide GFCI protection for the entire setup. The 2 middle and 2 right are controlled by the relay board.</p>
<p>Very cool! How exactly did you wire up the GFCI with the other sockets?</p>
When you buy a GFCI socket, the tiny instruction manual tells you about it. The main power cord is connected directly to the input connections on the GFCI socket. There are 2 output connections for hot and neutral (a new GFCI socket will have them covered by a warning on a yellow strip that tells you to read the manual) that I have splitting off to:<br>1. An internal power outlet to power my Pi.<br>2. The neutral split off to the 4 other outlets.<br>3. The hot split to the 4 relays which then go to the outlets when switched.
<blockquote>2. The neutral split off to the 4 other outlets.</blockquote><p>When you say &quot;split&quot;, is that the same way it was done in the tutorial? Does &quot;split&quot; mean <strong>'in series'</strong>?</p><p>NOOB</p>
<p>Pretty much like the tutorial above. Just have the neutral wire on the incoming power connected to the neutral connections on all the outlets.</p>
<p>phenomenal project! Thank you @rleddington</p>
Thanks for a great instructable!<br><br>I had issues with the relay board. Some channels just wouldn't work. I had 2 boards, each populated with Songle SRD-05VDC-SL-C relays. Different channels on each board, but 4 worked on board 1, and 5 on board 2. I checked and rechecked voltages and wiring and finally got all the relays to function properly when I supplied 5V to the VCC pin on the 3 pin header as well as the JD-VCC. Assuming that this was linked to the other VCC pin, I disconnected my extra wire, and moved the white jumper wire to pin 4 on the RPi header. Suddenly all works swimmingly. <br><br>What are the downsides to this hack, if any? I'd like to figure that out before close up the box and install it 100 miles away. This is supposed to save me trips, not add to my problems.
<p>One &quot;downside&quot; might be that there's no provision to report back the &quot;actual&quot; state of a relay. If you trigger a relay, and the circuit doesn't come on - how do you know (from 100 miles away)? To &quot;close the loop&quot; you'd probably want some way to monitor voltage in the circuit itself and report that back via the 'net.</p>
<p>The devices I plan on connecting to the outlet controller:</p><p>1) DSL modem 1<br>2) DSL modem 2<br>3) Main wifi router for the camp<br>4) Multi-wan router linking 1&amp;2 to 3.</p><p>My controller will be connected to 1 of the DSL modems.</p><p>So I will be able to tell whether the selected device comes back up after I reboot it electronically.</p>
<p>Interesting... I did this in the FL apt but without the better idea of the MultiWan redundancy.</p><p>You really should wire this in reverse. Use the NC (Normally Closed) positions on the relays and use the switch not to turn them ON, but to turn them OFF (i.e., hard-reset). That way the natural position of a non-active relay is power on to the connected device.</p><p>Additionally, since you already have some redundancy, get 2 Pies and 2 sets of relays. Have each Pi able to reset the other (trust me, occasionally you may need this) and have all the other equipment go through two NC relay connections, one on each Pi. This way either Pi can reset the system.</p><p>While you are at it...</p><p>Another major flaw is that the mode of the GPIO pin is set from the web server page. Not good. This should be done at boot time.</p><p>In the file /etc/rc.local just before the last line add the following 2 lines for each relay connection coming out of the Pi GPIO <em><u>Pin</u> </em>(of course substitue <u><em>Pin</em></u>, for what ever pin number you use). This will put the GPIO pins (and hence the relays) into a default position on boot-up, without having to open the web browser.</p><p>/usr/local/bin/gpio write <u><em>Pin</em></u> 1<br>/usr/local/bin/gpio mode <u><em>Pin</em></u> out</p>
<p><strong>@bmikeb &quot;Additionally, since you already have some redundancy, get 2 Pies and 2 sets of relays. Have each Pi able to reset the other (trust me, occasionally you may need this) and have all the other equipment go through two NC relay connections, one on each Pi. This way either Pi can reset the system.&quot;</strong></p><p>What is the redundancy? Can you elaborate on how one Pi would reset the other? An if either Pi could reset the system, how would the interface look. Wouldn't there be 2 UI's? Wouldn't that break the SOLID principle.</p>
<p>Put a camera on the (s)Pi</p>
<p>I am looking to use this to control 24 outlets with one Pi. Anyone have advise for that?</p>
<p>Great post, thanks.</p><p>A question : where to find the application that runs on your phone and are the sources of this app available ?</p><p>Thanks in advance,</p><p>Domi</p><p><a href="http://www.arduino4fun.com" rel="nofollow">www.arduino4fun.com</a></p>
<p>I have a power conditioner set up for my recording equipment. I've been looking at power sequencers with conditioners built in, and the price point doesn't justify the functionality for me. Does anyone know if I can safely plug this project into a power conditioner? Should there be any concerns with the 'cleanliness' or stability of the power coming out of the relay-controlled outlets?</p>
<p>The relays are meerly switches. Their will be no interference with the &quot;conditioned&quot; voltage or frequency.</p>
<p>hi rleddington,</p><p>Thank you for posting this. I am trying to do this as well using my raspi2.</p><p>I am relatively a newbie w.r.t to anything electrical, so I have a rather simple question.</p><p>Is the white wire (14 gauge) you talk about solid or stranded? I have a stranded one and it doesn't seem to fit the hole on power receptacle. Pretty sure am doing something wrong.</p><p>Thank you</p><p>Srikar</p>
<p>The holes in the back of the receptacles are for solid wire only. If using stranded strip in the same manner as solid with the exception of completely removing the cut insulation. Slide most of the insulation off of the end of the wire leaving just a little bit of the stranded wire in the end of the end of the insulation. Then bend your exposed stranded wire around the side screw for whichever side you are connecting to and tighten. The insulation keeps the individual strands from separating and from &quot;popping&quot; out from under the screw head.</p>
<p>Another flaw, easily corrected...<br><br>In the system as described, in the main web page index.php, the process of loading the web page uses the &quot;system(gpio mode <em>$pin</em> out)&quot; function to establish the mode of the control pins and sets them to OUTput.<br><br>Yes, the mode of these pins does need to be set to OUT. However, this is being done in the wrong place. In the current set-up you must access the web page to initialize the pins. Not good. This should be done much earlier, when the Pi first boots up.<br><br>To do that, in the file /etc/rc.local just before the last line add the following pair of lines for each relay connection coming out of the Pi GPIO <u><em>Pin</em></u> (of course substitue <u><em>Pin</em></u>, for what ever pin number you use). This will put the GPIO pins (and hence the relays) into a default position (1 being non-active) on boot-up, without having to open the web browser.<br>/usr/local/bin/gpio write <u><em>Pin</em></u> 1<br>/usr/local/bin/gpio mode <em><u>Pin</u></em> out</p><p>For 8 relays, this will add 16 lines (or use a bash the &quot;for&quot; loop).</p>
<p>Not reliable! In step #7, The White wire (if you choose white) should go between the relay board pin #10 (VCC) and the Raspberry Pi pin #2 (or #4), <strong>not</strong> pin #1.</p><p>Those relay boards opperate at 5 Volts, and need the voltage from the Raspberry Pi 5 volt rail (pins #2, #4).</p><p>As wired it might work if you got really good relays. But if you turned them all on it would cause a nasty strain on the 3.3 Volt rail. The 3.3V should only be for logic signals and reference it should not be used as a power supply for the relays. This could burn out the 3.3 Volt rail.</p><p>Nice wiring; clean, neat, crisp!</p>
<p>Thanks for confirming my change makes sense. </p>
<p>Great job!</p>
<p>quite honestly, something like this: <br><a href="http://www.ebay.com/itm/SPI-breakout-board-with-6-relays-for-AVR-arduino-PIC-Raspberry-Pi-/160927333012?hash=item2578043a94:m:mRy5ta6aiLDQduMM5r8g6sQ" rel="nofollow">http://www.ebay.com/itm/SPI-breakout-board-with-6-...</a></p><p>or this:</p><p><a href="http://www.ebay.com/itm/STEDO08H-SPI-Serial-board-module-8-Relays-12V-for-Arduino-Raspberry-Pi-/262087264951?hash=item3d059e4ab7:g:7lwAAOSwBLlVep6x" rel="nofollow">http://www.ebay.com/itm/STEDO08H-SPI-Serial-board-...</a></p><p>would be vastly superior due to not blocking all those GPIO ports with 1:1 driving. Plus since its SPI, you could drive multiple boards with using the now freed up GPIO as board selector pins.</p><p></p>
<p>It's apples and oranges. If the Raspi is dedicated to this box only, using all the IO is fine. If you want lots of extra I/O , using SPI, I2C or 1-Wire bus if great. Do an instructable demonstrating your way.</p>
<p>I made this, but cannot figure out how to switch the relays from active-low to normal. How do I do that?</p>
<p>Search the code... somewhere in there is will say &quot;gpio write&quot;.</p><p>The full line will be something like &quot;gpio write <em>xyz</em> 0&quot;, or 1.</p><p>In such locations, change all 0's to 9's, then change the 1's to 0's, and then go back and change the 9's to 1's. You will then have chaged the code from active high, to active low.</p>
<p>Hi</p><p>To fix the problem, you need to use transistors. Take a look on this link: </p><p><a href="https://docs.google.com/file/d/0B5-HND9HJkXWSTQtYlFTZ3VyODA/edit?usp=sharing" rel="nofollow">https://docs.google.com/file/d/0B5-HND9HJkXWSTQtYlFTZ3VyODA/edit?usp=sharing</a></p><p>I followed the instructions and it works fine for me.</p><p>Best regards<br></p>
<p>And you do this to change the relays from active low to active high? why?!</p><p>Why pay money and sapce for transistors?</p><p>Assuming you use pin 8 to control the relay...</p><p>Instead of turning the relay on with the active high logic of &quot;gpio write 8 1&quot;...</p><p>Switch to active low logic of &quot;gpio write 8 0&quot; to activate the relay, and to deactivate the relay use &quot;gpio write 8 1&quot;.</p>
<p>I know instructables is not just about solutions but also about fun making it yourself. Just in case if someone want a quick easy device we are using this 100$ 8 port device which is working in our office flawless from years. </p><p><a href="http://www.digital-loggers.com/lpc.html" rel="nofollow">http://www.digital-loggers.com/lpc.html</a></p>
<p>For IFTTT and advanced timer controls these products are great too: <a href="http://www.wemo.com/products.html" rel="nofollow">http://www.wemo.com/products.html</a></p><p>The web servers are available on the internet anywhere without port forwarding on your router. So now I can close my garage door from work when I forget..</p>
<p>Hey, you should get on &quot;Shark Tank&quot;...with this...LOL</p>
<p>How I know that my device is ON / OFF when I am at long distance ... Any acknowledgement ? </p>
<p>You could run a voltage divider from the controlled circuit to ground and sense the voltage with an I/O line. Probably want an in-line diode and a small cap - since you're sensing A/C (and you don't want negative voltages fed to the Pi - and the cap would let you select a reasonable RC time constant). Note: This involves running a circuit from hot to ground that relies on resistors NOT shorting out.</p><p>A &quot;better&quot; way might be to wind a small coil around the power line and read induced voltage. This has the advantage of keeping control and power lines isolated. However, it will be dependent on current flow.</p><p>There are better solutions. This might help: <a href="http://www.ti.com/lit/an/slyt393/slyt393.pdf" rel="nofollow"> http://www.ti.com/lit/an/slyt393/slyt393.pdf</a></p><p>or at least get you started. </p><p>Some more discussion here: http://electronics.stackexchange.com/questions/72672/using-an-opto-isolator-to-detect-120vac-on-a-microcontroller</p>
Yes, when you visit the web-page served by your raspberry pi, you get to see if the devices are on or off just. (Red for OFF, green for ON)
<p>Doesn't that only provide information on the COMMANDED output? That is, the Pi may be sending a signal to the relay, but there's no guarantee the relay has operated and power is actually flowing through the controlled circuit - right? To do that, you'd want some sort of sensor on the line itself which would report to the Pi which would display the status.</p>
And if any one device is not starting then how we get the feedback that it is not starting ?
Thanks for the great instructable. I had a lot of fun making this to work our Christmas lights. The only problem I'm having is that everything turns on when I start it even though the power buttons show off (red). I have to turn each one to green (which should be on) to turn the switch off. It's just backwards. It all worked fine when I just used LEDs as in the instructable you based this off. Any ideas why it's working the opposite with the relay board?
<p>The issue is that in the examples / pictures in the article the &quot;wrong&quot; relay board receptacles are used. Each relay has 3 receptacles, left one middle one and right one.</p><p>In the pictures the left and middle receptacles are used, if one uses de right one and the middle receptacles the board / Raspberry will behave exactly like the website (green for on red for off)</p><p>I personaly find this safer and more practical as by default now all outlets are in the off position by default. <br>But there is no right or wrong here.</p><p>You can check this yourself by REMOVING THE POWER to the relays / outlets (really important) only power your raspberry and use a multimeter and check the receptacles with the continuity test. (If your multi-meter doen't have a continuity test, just put it on one of the resistance settings.)<br><br>Did I mention to remove the main power first :D ?<br><br>By the way Thanks <a href="https://www.instructables.com/member/rleddington/" rel="nofollow">rleddington </a>for writing this article I found it verry usefull !<a href="https://www.instructables.com/member/rleddington/" rel="nofollow"><br></a></p>
<p>I am looking at purchasing one of these relays and don't understand the schematics but im sure from what you are saying you can answer my question. I am wanting to wire up 2 outlets to one relay where when the gpio is set to high one outlet is on and when it is set to low an opposing outlet is on. Is this possible by powering the &quot;middle&quot; pin and having outlet 1 connected to the &quot;left&quot; and outlet 2 connected to the &quot;right&quot;? Im looking to hook up 2 lights to this and want them to never both be on at the same time but instead alternate with each other.</p>
<p>Yep, in that case you would hook up one outlet to the left side of the relay and the other to the right side of the relay. Should do exactly what you want, when one is on, the other would be off. Controlled from a single output pin.</p>
I just edited the code and basically switched high and low output and everything works great. Thanks again!

About This Instructable




More by rleddington:Web Controlled 8-Channel Powerstrip Magic POV Wand (yet another POV toy) 
Add instructable to: