Introduction: Raspberry Pi Garage Door Opener
Hello Everyone!
This instructable explains how I setup a Raspberry Pi to open my garage door using a smarthphone. While this has been done before, I thought I'd post my solution. This was my first hardware project and instructable ever and I'm sure I made some mistakes. So, when you find one let me know!
Project Overview:
What we will be doing is turning the Raspberry Pi into a small web server. When you access the webserver from your browser of choice, you will have a big button that triggers the garage door via a relay. We will wire a very basic circuit to the Pi's GPIO pins and upload a website that triggers the circuit. When the relay is triggered, it closes the circuit hooked up to the garage motor and opens the garage.
Why would anyone want to do this?
Well, my garage door opener was broke and this was cheaper than replacing the other system. As an added plus though, you could wire up additional sensors and be able to make sure your garage is closed remotely if your were so inclined.
Shopping List:
I consider myself pretty cheap, and I tried to keep the costs minimal. All of the items are available on prime.
1.) Raspberry Pi - Model A - $32
2.) Wifi Adapter - $10
3.) PSU - $5
4.) 5v Relay - $6
Total: $53.00
You will also need an sdcard >= 2GB and some wires, but I had extra of each.
Step 1: Install and Optimize Rasbian (for Our Purposes)
This first step is to install an operating system to your rpi. I'm a bit of a debian fanboy, and had an extra 2GB sdcard, so I went with a shrunk version of Wheezy. The image I used can be found here:
http://raspberry.mythic-beasts.com/raspberry/images/raspbian/
For full instructions on installing an OS to your PI and other images, visit http://www.raspberrypi.org/downloads.
On Ubuntu, I used gparted to format to fat32, and dd to write the img.
After you install the OS, plug in a usb keyboard and hook up the raspberry pi to a monitor. Assuming you are using Wheezy, on the first boot rasp-config will automatically run. You should use this tool to stretch the parition and enable ssh (under the advanced menu on newer versions I believe).
After I installed my img, I also removed the GUI to free up some space. (If you have a large SD, you can skip this.) To do this type these commands:
$ sudo apt-get remove --purge x11-common
$ sudo apt-get autoremove
This removes all packages that depend on X11 which is pretty much all of the GUI.
Step 2: Setup Wifi Via the Command Line
The next step is to setup your wifi from the command line. This will allow us to control the pi remotely via ssh.
Here is a great guide for Wheezy:
http://www.howtogeek.com/167425/how-to-setup-wi-fi-on-your-raspberry-pi-via-the-command-line/
Since we are using the Model A with only one usb port, you will need to set up the configuration with your keyboard, shutdown the pi, insert the Wifi Dongle and then start it backup. This may take a little guess and check.
The command to shutdown the pi is: sudo shutdown -h 0
If all goes well, once you set it up and reboot, your pi will be given an IP address by your router. You can find this IP address by either hooking the pi up to an hdmi monitor and looking at the boot log, or logging in to your router and looking at the DHCP table.
Step 3: Install Software
Now that we have the wifi setup, we are going to download and install the necessary software to our pi. Since our usb port is now being used by the wifi dongle, we will do this via ssh.
If you are using Ubuntu, open up the terminal and type ssh pi@[Your Pi's Ip address]. If you are using Windows, you can download Putty. On OSX, you can also just ssh from the terminal. Again, the default password on Wheezy is raspberry.
Once your a logged in, download, compile, and install Wiring Pi. This software allows us to control the GPIO pins on the pi. Here is a step by step guide for that:
http://wiringpi.com/download-and-install/
Once Wiring Pi is installed, you will want to install Apache and PHP via these commands:
$ sudo apt-get update
$ sudo apt-get install apache2 php5 libapache2-mod-php5
Once this is done, you will have a working webserver! To verify that, just type in your pi's ip adress in a browser. You should see Apache's default website which says "It Works!".
Step 4: Upload the Garage Opener Website
Now that we have a working webserver, we are going to upload a website to it. I've created a very basic one that will trigger the relay we will wire in the next step.
Here are two ways to do that:
Ubuntu
Dowload the GarageOpener.zip to your desktop. Open up your terminal, and type the following commands:
$ ssh pi@[YOUR PI'S IP ADDRESS]
$ sudo rm /var/www/index.html
$ sudo chown pi:root /var/www
$ exit
$ cd ~/Desktop
$ scp GarageOpener.zip pi@[YOUR PI'S IP ADDRESS]:/var/www
$ ssh pi@[YOUR PI'S IP ADDRESS]
$ cd /var/www
$ unzip GarageOpener.zip
$ rm GarageOpener.zip
Any OS
Download Filezilla. Using Putty or another ssh terminal:
$ ssh pi@[YOUR PI'S IP ADDRESS]
$ sudo chown -R pi:root /var/www
Start filezilla. Log into the raspberry pi with these credentials:
Host: sftp://[YOUR PI'S IP ADDRESS]
Username: pi
Password: raspberry
Then upload all of the files from GarageOpener.zip to /var/www. Also, delete the existing index.html.
Some Technical Notes (for those interested):
The website uses jQuery to post to itself (via AJAX) when a user clicks on the big button. I did this so that if you refresh the page it doesn't trigger your garage to open.
If your using an iPhone (or the latest dev version of Chrome on Android) and add this website to your home screen, it should work like an app without the browser chrome. (It will still only work when your on your home wifi though :-P )
Attachments
Step 5: Wire the Circuit to the Pi!
Now for the fun part - we wire the relay to the pi! For the code I provided (step 4 and 6) I used GPIO pin 7. You can use whichever one you want, but be sure to change the code.
Below is a diagram and my wired pi mounted on cardboard with zip ties. I used an old floppy disk ribbon cable for easy testing of ports and just left it connected.
Step 6: Create a Startup Service
This step is important.
Most relays including the one I purchased, operate like this - when the signal is ON the circuit stays off. When the signal is OFF then the circuit is on. So what happens if your pi looses power? Well most relays have a safety mechanism that keeps the circuit OFF when there is no power at all. The problem that occurs happens between when the pi (and subsequently the relay) gets its power back but before the pi has finished booting to turn the signal ON which is need to keep the circuit off. You could wake up in the morning with your garage open and potentially a few new friends!
After some experimenting, I found a simply work around. I found out that my relay doesn't actually initialize until the GPIO pin mode is set via this command: gpio mode 7 out. Furthermore, I found out that it you set the GPIO pin to ON (gpio write 7 1)before you set the GPIO mode, the relay will stay off once initialized.
To make this initialization run at boot, I created a start-up script.
$ ssh pi@[Your Pi's IP]
$ sudo nano /etc/init.d/garagerelay
Then paste this script:
#! /bin/bash
# /etc/init.d/garagerelay
# Carry out specific functions when asked to by the system
case "$1" in
start)
echo "Starting Relay"
# Turn 7 on which keeps relay off
/usr/local/bin/gpio write 7 1
#Start Gpio
/usr/local/bin/gpio mode 7 out
;;
stop)
echo "Stopping gpio"
;;
*)
echo "Usage: /etc/init.d/garagerelay {start|stop}"
exit 1
;;
esac
exit 0
Make the file executable:
$ sudo chmod 777 /etc/init.d/garagerelay
Now tell your pi to run this script at boot:
$ sudo update-rc.d -f garagerelay start 4
(Note: You can safely ignore the "missing LSB tags" warning.)
Voila!
Step 7: Attach Raspberry Pi to the Garage
This part is very easy. Just follow the wires of the button attached to your garage motor and attach the relay the same way. Since the relay isolates the circuit, the direction doesn't even matter.
And you're done! Let me know it works for you.

Participated in the
Microcontroller Contest
22 People Made This Project!
olsentinkers made it!
kergareth made it!
troubadourexpat made it!
SteveA206 made it!
chuckf201 made it!
AndrewM559 made it!
joshuacarroll made it!
JorisM3 made it!
JulianW12 made it!See 13 More


















195 Discussions
Question 5 weeks ago
Hi
I just started this project and managed to install and run all commands. my pi zero reboot ok but i get
The requested URL was not found on this server.
xxx.xxx.1.123 Port 80I am at my local network not awayAny ideatx
2 months ago
<?php
// Added authentication
// run a command using the cookie values for username and password
function run_cmd($cmdpart)
{
global $retval,$stdout;
// use su to run a command as a user and redirect stderr to stdout
$cmd='echo '.$_COOKIE['password'].'|su '.$_COOKIE['username'].' -c "'.$cmdpart.'" 2>&1';
exec($cmd,$output,$myretval);
// if any commands exited with a good return value login should be good.
// otherwise flag retval which should request another username password combo
if($myretval==0||$retval==-1)
{$retval=$myretval;
}
$stdout.=str_replace('Password:','',$output[0])."<br>";
}
//set retval to -1
$retval=-1;
if(isset($_REQUEST['username']) && isset($_REQUEST['password'])){
setcookie('username',$_REQUEST['username'],time() + (86400 * 30), "/");
setcookie('password',$_REQUEST['password'],time() + (86400 * 30), "/");
}
if(isset($_COOKIE['username']) && isset($_COOKIE['password']) && isset($_REQUEST['trigger']) && $_REQUEST['trigger'] == 1){
error_reporting(E_ALL);
run_cmd('date');
run_cmd('gpio write 7 0');
usleep(1000000);
run_cmd('gpio write 7 1');
}
if(isset($_COOKIE['username']) && isset($_COOKIE['password']) && isset($_REQUEST['logout']) && ($_REQUEST['logout'] == 1)||$retval==1) {
setcookie('username','',1,'/');
setcookie('password','',1,'/');
header("Refresh:0");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Garage Opener</title>
<link rel="apple-touch-icon" href="apple-touch-icon-iphone.png" />
<link rel="apple-touch-icon" sizes="72x72" href="apple-touch-icon-ipad.png" />
<link rel="apple-touch-icon" sizes="114x114" href="apple-touch-icon-iphone-retina-display.png" />
<link rel="stylesheet" href="css/style.css" type="text/css">
<meta name="apple-mobile-web-app-capable" content="yes">
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="js/script.js"></script>
</head>
<body>
<?php if(isset($_COOKIE['username']) && isset($_COOKIE['password']) && $_COOKIE['username'] != '' && $_COOKIE['password'] != '') { ?>
<form>
</form>
<form method="POST">
<input type="hidden" id="trigger" name="trigger" value="1">
<div>
<input type="submit" name="submit" value="" />
</div>
</form>
<form method="POST">
<input type="hidden" id="logout" name="logout" value="1">
<div>
Logged in as:<?php echo $_COOKIE['username']; ?>
<input type="submit" name="submit" value="Logout" />
<?php echo $stdout; ?>
</div>
</form>
<?php
}else{
if(isset($_POST['username']) && isset($_POST['password'])){
//refresh needed to finish setting cookies
header("Refresh:0");
}else{
?>
<form>
</form>
<div>
<form method="POST">
<table>
<tr><td><label for="username">Username: </label></td><td><input id="username" type="text" name="username" /></td></tr>
<tr><td><label for="password">Password: </label></td><td><input id="password" type="password" name="password" /></td></tr>
<tr><td colspan="2"><input type="submit" name="submit" value="Submit" /></td></tr>
</table>
</form>
</div>
<?php
}}
?>
</body>
</html>
3 years ago
Thanks for the excellent documentation and instructions. I made this yesterday using a Raspberry Pi 3, but I am now thinking of modifying the set-up to use a Pi Zero W, since this set-up really doesn't require much horsepower. Has anyone else done that?
I also modified the overall set-up a little bit by setting up a static ip using port forwarding on my Google WiFi and the no-ip service so that I can access the opener from anywhere. I have a Smart Things hub at my house, so I can sense whether the garage is open or closed, then I can use this RasPi set-up to open or close it when I want to.
I do have three questions:
1. How can I modify the UI with a photo or some other more interesting graphic than just a plain gray box? I'm not well-versed in CSS, and I am guess that it is all in the awrap div. Can anyone lead me in the right direction?
2. I'd like to also modify the code (and the UI) to allow me to control two garage doors. I'm sure someone has done it, so I'd be interested in the code and UI mods.
3. Now that the Pi is essential an IoT device, has anyone modified the code to put a password or other security in the system? We don't want some script kiddie opening and closing our garage door after finding the Pi while looking for devices.
Thanks!
Reply 5 months ago
How did you know how to modify the directions for the raspberry 3? I have a model B and I was wondering if I can still do it.
Reply 7 months ago
I've completed the hardware solution for the Pi Zero W but I haven't yet visited the software. https://www.instructables.com/id/Raspberry-Pi-Zero-Garage-Door-Opener-Hardware/
Reply 2 years ago
Recently, I made a similar device. The biggest difference is that I programmed a python script that connects to an mqtt server. It listens for commands sent via mqtt. I have a button on my smart phone that can operate the device and it is also connected to my amazon echo so I can say "Alexa, ask garage Door to open." And it opens.
One benefit to using the mqtt method is that you don't have to worry about forwarding ports or opening the firewall. mqtt works in a sub/pub model. the pi subscribes to the server, the other devices publish to the server.
If anyone is interested I do have all this packaged in a .deb so you just install it, modfiy a couple lines in the .conf script and you are ready to go!
Reply 1 year ago
Please provide link: I am interested
"If anyone is interested I do have all this packaged in a .deb so you
just install it, modfiy a couple lines in the .conf script and you are
ready to go!"
Reply 2 years ago
Hi! Can you please explain how you have this setup with Alexa? I have all the parts, and plan on building this asap, but I'm unsure about getting alexa to control it. I'd greatly appreciate your input! Thanks!
Reply 2 years ago
I would be extremely grateful to get my hands on your work! Is your app for android, or iOS?(and would you share the code for all this)
Reply 2 years ago
Definitely interested in this!
Reply 2 years ago
I am trying to make this with a Zero W right now but the website does not trigger the relay properly. I know the relay works because I tested it with a python script but for some reason nothing is happening with the web trigger. Did you come across this issue?
Question 5 months ago on Introduction
Hi,
Trying to purchase the wires but unsure of the exact name. Are they female to female wires?
Thanks!
Tracey
Question 2 years ago
new to raspberry pi. thought this seemed like a useful and simple enough project to get my feet wet. using a Pi zero W btw.
I got the web server/page running, and the relay is getting 5v power. I can even make the relay fire by shorting the ground to either relay 1 or relay 2.
but for the life of me i can not get it to fire the relay by clicking the button. any ideas?
Answer 6 months ago
Raspi zero is probably using a different pin number than 7. Start up script and trigger php would need to be modified with the appropriate pin
Answer 1 year ago
Hi - did you ever solve this? I'm having the exact same problem and it's driving me crazy!!
Question 1 year ago on Step 6
Anyone know how I could add functionality to open another garage door using the same pi on the 2nd channel of the relay?
Answer 6 months ago
You'd create a second trigger utilizing the GPIO pin that in2 is set on the relay, assuming you have a 2 switch relay. You'd also need to modify the startup script to include the gpio mode 'pin#' out so you can utilize the gpio pin
Answer 10 months ago
This is a little late, but if anyone else is interested, please see my repo at https://github.com/nwielenga/garageDoorOpener/tree/master, currently set up to open 3 doors.
4 years ago
THANK YOU for posting this - what a great system and guide! I built this two years ago and just had to re-build it due to an SD card failure.
Mine wouldn't work until I added a "-g" to the two commands in the /etc/init.d/garagerelay scripts as such:
# Turn 7 on which keeps relay off
/usr/local/bin/gpio write -g 7 1
#Start Gpio
/usr/local/bin/gpio mode -g 7 out
I vaguely remember having to do the same thing 2 years ago. You might want to edit the instructions to include the -g, which as you probably know tells the Pi to refer to the GPIO pin number, and not the "actual" or numeric pin number.
Also, one other tip. The photos you show are for an older RPi Model A, which is fine. Personally I'm running on a Model B. You may want a note saying to refer to your own pin-out diagrams (and specifically the GPIO pins) if anyone is using a more modern Pi 2 Model B or B+, or even a Pi 3 Model B...
Thank you again - I am running this to provide access to an entire Club so they can open the access gate from their phones via wifi when the RF-remotes won't reach the base station. Now... the next project is for me to turn it into an app instead of "just" a local web server! (Hah, that's what I said ~2 years ago too...!)
Reply 4 years ago
Also, I had to initialize GPIO as off, or zero, not 1 as in the example. After rebooting the relay was pegged and connected (causing the remote to trigger and open the gate), so I manually issued the gpio -g write 7 0 command, fixed the startup script with the "0", and rebooted to confirm that it worked... which it did! Thanks again, and I hope some of this helps others as well!