How to Automate Your Garage Door With IFTTT and Raspberry Pi
Smart home gadgets are cool but can be expensive. With a Raspberry Pi and a component or two, it’s easy and cheap to connect existing devices to the internet.
Making your garage door internet-aware is a great introduction to learning how to control the real world with a Pi. And let’s be honest, who doesn’t want to feel a bit like Batman and have their garage open by itself when they pull into their driveway?
What You’ll Need
To automate your garage door, you will need:
- A garage door motor that can take an external trigger.
- A Raspberry Pi (any model) connected to the internet. This tutorial assumes your Pi is running the Raspbian operating system (if it’s not, see our article on how to install Raspbian on your Raspberry Pi ).
- A relay expansion board, which can find find on Amazon.
- A 2A power supply for the Raspberry Pi. A lower-rated model may have trouble driving the relay board as well as the Pi.
- Four jumper cables (plus one more for each extra door you want to control).
- Enough two-core cable to reach between the Pi and the garage door motor. Cheap speaker wire is ideal, but telephone or Ethernet cable can work too.
Once you’ve collected those components together, it’s time to get started.
How This Automated Garage Door Works
Most garage door motors can be triggered to open or close through an external input. Manufacturers include these inputs so that installers can hook up a simple push button somewhere in the home to open or close the door without using the regular remote. When a trigger button is pushed and released, it momentarily closes a circuit which tells the motor to start or stop.
You’re going use a relay in place of an external button. Closing the relay briefly will close the circuit, exactly as if a trigger button had been pressed. A Python script running on the Raspberry Pi will let you control the relay, and therefore the door, from your home network.
Step 1: Connect the Raspberry Pi to the Relay
You’ll be making at least four connections between your Raspberry Pi and the relay board. If you’re using a Pi Zero you will either need to solder the connections directly, or solder a GPIO header to the Pi and use jumper wires for the connections. The latter option is recommended because if you ever want to disconnect the relay board and use the Raspberry Pi for something else, you won’t have to de-solder your connections. The bigger model Pis already have header pins for connecting push-on jumper cables.
If you’re new to using the GPIO, be sure to read our article Everything You Need to Know About Raspberry Pi GPIO Pins .
Before hooking everything up, check to see if your relay board has a jumper connecting the VCC and JD-VCC pins together. If it has, remove it, because you’ll need to power VCC and JD-VCC separately.
With everything powered down, connect the relay board to your Pi as follows:
- Begin by connecting Pi Pin 2 (5V rail) to the JD-VCC on the relay board.
- Connect Pi Pin 1 or Pin 17 (3.3V rail) to VCC on the relay board.
- Connect Pi Pin 6 (GND) to GND on the relay board.
- Finally, connect Pi Pin 7 (GPIO 4) to IN1 on the relay board. This is the connection that switches the relay.
If you have more than one garage door, or if you want to add control for electric gates, you should add extra connections between IN2, IN3, etc. on the relay board and other free GPIO input/output pins on the Pi.
Step 2: Install Dependencies on the Raspberry Pi
Raspbian comes with Python pre-installed, but you will need to add the GPIO library. Type the following into the terminal window on your Pi:
sudo apt-get update sudo apt-get -y install python-rpi.gpio
Now make a new folder in your home directory, somewhere to put the Python script that’s going to control the relays:
mkdir ~/garagedoor cd ~/garagedoor
Finally, download bottle, a lightweight framework that will create a simple web server on your Pi:
Step 3: Create the Control Script
Here’s a very simple Python script to control the relay board via HTTP:
# Python Script To Control Garage Door # Load libraries import RPi.GPIO as GPIO import time from bottle import route, run, template # Set up the GPIO pins GPIO.setmode(GPIO.BOARD) GPIO.setup(7, GPIO.OUT) GPIO.setup(11, GPIO.OUT) GPIO.output(7, True) GPIO.output(11, True) # Handle http requests to the root address @route('/') def index(): return 'Go away.' # Handle http requests to /garagedoor @route('/garagedoor/:doornum') def garagedoor(doornum=0): if doornum == '0': return 'No door number specified' elif doornum == '1': GPIO.output(7, False) time.sleep(.8) GPIO.output(7, True) return 'Door number 1 cycled.' elif doornum == '2': GPIO.output(11, False) time.sleep(.8) GPIO.output(11, True) return 'Door number 2 cycled' run(host='0.0.0.0', port=1234)
On your Raspberry Pi, create a new Python file using nano:
Copy and paste the script above into the empty document. Exit and save with CTRL+X, then Y, and Enter to confirm.
Now test the script by running it:
If everything is working, you will see a message like this:
If you get any error messages, check that everything got pasted into the file correctly and that you don’t have another web server like Apache running on the same port (it will interfere with the server the script creates).
Assuming there are no errors, go to a web browser on another computer on the same network, and into the address bar type the IP address of your Pi followed by a colon and 1234. For example, if the IP address of your Raspberry Pi was 184.108.40.206, you would type 220.127.116.11:1234 into your browser.
If everything works, you will see a message telling you to go away!
Now add /garagedoor/1 after the IP address and port number, like this: 18.104.22.168:1234/garagedoor/1
Hit Enter, and you should hear the first relay on the board click twice as it closes and opens again. If you change the 1 to a 2 and reload the page, you’ll hear the second relay cycle.
Step 4: Connect the Relay to the Door Motor
Refer to your garage door motor manual to find where an external controller can be connected. Attach the two-core cable to it, and connect up the other end of the cable to the relay board’s screw terminals.
There are three terminals per relay—use the normally open pair as shown in the picture.
Test everything from the web browser again (you can just refresh the page). If all goes well, the relay will click a couple of times and your garage door should start to open.
Step 5: Set the Script to Autoload
If your Raspberry Pi reboots for any reason, such as a power outage, your Python script will stop running. To make it load on startup, add the following line to your /etc/sc.local file (if you are logged into your Pi as a different user, change Pi to your username):
nohup python /home/pi/garagedoor/door.py &
Do this using your usual text editor, saving the file when you’re done.
Security Considerations to Keep in Mind
There is no kind of security on this sample Python script—anyone who can access your Raspberry Pi via its IP address will be able to open and close your garage door. It’s tempting to think that just because nobody knows your script is there, nobody will find it, but security through obscurity has been shown time and again to be a bad idea.
A full authentication system is beyond the scope of this tutorial, but a simple solution to security is to not make your Pi accessible outside of your home network.
Here are some more tips for securing your Raspberry Pi .
Controlling Your Garage Door With IFTTT or Siri
If you do choose to make your Pi accessible from the open internet, you can control your garage door from services like If This Then That (IFTTT). For example, you can combine the Alexa and Webhooks IFTTT services to open your garage door when you say a trigger phrase to an Amazon Echo.
If you drive a car with an IFTTT connected service, you could create an applet to open the door as your car approaches your property, Batman-style.
Want to know more? See our downloadable guide to using IFTTT like a pro .
Another way to trigger your newly internet-aware garage door is with a Siri shortcut. Putting a really simple shortcut into your widgets means you can forget about carrying annoying keys and instead open your garage with a swipe and tap on your iPhone.
Beyond the Garage: More DIY Smart Home Projects
Relay boards can switch mains voltage and so can control most household appliances. This project can serve as the basis for adding smarts to almost anything with a power supply. Coffee machines, lighting, air conditioners—all are ripe for automating with a Raspberry Pi and some relays.