Smart home gadgets are cool but can be expensive. With a Raspberry Pi and one or two components, it’s easy and cheap to connect existing devices to the Internet.
Creating your garage door’s internet door is a great introduction to learning how to control the real world with the Pi. And let’s be honest, who doesn’t want to feel a little like Batman and have their garage open when they pull up to their driveway?
What you need
To automate your garage doors, you will need:
- Garage door motor that can use an external trigger.
- Raspberry Pi (any model) is connected to the internet. This guide assumes that your Pi is running the Raspbian operating system (if it isn’t, see our article on how to install Raspbian on a Raspberry Pi).
- Relay expansion board, which can be found on Amazon.
- Power supply 2A for Raspberry Pi. The lower rated model may have trouble driving the relay board as well as the Pi.
- Four connecting cables (plus one more for each additional door you want to control).
- A two-wire cable is enough to get between the pi and the garage door motor. A cheap speaker is ideal, but a phone or ethernet cable might work too.
Once you’ve got these 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 installers can plug in a simple button somewhere in the home to open or close a door without using a conventional remote control. When the start button is pressed and released, it momentarily closes a circuit that tells the engine to start or stop.
You are going to use a relay instead of an external button. Shorting the relay will close the circuit, just as if the start button had been pressed. A Python script running on a Raspberry Pi will allow you to control the relay and therefore the door from your home network.
Step 1: Connect Raspberry Pi to Relay
You will be making at least four connections between the Raspberry Pi and the relay board. If you’re using a Pi Zero, you’ll either need to solder the connections directly, or solder the GPIO header to the Pi and use jumper wires for the connections. The last option is recommended because if you ever want to disconnect the relay board and use your Raspberry Pi for something else, you won’t need to solder your connections. The larger Pis already has pins for connecting push-on jumpers.
If you are new to using GPIO, be sure to read our article Everything You Need to Know About Raspberry Pi GPIO Pins.
Before connecting, check if your relay board has a jumper connecting the VCC and JD-VCC pins. If so, remove it because you need to connect VCC and JD-VCC separately.
With everything off, connect the relay board to your Pi like this:
- Start 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 the pin of Pi 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 must add additional connections between IN2, IN3, etc. on the relay board and other free GPIO input/output pins on the Pi.
Step 2Install Dependencies on Raspberry Pi
Raspbian comes pre-installed with Python, but you will need to add the GPIO library. Enter the following in the terminal window on your Pi:
sudo apt-get update sudo apt-get -y install python-rpi.gpio
Now create a new folder in your home directory somewhere to put the python script that will control the relay:
mkdir ~/garagedoor cd ~/garagedoor
Finally, download bottle, a lightweight framework that will create a simple web server on your Pi:
wget https://bottlepy.org/bottle.py
Step 3: Create a control script
Here is 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:
nano door.py
Copy and paste the script above into a blank document. Exit and save by clicking CTRL+X then Y and Enter for confirmation.