Using Raspberry Pi to control an electrical outlet

Lead Image © Deanna Boldrick,

Socket Hop

Raspberry Pi fills in for a remote control device, switching the power on and off at an electrical outlet.


This article originally appeared in The MagPi magazine:

I'm sure I'm not the only person who jumped at the chance to buy a machine like the Raspberry Pi, then wondered what to do with it. It isn't the first time I've bought something that seemed like a good idea then been stuck for how to use it. A couple of years ago, I noticed a sign on a petrol pump advertising a similarly fascinating device: a remote-controlled power sockets at UK£ 5 for a pack of three (Figure 1). The device plugs into a wall socket, and you can plug an electrical appliance into it. A remote control that comes with the device lets you turn the power to the appliance on or off. The device thus lets you use the remote to control the power to any appliance (such as a lamp or a coffemaker), as you would for a garage door opener or a TV set.

Figure 1: This fascinating device I bought at a gas station lets the user switch an outlet on or off by remote control.

I bought the device and tried it with a few random appliances, stuck it in the cupboard, and forgot about it. When pondering a project for my Raspberry Pi, I hit upon the idea of combining these two impulse purchases. Could I get the Raspberry Pi to imitate the behavior of the remote so that I could turn appliances on and off with it?

I made up a lot of this as I went along, and I'll describe the episode chronologically, so you can share my sense of discovery.

Nothing in this project involves going near any dangerous voltages. Soldering is limited to just a few joins, and the source code is available online. The device described in this article is designed for UK power specifications, but you'll find similar products in the US and elsewhere. Even if you don't want to implement the exact same kind of device with your Raspberry Pi, you can think of the following experiment as a case study in the kind of practical problem solving you might need some day for your own Raspberry Pi adventures.

Getting Started

The first step was to identify the signaling used by the socket device so I could mimic it using the Raspberry Pi. Communication between the remote and the sockets is radio based, which allows control without line of sight. This design also allows sockets in various rooms to be controlled from one location. A sticker on the back of the socket device said 433.92MHz, so I searched eBay for "433MHz receiver" to find a suitable receiver for the Raspberry Pi. I wound up with a transmitter and receiver for only UK£ 1.60, including postage and packing (Figure 2). It was shipped from China, so I faced a fairly high probability of the package going missing in the post, but I've always found that professional Far East sellers are happy to send another if an item doesn't arrive.

Figure 2: I found a tiny receiver and transmitter that operate at 433MHz.

I had to solder antenna wires onto both the transmitter and receiver, but it was not a difficult piece of soldering, although the pads were quite small (Figure 3).

Figure 3: Soldering the transmitter to the Raspberry Pi.

To read the code from the remote control, I connected the receiver module to a computer mic socket. I have a 5V power supply lying around, but there's a 5V output on the Raspberry Pi that would serve the purpose. The output from the module is 5V digital, which isn't suitable to be connected straight into a computer audio socket, so I connected the output of the module via a 1Mohm resistor to the mic socket of my laptop, borrowing the cable from my speakers to make the connection. I assembled the circuit on a breadboard, but I'll describe an alternative later.

Audacity [1] is an excellent tool for examining this kind of signal. Once I was satisfied that the recording level was about right, I started recording and pressed one of the buttons on the remote. I then stopped the recording and zoomed in on the signal generated by the button press. The signal repeated over and over until the remote button was released. Short pulses seemed to be about 0.25ms and the long ones three times as long. I converted the transmission to the binary string shown in Figure 4.

Figure 4: Pressing one of the keys on the remote caused a transmission analogous to this binary string.

This step is the make or break time for the project. If you have come this far and cannot find a repeating pattern that appears when you press the remote control button, your sockets might not be using the simple AM signaling that this project relies on.

To recreate the transmission with the Raspberry Pi, I needed to wire the transmitter up, but first, I powered it from the 5V supply and checked the input pin with a voltmeter. On some devices, the supply voltage is present on the input pins when there's little or no load. This could potentially damage the Raspberry Pi, so I wanted to make sure. No voltage was present, so I went ahead and wired it up.

