Update 1/14/2014: Thanks to everyone who voted for this project in the Hardware Hacking Contest! Looking forward to my new maker pen.
Update 12/31/2013: looking for a more advanced version of this project? Check out my new multi-channel voice-controlled version.
We all know you could just go BUY an electrical outlet timer instead of building your own, but whereâs the fun in that? This project will show you how to hack a $10 wireless outlet adapter so you can control it with a Raspberry Pi and a simple Python script, with the motivation of controlling Christmas lights â but it will work for any device you can plug into an electrical outlet.
I should acknowledge right away that this is by no means the first (nor the last) project that will discuss home automation or controlling electrical outlets with a Raspberry Pi. My goal is for this to be a very beginner-friendly project â the code will run locally on your Raspberry Pi (no internet control), the circuit will only control one outlet, and you wonât be touching any high-voltage wires. If youâre ready to tackle something more advanced, check out these Instructables on web-enabled multiple outlet control from hacking a multi-channel remote or building your own power strip. This project will still require use of a multimeter and a soldering iron, so it does require some experience with electronics, or someone who can help.
I also hope to provide some helpful pointers for Raspberry Pi newbies (this is my first RPi project, and Iâm more familiar with Arduino, so I hit a few bumps getting started), but please note that this is NOT a âGetting Started with Raspberry Piâ tutorial â Iâd recommend their official Quick Start Guide and this wiki resource for that, and I will assume you already have a Raspberry Pi up and running with Raspbian.
Now, on to a materials list. Disclaimer: you may notice that Iâm partial to Amazon and Sparkfun, but of course you can shop around and try other vendors like Adafruit, Radio Shack, Ebay, etc. Costs are approximate as of December 2013.
Materials
- Raspberry Pi Model B ($40)
- Monitor or TV and appropriate cable. I have an old Acer Model AL1717 B VGA/DVI compatible monitor ($50 at a recycling center) and an Amazon Basics HDMI to DVI adapter ($8) cable.
- 5V micro-USB power supply. I have this one ($6).
- USB keyboard and mouse. I have a Logitech K400 wireless keyboard/mouse combo ($35).
- SD card, at least 4GB, with an operating system installed. I have an 8GB card ($9) and I followed directions to install the raw image of Raspbian, available here.
- Ethernet cable or USB wi-fi adapter. I have an ethernet cable plugged straight into my router. Note that the Raspberry Pi only has two USB ports, so you will need a USB hub if you use a mouse/keyboard combo that requires two ports, and also want wi-fi.
- Circuit components
- Two single-pole, double-throw (SPDT) relays, available from SparkFun ($2 each).
- Two N-channel MOSFETs, available from SparkFun ($1 each)
- Male-female jumper cables. 10-pack available from SparkFun ($4). These cables have a female end which allows you to connect to the Raspberry Piâs (male) GPIO (General Purpose Input/Output) pins, and a male end that can be pushed into a solderless breadboard.
- Male-male jumper cables or 22 AWG solid-core hookup wire for making breadboard connections. I prefer using hookup wire because you can cut the wires to a shorter length and your circuit is less messy, but either will work. You will need about 15 connections for the project, so a single 10-pack of wires is not enough. SparkFun has a variety of jumper wires available, like premium wire ($4 for 10-pack), or standard wire ($5 for 30-pack). They also have a variety of colors of solid-core hookup wire ($3 per spool).
- Solderless breadboard. I have this one from SparkFun ($5) but there are tons of breadboard options out there. You could also use perf board if you know what youâre doing and want to solder a permanent circuit.
- Hardware
- Wireless outlet adapter with remote control. I used a âUtilitech Outdoor Wireless Outletâ, part number 0357410, that I bought in-person at a Loweâs ($10). Unfortunately I am having a hard time finding a link to that exact product anywhere online. You should be able to follow my procedure using any single-channel remote with separate ON and OFF buttons (you could adapt the instructions to work for a remote with a single toggle on/off button, or multiple channels, but I wonât provide those directions). For example, Â this one from Amazon.
- Christmas lights of your choice (price varies). Remember to pay attention to the labels about how many strands you can plug into a single outlet.
- Tools
- Multimeter with continuity-test function. I have this one from SparkFun ($15).
- Not mandatory, but I love banana to alligator clip adapter cables ($4). These let you clip your multimeter on to stuff and leave your hands free to work, as opposed to using probe tips. You can also get these probe-tip-to-alligator-clip adapters from Radio Shack ($3).
- Soldering iron and solder. I have this variable-temperature iron from SparkFun ($45) and use their lead-free solder ($8).
- Wire strippers â only needed if youâre using hookup wire instead of jumper cables. These are available all over the place, but for the sake of consistency hereâs a SparkFun pair ($5).
- Screwdriver and/or utility knife. Type will depend on the wireless remote for your outlet. Some have a screw to remove the back cover (mine had a small Phillips head), but others are sealed and you will need to crack them open.
Total Cost
The total cost of this project will vary quite a bit depending on what you already have available. Here are rough totals for the sub-sections above:
Raspberry Pi: $150
Circuit: $23
Hardware: $20
Tools: $77
So, if you already have a Raspberry Pi setup, an electronics workbench with a soldering iron, spare breadboards and jumper wires etc., and some Christmas lights, this project will be pretty cheap (probably under $20). If youâre literally buying everything brand-new, itâs nearly $300. But of course, all of those tools will be good investments for other projects in the future â hopefully you wouldnât buy them just for this!
Now, on to the next step to get started cracking open and hacking your wireless remote.
Step 1: Open the Remote Case and Remove the Circuit Board
First youâll need to open up your wireless outlet and crack open the remote. Here are the steps I followed â you may have to improvise if you couldnât find the exact same model remote. Your goal is just to get the circuit board out intact. Itâs OK if you have to break some of the plastic case to do this.
1. Remove the battery cover and battery.
2. Pry off the little keychain attachment.
3. Remove screw holding on the back cover.
4. Pop off the back cover.
5. Remove the circuit board.
Step 2: Reverse-engineer the circuit: Part 1
This is probably the most difficult (and possibly intimidating) part of the project â figuring out how the circuit works. Donât feel overwhelmed though â you DONâT need to understand what every single component in the circuit does. Your goal is just to figure out what the on and off buttons do, so you can use the Raspberry Pi to âtrickâ the remote into thinking those buttons are being pressed.
So, whatâs going on when we look at the circuit board? The first two images, showing side-by-side pictures of both sides of the board, give you a rough idea of the things we care about. Thereâs a battery, which has positive and negative terminals, connected to the board. There are two pushbutton switches, labeled SW1 and SW2 on the circuit board, corresponding to the ON and OFF buttons on the exterior plastic case, respectively. Each one of these switches has four pins connected to the circuit board, labeled in the second image above. So, we want to figure out what these buttons do â a good way to start is to figure out what the voltages at these pins are when the buttons are pushed.
This is where your multimeter will come in handy. If you need help using a multimeter, check out this tutorial from SparkFun or this one from Science Buddies (full disclosure: I work for Science Buddies and I wrote that tutorial). Youâll want to attach your multimeterâs black probe to the negative battery terminal (this is where I find those alligator clips very handy). Then, use the red probe to measure the voltage at EACH pin when:
- Nothing is pressed
- The ON button is pressed (SW1)
- The OFF button is pressed (SW2)
The last three pictures above show the results of my measurements. It looks like, when nothing is pressed, all of the pins sit at either 12V or 0V. However, when either button is pressed, some of the 0V pins go up to 4.5V (and some of the 12V pins also go down to 4.5V). What does that tell us? It means that the remote is probably using digital logic, where a 5V signal is considered âhighâ (or TRUE) and a 0V signal is considered âlowâ (or FALSE). If you arenât familiar with digital logic, check out this SparkFun resource on logic levels or the Wikipedia article.
So, now we know how the voltages of the button pins change when they are pushed. The next step is to figure out which of those pins are connected to inputs on the chip â the big black rectangle that acts like the âbrainsâ of the circuit.
Step 3: Reverse-engineer the circuit: Part 2
Next you need to analyze the chipâs connection to the circuit. The chip in my remote is in a dual in-line package (DIP), meaning it has two parallel rows of pins. The first image above highlights all of these pins on the bottom of the board using yellow circles.
Our goal is to figure out which of these pins are connected to the pins on the buttons we analyzed in the previous step. You can guess at this by looking at the lighter-green traces on the circuit board. The second image above highlights these in magenta and orange â it looks like we can trace two different pins (one from the ON button and one from the OFF button) to pins on the chip.
It probably isnât a good idea just to eyeball this, however. You need to use a multimeter in continuity-test mode to check and make sure that these pins are actually electrically connected. So, do that â set your multimeter to continuity test, and use the probe tips to simultaneously poke two different pins (bounded by the orange or magenta polygons above, respectively). If the pins are electrically connected, you should hear a loud BEEP from your multimeter.
Now, letâs cross-reference this with our analysis of the pin voltages (on the buttons) in the previous step, to find out what happens to the pin voltages on the chip when the buttons are pressed. The last three images above show this. It looks like both pins sit at 0V by default. When the ON button is pressed, one of them goes up to 4.5V. When the OFF button is pressed, the other one goes up to 4.5V. This confirms our suspicion that the circuit probably has a chip operating at 5V logic levels. Now we can move on to building a circuit that will trick the remote into thinking the buttons are being pressed, by sending 5V signals to these pins.
Step 4: Solder Jumper Wires to the Remote
Youâll need to solder three jumper wires to the remote: one to each of the pins identified in the previous step, and one to the pins connected to the batteryâs negative terminal (so you can make sure the remote has a common ground connection with the circuit youâll build).
Make sure you remove the battery before soldering (excessive heat can make batteries explode), and if youâre cutting your own jumper wires, make sure theyâre long enough (a few inches) to reach a nearby breadboard.
Odds are if youâve gotten this far you already know how to solder (or can get help from someone who does), but hereâs the SparkFun soldering tutorial just in case.
Step 5: Build the Circuit
In this step youâll assemble the circuit that will connect your newly-hacked remote to your Raspberry Pi. Follow the steps below (corresponding to the diagrams above) to build the circuit. To learn how the circuit actually works, go on to the next step.
1. Line up your Raspberry Pi, breadboard, and remote left to right as shown.
2. Populate your breadboard with the two MOSFETs and two relays. Pay attention to the orientation based on the pins of the relays (one side has three pins, the other side only has two), and the big âtabsâ on the back of the MOSFETs (facing to the left in my diagrams).
3. Use jumper cables to make connections on the breadboard as shown in the second diagram.
4. Use jumper cables to make external connections to the remote and the Raspberry Pi, as shown in the third diagram. A couple notes:
- Iâve only shown the pins on the remote that you need to connect to, rather than drawing the whole circuit. You already soldered jumper wires to these pins in Step 4, so you should be able to plop their other ends right into the breadboard.
- This is where the Raspberry Piâs wildly non-intuitive pin numbering scheme can be quite confusing for a new user, especially if youâre used to an Arduino. I wonât go into details here â this page has a good explanation if you scroll down to âA word about GPIO pin numberingsâŠâ. Iâve provided a simplified diagram that highlights the pins you need to use for this project, which will hopefully be less confusing than the diagram on their Wiki initially was for me. Point being, you need to connect to +5V (P1-02), GND (P1-06), GPIO 17 (P1-11) and GPIO 18 (P1-12), as shown.
- The colors I used in my diagrams donât match photos of my physical circuit perfectly, because I only had red and black hookup wire. You can use whatever color scheme you want, as long as you get the connections right.
For more detail: Wireless Christmas Light Timer with Raspberry Pi and Python