Knowledge for the World

How to control your garage door from your phone using a Raspberry Pi

When I think of home automation with a Raspberry Pi, controlling my garage door is the first project that comes to mind. So I decided to build this myself. The goal is to hook a Raspberry Pi up to my garage door opener, set up some sensors to determine the state of the garage door, and create a web page that will control it all.

I've written enough software and played with a Raspberry Pi long enough that this was a fairly simple project. The hardware is cheap and I've packaged the software up in a Docker image, so with this guide and a few free hours you can build this as well!


A demo of the project

Here's a quick demonstration of the finished project.


The full Raspberry Pi setup is outside the scope of this guide, but we've written a handful of guides to help. I recommend starting with our guide on a completely headless Raspberry Pi setup.


Figure out how your garage door opener works

Although this guide will provide most of the technical details to complete this project, there are a few things you'll have to do on your own.

So the first step is to take apart your garage door opener (the one mounted to your wall) and figure out how it works. There's a good chance that your garage door opener works in a way similar to mine.

I unmounted the opener from the wall and opened it up. There was a red and a white wire that were secured to the PCB (circuit board) by screws, so my first guess was that this operates like a normally open switch and if I connect the wires (and close the circuit) it will activate the garage door.

To test this theory, I hooked up my own NO (normally open) switch to the two terminals on the PCB and tested it out. To my delight, it worked!

So with this mystery solved, our circuit is going to be pretty straight forward. We'll put a relay in place of this switch, and the Raspberry Pi can drive that relay.


Wire up the circuit to the Raspberry Pi

In this step, we'll follow the diagram below to wire up our circuit. We'll use the Raspberry Pi to drive the relay, which will activate the garage door opener. And of course, we'll need software to drive the relay, but we'll deal with that later.

The following is diagrammed below, but I'll describe it here as well. Using female to female jumper cables, wire pin 11 on the Pi to the IN1 pin on the relay. Wire pin 2 (5V) on the Pi to the VCC pin on the relay. Then wire pin 6 (GND) to the GND on the relay.

Then wire the relay to the garage door opener. The relay should be wired in the normally open position so that when activated, the circuit will close, and the garage door will open/close. If you need more details, follow the guide we've written on how to use a relay with the Raspberry Pi.


The next part that will require a bit of problem solving on your part is to determine where to put the sensors. We're going to use two magnetic reed switches to determine the position of the door. The three positional options are open, closed, and somewhere in between.

If you're unfamiliar with these types of switches, they are the same switches that are often used in security systems to secure a window. There are two parts to the switch - the sensor and the magnet. When the magnet is moved close to the sensor, it closes the circuit. When the magnet is moved away, it opens the circuit. So we're going to use this to determine the position of the garage door.

We'll use two switches because we want to know if the door is fully open, fully closed, or somewhere in between. So the first step is to open the garage door completely and determine where to put the first switch. Then close the door and determine where you'll put the second switch.


Wire up and secure the sensors

Once you've determined the positions, you'll need to measure how much wire you'll need to connect each switch to the Raspberry Pi. Each switch has two leads, so I needed four 25 foot spools of 22 gauge wire. So before securing the switches, solder the spools of wire to the leads from the switches. Then secure the switches in place. The switches I bought had an adhesive strip that I used to secure them.

Note: This may seem obvious, but the magnet should be attached to the door and the sensor should be attached to the rail.

After securing the first switch, close the garage door and position the second switch. I recommend putting the two switches as close to each other as possible to make the wiring easier.


With the two switches in place, you'll now need to run the wire across the ceiling. I secured the wire to the ceiling using staples, but I was extremely careful not to damage the wire, It might be wise to use cable clips.

Now that the wires are run to the Pi, we'll need to connect them. I prefer to use jumper cables to connect to the Pi, so I'm going cut two female to female jumper cables in half, then strip and solder them to the switch wires. Now I can easily connect them to the correct pins.

This next part is somewhat important. We need to know which wires belong to the "fully open" switch and which wires belong to the "fully closed" switch. For both switches, we'll connect one of the wires to a ground (it doesn't matter which wire); you can use pins 20 and 14 on the Pi. Now connect the "fully open" wire to pin 19 and the "fully closed" wire to pin 13.

It's important that you use these pins if possible, because the software we'll use later is configured to read from these pins. If you feel that you must use different pins, we can specify the correct pins at run time.


The web application that serves the garage door controller UI is packaged in a Docker image, so we'll need to install Docker. To install Docker, log in to your Pi and run:

curl -sSL | sh

We've also written a detailed guide on installing Docker on the Raspberry Pi, so check out the guide if you need more details.


With Docker installed, we can now run the garage-pi image using:

sudo docker run \
    --restart=always \
    --device=/dev/mem:/dev/mem \
    --name=garage-pi \
    --privileged \
    --publish 80:80 \
    -d \
Navigate to the garage door controller on your mobile browser

Before we open this up on the phone, we need to find the IP address of your Pi. There are a few ways to do this, but we're going to use nmap. Explore the attached guide if you'd like to use a different method.

To find the IP address, open a shell and type:

sudo nmap -sn

Note: You'll need to use the correct subnet.

You should be able to find your Raspberry Pi in the output. Copy down the IP address. The IP address of my Pi is so I'll use this for the rest of the step, but where you see this IP address you should know to use your own.

Now navigate to your IP address in your mobile browser by typing You should see the very simple web UI that can be used to control your garage door.

At this point, you should be able to hit the "Open" and "Close" button to control your garage door!


Add a shortcut to your home screen

Since the IP address can be hard to remember and cumbersome to type on your phone, you might consider adding a shortcut to your home screen.

If you're using an iPhone you can click the action icon at the bottom of Safari, then scroll across the bottom menu until you see Add to Home Screen.


If this is something you're interested in building for yourself, you might consider contributing to the project.


The finished project

Here's a look at the Pi and relay inside of the project enclosure!