The transmitter is intended to stay connected to the Raspberry PI, so it's powered from the +5V pin of the GPIO. I connected GPIO7 of the Raspberry PI to the data pin, and I connected the GND of the transmitter to the GND of the Raspberry GPIO connector. The 3.3V signals are enough to drive the transmitter, although I only found this out by trying it.

From the waveform in Audacity (Figure 5), I could see what I would need to send, and how fast I would need to send it. Linux is a multitasking OS, and something else could use the CPU at just the wrong time, which would leave my program outputting bits at the wrong time, so I repeat the output 10 times, hoping that at least one gets transmitted correctly. My initial rough cut of the software was based on the first GPIO example in C from the Rasp Pi low-level peripherals page on the site [2].

Figure 5: Wave form for one of the remote buttons, as shown in Audacity.

Optimistically, I plugged the socket device in for the test run. When I ran my software on the Raspberry Pi and captured the signal with the laptop, receiver, and Audacity, I could see that the waveform was about right, but upside down. Needless to say, the socket didn't do anything. I flipped all the bits in my output stream and re-ran the test code. This time the socket switched on, which surprised me a bit, because I expected to have to do a lot more tweaking. The waveform in Audacity matched the one I'd captured earlier pretty well. I went on to transcribe the other buttons in a similar fashion.


So, that's the story; now for a bit more detail on the hardware and software.

The connector for the GPIO and the cable I used is from an old PC floppy disk drive cable. These have a wider cable and plug than the Raspberry Pi GPIO connector, but you can fit the plug onto the GPIO pins with part of the plug going off the end. Of course, this won't work if your Raspberry Pi is in a case. One end of the cable has the ribbon split into three, with the middle of the three twisted over. I used this end of the cable because it left the cable pointing out away from the Raspberry Pi. The other end of my cable would have gone over the top of it.

With the cable this way around, eight wires in the ribbon cable extend off the end of the GPIO connector. I cut these off quite close to the connector and used some of the wire for the antenna wires mentioned earlier. A single wire is connected to the GPIO pins before the first split in the cable; this wire isn't used for the transmitter, though.

The critical pins are shown in Figure 6. The first wire after the split is +5V; I use this wire to power the transmitter. Next come three other wires I don't need, then the GND connection. Finally, right at the bottom of the ribbon is GPIO7, the pin I use to control the transmitter. The +5V and GND lines could also be used to power the receiver while collecting data from the remote control.

Figure 6: Critical GPIO pins.

On the other end of my ribbon cable is a very small piece of stripboard. Soldered to this board is a connector for the transmitter, made by cutting down an IC socket. Another way to connect the transmitter module would be to use the connector from the other end of the floppy drive cable. The transmitter would go into three of the pins. The three wires connecting to the transmitter in that socket could be connected to the +5V, GND, and GPIO7 wires from the connector on the Raspberry Pi.

A chocolate-block-type connector would be fine for connecting the wires because the signals aren't very high speed. If your transmitter has another pin for the antenna connection, you would just need to trim the wire on that pin to the correct length and do no soldering at all.

Buy this article as PDF

Express-Checkout as PDF

Pages: 4

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content

  • Automating the home with the Raspberry Pi

    The Raspberry Pi GPIO interface offers the perfect starting point from which to control devices, such as turning lights on and off, starting the coffee machine in the morning, or turning on the TV and playing your favorite movies at scheduled times.

  • Android Pi

    Your Android device can be a versatile companion for Raspberry Pi. We describe some useful apps to help you make this happen.

  • Raspberry Pi IR remote

    Turn a Raspberry Pi into an IR remote control for your DSLR, TV, or any other device with an IR port.

  • A new way of no-solder prototyping

    The Grove system's standardized connector and multitude of devices allow quick and easy project prototyping with your favorite small-board computers.

  • PHP on Raspberry Pi

    Getting started with PHP on Raspberry Pi is easy. We show how to build a simple PHP app to control an LED.