Can You Use Raspberry Pi For Home Automation?By - 09/26/2018
A few months ago, I started a little side project that involved Raspberry Pi, a wonder machine for many DIYers. I had one goal: To determine if it was a good idea to use Raspberry Pi to create a DIY smart home.
What Is Raspberry Pi?
Simply put, Raspberry Pi is a mini-computer. The one I have (Raspberry Pi 3 Model B) is only as large as a deck of cards. Of course, that’s just the CPU. You’ll have to plug in a mouse, keyboard, and monitor to use the mini-computer or if you don’t have any of those things handy, some programs let you access Raspberry Pi through a Windows computer. Also, Raspberry Pis don’t include built-in flash memory. Instead, you’ll need to load the necessary operating system (OS) onto a MicroSD card and use it as Raspberry Pi’s internal memory source.
Choosing A Raspberry Pi Model
There are several Raspberry Pi models, but not all are suited for smart home projects.
Raspberry Pi Zero
Raspberry Pi Zero (base model, Zero W, and Zero WH) is cheap and only about half the size of the full-sized Raspberry Pi. However, it lacks USB ports and only has two ports for Micro USB and one port for Mini HDMI. That said, you need additional hardware such as an OTG USB hub (to connect your mouse, keyboard, etc.) and a Mini HDMI to HDMI adapter (to connect your display).
Of the three Raspberry Pi Zero models, I recommend Zero W and Zero WH because these two models are Wi-Fi and Bluetooth-ready. The base model requires a Wi-Fi dongle and a separate Bluetooth dongle to connect to the internet or Bluetooth devices.
|Raspberry Pi Zero (Not Recommended)||Raspberry Pi Zero W||Raspberry Pi Zero WH|
|Price||Starts at $5.00||Starts at $10.00||Starts at $15.00|
|CPU||1 GHz Single-Core||1 GHz Single-Core||1 GHz Single-Core|
|RAM||512 MB||512 MB||512 MB|
|USB Ports||N/A (2 Micro USB Ports)||N/A (2 Micro USB Ports)||N/A (2 Micro USB Ports)|
|HDMI Ports||N/A (1 Mini-HDMI Port)||N/A (1 Mini-HDMI Port)||N/A (1 Mini-HDMI Port)|
|GPIO Pins/Headers||40 Pins||40 Pins||40 Pins|
|Where To Buy||Cana Kit||Amazon||Amazon|
(Pro Tip: The prices stated on the chart are the price of the board only. Some sellers offer kits that may include a hard case for the board, a pre-loaded SD card, and other modules to help you get started. If you already have a specific project in mind, browse through the available kits to see your options.)
Standard Raspberry Pi
If you want the standard, full-sized Raspberry Pi, it also comes in several models. There’s the first generation Raspberry Pi (Model A+ and B), second generation Raspberry Pi 2 Model B, and the latest generation Raspberry Pi 3 (Model B and B+). Compared to Raspberry Pi Zero, standard Raspberry Pi boards have better CPUs, more RAM, and more ports.
Of the five standard Raspberry Pi models, I recommend using Raspberry Pi 3 Model B or Model B+. Both are Wi-Fi and Bluetooth-ready with one Ethernet port (Model B+ has a Gigabit Ethernet port) and four USB 2.0 ports each. The difference between the two models is that Model B+ supports Power over Ethernet if you add a PoE module. PoE is something to be excited about, especially if you’re building a custom smart home or security device, as it has a reputation for being fast and reliable in transferring data. And because the prices of Model B and Model B+ are almost the same, I say go with Model B+.
|Raspberry Pi 1 Model A+ (Not Recommended)||Raspberry Pi 1 Model B+ (Not Recommended)||Raspberry Pi 2 Model B (Good)||Raspberry Pi 3 Model B (Better)||Raspberry Pi 3 Model B+ (Best)|
|Price||Starts at $20.00||Starts at $25.00||Starts at $35.00||Starts at $35.00||Starts at $35.00|
|CPU||700 MHz Single-Core||700 MHz Single-Core||900 MHz Quad-Core||1.2 GHz Quad-Core||1.4 GHz Quad-Core|
|RAM||256 MB||512 MB||1 GB||1 GB DDR2||1 GB DDR2|
How To Automate Your Home Using Raspberry Pi
What appeals most to DIYers about Raspberry Pi is the fact that it’s customizable and modular. It’s a computer, so when used for building a smart home system, you can program Raspberry Pi to have all of the features you need. Of course, programming it is not an easy process. You need a certain degree of software and hardware knowledge to execute your design, or you can spend hours researching similar Raspberry Pi projects for inspiration.
I decided to do something simple. I know my way around electronics, but I’m new to software development. I based my project on this JackkTutorial’s Instructables post. My goal was to be able to turn a lamp on and off using a remote browser.
Below is a list of materials I used to complete the project with links to buy where applicable.
- Raspberry Pi 3 Model B (Buy on Amazon.com)
- 32 GB MicroSD Card or higher (Buy on Amazon.com)
- 5V Relay Switch 1, 2, 4, 8 or 16 Channels (Amazon.com)
- Desk Lamp
- Screw Drivers
- Wire Stripper
- Needle-Nose Pliers
- Soldering Iron
- Other Common Tools
This isn’t an instructional article, so I won’t go into extreme detail. I will, however, explain some of the key points of the project.
The All-Important Feature: GPIO
I started by preparing the hardware. For this part, Raspberry Pi’s GPIO is the most crucial component. What is GPIO? It stands for General Purpose Input/Output. They are pins or headers found along the edge of each Raspberry Pi where you can connect Arduino sensors, components, and modules. Using software, you can configure whether each pin is for digital input (receiving data from an attached module) or output (sending data to an attached module).
Each Raspberry Pi has 40 pins, but not all of them are GPIO. Two of them output 5v of electricity, two more output up to 3.3v, and eight of them are for ground. These electrical pins are used to power modules that require voltage, such as the relay switch I used.
(Pro Tip: You can check which pin is which by opening a terminal window on your Raspberry Pi and running the command [pinout]. Doing so will show you which pins are GPIO, 5v, 3.3v, or ground.)
GPIO is all-important because it makes the mini-computer modular. For example, if you want your Raspberry Pi to give you the current ambient temperature, you can connect an Arduino temperature sensor. The sensor will then report its reading to the Raspberry Pi through a GPIO pin set as input. As another example, you can connect an actuator, such as a relay switch so your Raspberry Pi can tell it when to open or close (output). There are a bunch of components that you can hook up to Raspberry Pi’s GPIO pins, such as sensors (motion, PIR, humidity, temperature, gas), navigation modules (GPS, gyroscope), wireless modules (RF, IR, Bluetooth), servo motors, touchscreen interfaces, and more.
For my project, I used three wires to connect the relay switch to Raspberry Pi. The first wire connects the 5v output pin (Raspberry Pi) to the VCC pin (relay switch). This connection powers the relay switch. The second wire connects a GPIO pin set as output (Raspberry Pi) to IN1 (relay switch). This is where the signal that dictates whether to open or close the relay switch passes through. If you’re using a 2, 4, 8, or 16-channel relay switch, you can connect the wire to any of the IN pins. Just be sure to use the correct corresponding channel (e.g., IN1 for CH1, IN2 for CH2, etc.) The last wire connects a ground pin on the Raspberry Pi to the ground pin on the relay switch.
Besides wiring the relay switch to the Raspberry Pi, I also wired the lamp to the relay switch. I cut the lamp’s power cable in half, displaying two wires: an insulated live wire (brown) and a neutral wire (blue). I reconnected the two neutral wires using a soldering iron. As for the live wires, I connected them to the relay switch as shown in the image below. Bear in mind that this step involves messing with wires that will need to be plugged directly into an outlet. You have to be very careful. Do not attempt to do this if you lack electrical experience.
After setting up the hardware, the next part of the project is creating the software. But first, you need the right operating software for your Raspberry Pi. For this project, the official Raspbian OS works well. You can download the OS and learn how to install it here.
Once your Raspberry Pi has booted using the Raspbian OS, you can start creating your software and user interface. Because I’m a noob in coding and creating software, I simply followed JackkTutorial’s Instructables post.
Create A Raspberry Pi-Hosted Web GUI
The first thing you need is a Web GUI. A simple one with large On/Off buttons will do for a simple setup such as mine. If you’re reading this, I assume you already know a thing or two about using HTML and PHP. If you’re unfamiliar with the two languages, this video can help you build a simple Web GUI using Dreamweaver.
Once the Web GUI is done, there’s still more work to do. If you try to press on the On/Off buttons, you’ll notice that nothing happens. The next step is to link the buttons to the command that sends the signal to the relay switch. To do that, you need to host the Web GUI on your Raspberry Pi. The process is quite long, and I’m not an expert, so it’s best that you learn from an expert. This YouTube video by Tinkernut will help you out. By the end of this step, you should have a website (hosted by Raspberry Pi) viewable from another computer or browser.
Linking The Buttons
And now the final step: Creating the Python script that sends the on/off signal to the GPIO that controls the relay switch.
I copied and used JackkTutorial’s script, which worked perfectly.
To turn on the lamp:
import RPi.GPIO as GPIO
To turn off the lamp:
import RPi.GPIO as GPIO
I saved both scripts as “lampon.py” and “lampoff.py” respectively. I then created two PHP files (lampon.php and lampoff.php) within the Web GUI directory that executes the Python script when opened. For example, if I open lampon.php, lampon.py will execute to turn on the lamp.
Finally, I linked the On button on the Web GUI to the lampon.php page using HTML. I also linked the Off button to the lampoff.php page. Because the Web GUI is a hosted webpage, I can access it using any browser wherever I am. Thus, I can turn the lamp on and off anytime and anywhere, provided that Raspberry Pi is switched on and connected to the internet.
Should You Automate Your Home With Raspberry Pi?
Should you? A better question is “Can you automate your home with Raspberry Pi?” Raspberry Pi is a powerful tool, but its power depends on the one who wields it. As I said earlier, you need software and hardware knowledge to be able to create something worthwhile using Raspberry Pi, and even then, we’ve seen major developers fail at the task. You will need an extreme amount of patience should you choose to pursue this option, especially when it comes to building the software. I’d be lying if I told you that everything worked on the first try. I had to start over several times, which was frustrating.
So what do I recommend? First, define your smart home goals. What devices do you want to automate? What features do you want? Which products do you want to integrate? Second, scour the smart home market to see if there’s an existing system that can give you what you want. If there is, figure out the cost. If there isn’t, check if the features you want are achievable using Raspberry Pi. Maybe join a few RPi forums or ask the experts. Third, compute how much it will cost you if you were to build your Raspberry Pi smart home. Don’t forget to factor in the working hours you will need to spend building your project. Next, try to determine which works best for you — a DIY Raspberry Pi smart home or a pre-built system from a smart home company? And lastly, don’t be afraid to try new things.
You can start with a simple project, such as the one I did. And if you enjoy the process, you can eventually build upon it. For instance, you can add more lamps or low-power appliances. You can also give Raspberry Pi an ambient light sensor and then write a new script that turns on your lamps when it’s dark. The possibilities are almost endless.
To get started, check out the available Raspberry Pi kits on Amazon.com, starting at $35.00.