Introduction: Cheap and Effective Home Automation With Raspberry Pi

About: I am a 15 year old that likes using Raspberry Pi and Arduino for just about everything, from iOS HomeKit enabled RF sockets, to robots, to Nintendo-style gaming.

I've always wanted to be able to control lights wirelessly, but commercial options are usually expensive. Philips Hue lights cost around $70, and WiFi connected lights are expensive too. This tutorial will tell you how to control up to five lights/lamps/Christmas light strands for around $50, and can be expanded to include more lights too.

Step 1: What You Will Need


Raspberry Pi Zero W (Wireless edition)

Micro SD card (At least 4gb)

Mini HDMI to HDMI cable/converter (There is a difference between mini and micro, make sure to get the right one!)

Micro USB to USB cable/converter

USB Keyboard

Note: I got a Raspberry Pi Zero W starter kit from Amazon that came with Raspberry Pi Zero W, SD card, case, power cable, HDMI converter, and USB converter. For only $30, it is a pretty good option (link)

You can use a different model of Raspberry Pi, but the Raspberry Pi must have some form of internet connection, and the Pi Zero W is the cheapest option.

Remote Outlets - I used these

RF Transmitter and receiver (Note: the frequency of the transmitter/receiver must match the frequency of the remote outlets) - I used these

Wire for antenna (I used 22 gauge solid core copper wire, other wire may work)

Breadboard, Jumper wires, 40 pin male-to-male header


Raspberry Pi OS (Download Raspbian Desktop)

Etcher disk imager (For writing the Pi OS to the SD Card)


Soldering Iron

Computer for downloading Pi OS and Etcher

Step 2: Set Up the Outlets

Begin by plugging your outlets into a socket. Follow the included instructions to set them up, and plug in your lights/appliances. Make sure your remote is paired correctly and is controlling the sockets.

Step 3: Set Up a PubNub Account

PubNub is a free internet server that allows you to communicate between devices, anywhere in the world. The python script receives messages from a webpage, and uses the information in the message to turn lights on or off. You can sign up for your free account here. Once you have signed up, log in to your account. This will bring you to an app management page. Click on "Create new app". Give you app a name, then select it from the list of apps (If this is your first app, it will be the only one) This will bring you to the app keys page. Click on the "Demo Keyset", and you should see three different keys. Keep this page up, you will need these keys later. Now that you have your PubNub account, you can set up your raspberry pi.

Step 4: Prepare Raspberry Pi

Download Etcher and the Raspberry Pi OS onto your computer (links in parts list)

Install Etcher onto your computer, and unzip (if necessary) the Raspberry Pi OS. Insert your Micro SD card into your computer, and open up Etcher. Click "Select Image" and navigate to the unzipped Raspberry Pi OS file. Select it, then click "Select Drive" and select your micro SD card from the list. Click "Flash", and wait for Etcher to complete. Once Etcher has completed, remove your SD card from your computer (Etcher automatically ejects it) and insert it into your Raspberry Pi. At this point, you will need to connect your Raspberry Pi to a monitor with the HDMI converter. Plug in a keyboard (and mouse if you have a multi-USB hub) and plug in the power cable. There are two micro USB ports. One is for power, the other is an actual USB port. The one closest to the end of the Pi is power, while the one closer to the middle is USB. Your raspberry pi will now start to boot. It may restart a few times during the first boot, but that is normal. Once your raspberry pi is started, connect it to your WiFi. After connecting it to WiFi, you may use an SSH server, but I find it easier to use the GUI for now. Open up a terminal window and enter these commands:

sudo pip3 install 'pubnub==3.9.0'
git clone git://
git clone
sudo git clone /var/www/rfoutlet
sudo chown root.root /var/www/rfoutlet/codesend
sudo chmod 4755 /var/www/rfoutlet/codesend
cd wiringPi

Now, open up the Raspberry Pi file browser, and you should see the pi-home-automation folder. Double click it, and you should see a file called Copy it (opposite click and select "Copy"), then return to the pi folder and paste it there.

Shut down your raspberry pi (Either from the menu or by running sudo poweroff from the command line), and unplug it once the green light stops flashing.

Step 5: Creating the Circuit

Now it is time to create the circuit. I used a breadboard, but you can do it however you like.

First, we must create a antenna for the RF transmitter. This instructables tutorial tells how to make an antenna for an RF transmitter. Follow the instructions, then solder it to the transmitter's antenna pin (On the one I used it was labeled ANT) with the shorter side closest to the transmitter

Wire the transmitter

The transmitter uses these pins:

DATA: GPIO 17, Physical pin 11

VCC: Raspberry Pi 5v, Physical pin 2

GND: Raspberry Pi GND, Physical pin 6

Wire the receiver

The receiver uses these pins:

GND: Raspberry Pi GND, Physical pin 9

VCC: Raspberry Pi 5v, Physical pin 4

DATA: GPIO 27, Physical pin 13 (There are two data pins on the receiver I used, and I used the one closest to VCC)

Step 6: Get Your RF Codes

After your circuit is attached to your Raspberry Pi, check it twice to make sure everything is in place and then power it on again. Once it is powered on, open a terminal and type

sudo /var/www/rfoutlet/RFSniffer

Next, open up the RFControl program. Putting the terminal window and program window side-by-side may be helpful in the next step.

You will need the remote that was included in the outlet kit now. Hold it close to the receiver, and press each individual button. When you press a button, you will see two numbers printed to the screen. One of them will be a six or seven digit code, and the other a 3 digit code. You will need to edit the codes dictionary in the program. Enter the 7 digit code into the corresponding switch's place in the program, and for each on/off button, insert the 3-digit code (this is the length) into the corresponding switch's len in the program.

For example, if switch 1 on has a code of 1234567, an off code of 7654321, and a length of 890, the code would look like this:

codes = {
	'switch1': {
		'on': 1234567, #This is switch 1 on
		'off': 7654321, #this is switch 1 off
		'len': 890, #this is switch 1 code length
	'switch2': {...},



Repeat this for each button. While you are editing this file, change the strings that say "YOUR-SUB-KEY" to your PubNub subscribe key, and "YOUR-PUB-KEY" to your publish key.

Once each button has been recorded, you can continue on to the next section.

Step 7: Running the Program

Now that you have all your codes, go to this GitHub page (On your computer, not raspberry pi) and click on the green button that says "Clone or download", then click "Download ZIP". Once this file is downloaded, unzip it and open the folder. Open up the file named main.js. You will need to enter your subscribe and publish keys in the program by replacing "YOUR-PUB-KEY" with your publish key and "YOUR-SUB-KEY" with your subscribe key.

Next, go back to your Raspberry Pi and enter


Once it prints "Ready", go back to your computer and open the index.html file in a web browser by doing an opposite click, select "Open with", and select your preferred browser. You should see 2 columns of 5 buttons, with the names of the switches between them. Press any of the buttons. If it correctly controls the socket, great! If not, here are some things to try:

  1. Make sure the Raspberry Pi is receiving messages. It should print to the screen whenever it receives a message from the webpage. If not, make sure your subscribe/publish keys are correct.
  2. If the Raspberry Pi is receiving messages, make sure your RF codes are correct. Make sure your outlets are paired with your remote, your remote can control them, and that the codes you entered into the program are correct.
  3. If the RF codes are correct, make sure your Raspberry Pi is in range of the outlets. If they begin to work after moving it closer, you may need to re-do your antenna.
  4. If they still aren't working, please tell me your problem in the comments and I will try and help with it.

Step 8: Running at Startup

To make your python program run at start up, enter this into your raspberry pi terminal:

crontab -e

Next, go to the bottom of the file and enter this:

@reboot python3 /home/pi/ &

This will make your Python script run at startup each time. Now reboot your Raspberry Pi.

Step 9: Adding More Switches

If you would like to add more outlets, follow these instructions:

Open on your Raspberry Pi, and add a new switch to the switches dictionary.


switches = {

	...other switches...


		'on': xxxxxxx,

		'off': yyyyyyy,

		'len': zzz,


Next, add a new button in your web app by editing the switches variable in main.js on your computer.


var switches = [
	...old switches...

Replace NAME-TO-DISPLAY with the name you would like it to show on the webpage, and NAME-OF-SWITCH on both files with anything you like. It must be unique from the others though!

Step 10: Taking It Further

PubNub is supported on many different platforms, which means you can send messages from almost any device! I used a Raspberry Pi AIY Voice kit and PubNub to send commands with my voice, but that may be the topic of a later Instructable. Thank you for reading my Instructable, I hope it was helpful!

Step 11: Credits

Thanks to Tim Leland for the tutorial on setting up the RF transmitter: tutorial

Thanks to diy_bloke for the tutorial on creating an antenna: tutorial

Thanks also to any others that posted tutorials online that I viewed for help!

Remote Control Contest 2017

Participated in the
Remote Control Contest 2017

Raspberry Pi Contest 2017

Participated in the
Raspberry Pi Contest 2017