Maker Project: Creating a Garage Door Opener

Introduction

Have you ever wanted to control your garage door from your smartphone? By using a basic Arduino Uno, an Ethernet Shield, and a Relay Module, you can open and close your garage door from your smartphone too, from anywhere in the world (granted you have internet access!).

What You'll Need

  • Arduino Uno
  • Arduino Compatible Ethernet Shield
  • Relay Module (I used a SainSmart 4 Relay Module)
  • Web Server (Optional, but recommended)
  • Ethernet connection

Before You Get Started

This tutorial assumes that you have a basic understanding of electronics and the Arduino platform. It also assumes that you have a web server available to host the garage opener interface that is accessible from your smartphone. A web server is option because you can connect directly with the device to control the garage, but this tutorial will be utilizing a web server. You will also need an Ethernet connection in the garage, or a long connection from the garage door motor to your device. Additionally, one port will need to be opened on the firewall on your home network to allow our web page to talk to the Arduino.

How It Will Work

Garage door motors have connections on the back of them to hook up door block sensors and buttons to open the door from inside the garage. Our relay module will be connected to the same connection points as the button used to open the door. The relay is acting like an electronic button; the Arduino will tell it to open only for a second so the garage motor thinks the button was pressed.

The smartphone will load up a web page with a form button. When the button is pressed, the web page will connect with the Arduino and tell it to click the relay for 1 second, allowing the garage door to either open or close.

Arduino and Relays

You should have an Arduino Uno and an Arduino-compatible Ethernet Shield. Connect the Ethernet Shield to the Arduino device; all of the header pins should line up and it should fit snugly on top of the Arduino Uno.

The relay module I am using has 6 pins on it: VCC, Relay 1, Relay 2, Relay 3, Relay 4, and ground. Connect VCC to 5v pin, ground to gnd pin, and relay 1 to pin 2 on the Arduino. No breadboard is necessary for this project because the relay module contains all the components needed to make the relays switch with just logic-level power from the Arduino.

Garage1
Figure 1: A closer look at the relay module

Arduino Code

Here is the full code. I have added comments to explain what is going on. You will need to set a unique IP address and a MAC address. There is a good chance the numbers below are already unique to your network, but you should confirm this before putting this device live on your network.

#include <Ethernet.h>
// NETWORK SETTINGS
//MAC ADDRESS
int port = 84;byte mac[] =
   { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEE };
// DEVICE IP ADDRESS
byte ip[] = { 192, 168, 1, 241 };

// SET ETHERNET PROPERTIES
EthernetServer server(port);
EthernetClient client;

// VARIABLES
// READSTRING FOR ETHERNET READ
String readString;
int garageDoor = 2;  // RELAY PIN


// ********** START CODE **********


void setup(){
   // SET RELAY PIN AS OUTPUT
   pinMode(garageDoor, OUTPUT);


   // ETHERNET CONNECTION
   Ethernet.begin(mac, ip);
   server.begin();

   //PRINT TO SERIAL
   Serial.begin(9600);
   // PROJECT NAME
   Serial.println("GARAGE CONTROLLER");
   // IP ADDRESS
   Serial.print("IP Address: ");
   // IP ADDRESS
   Serial.print(Ethernet.localIP());
   Serial.print(":");
   Serial.println(port);   // PORT

   // EVERYTHING IS LOADED - WAIT FOR REQUESTS
   Serial.println("Ready for requests...");
   Serial.println();
}

void loop(){
   // CREATE A CLIENT CONNECTION
   EthernetClient client = server.available();
   if (client) {
      while (client.connected()) {
         if (client.available()) {
            char c = client.read();

            // READ CHAR BY CHAR UNTIL FINISHED
            if (readString.length() < 100) {

               // SAVE EACH CHAR TO STRING
               readString += c;
            }

            // WHEN END OF READ IS REACHED, PARSE
            if (c == '\n') {
               // DISPLAY NEW PAGE
               client.println("HTTP/1.1 200 OK");
               client.println("Content-Type:
                  text/html");
               client.println();

               client.println("<HTML>");
               client.println("<HEAD>");
               client.println("<TITLE>Garage
                  Control</TITLE>");
               client.println("</HEAD>");
               client.println("<BODY>");


               // CHECK TO SEE IF GARAGE DOOR REQUEST
               // WAS SENT

               if(readString.indexOf("flashgarage") >0) {
                  // "flashgarage" IS QUERY STRING KEYWARD

                  // SET RELAY PIN TO HIGH
                  digitalWrite(garageDoor, HIGH);
                  delay(500);   // WAIT
                  // SET RELAY PIN TO LOW
                  digitalWrite(garageDoor, LOW);
                  // PRINT TO SERIAL
                  Serial.println("Garage Door Opened.");

               }


               // END PAGE
               client.println("</BODY>");
               client.println("</HTML>");

               delay(1);
               client.stop();

               Serial.println
                  ("-------------------------
                  -------------------------");
               //clearing string for next read
               readString="";

            }
         }
      }
   }
}

Build and upload that code to your Arduino Uno!

Test Your Arduino

Once you have your Arduino code uploaded and connected to the network, try controlling the device manually via a web browser by going to http://192.168.1.241/?flashgarage (the IP should match whatever you chose earlier). Relay 1 should click to close the circuit and click again to open the circuit. Success!

Smartphone Web Page

To control the garage door from your smartphone, a web page will be used with a basic script. The script has a security feature built in to ensure that only the intended users can gain access to the site and control the garage door. The page I created is a basic web page with a single button and it's optimized for mobile web browsing.

Garage2
Figure 2: The controller, as seen on a smartphone

That web page is uploaded on my personal web server. To access that page, I open my browser to http://my.webserver.com/garage.php?securitycode=MYSECURITYCODE. If MYSECURITYCODE is wrong or missing, an error message is shown and nothing will be opened. I have this URL saved on my iPhone home screen with the security code already saved in the URL so I do not need to remember it each time.

Web Page Code

Below is the PHP portion of the web page script. To see the entire script, go to the downloads section and open the PHP document.

session_start();
// Secret code required to open the door
$securitycode = "MYSECURITYCODE";

// URL to Arduino device
$arduino_url =
   "http://MYHOMEIPADDRESS:84/?flashgarage";

// Check to see if button was pressed, security
// code is correct, and random hash is current.
if ($_GET['action'] == 'garagedoor' &&
   $_GET['securitycode']==$securitycode &&
   $_SESSION['random_hash'] == $_GET['hash']) {
   // CURL the Arduino URL
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $arduino_url);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_close($ch);
}
// Create random hash to prevent multiple
// submissions on refresh
$random_hash = md5(time().$securitycode.time());
$_SESSION['random_hash'] = $random_hash;

You will need to set a security code as well as your Arduino URL. Depending on what port you chose for your device, you will need to port forward that port on your Firewall to your Arduino device. Google "what's my ip address" to get your home IP address and put that in the preceding code with the port you selected.

Connect Your Garage

Check the back of your garage door motor; there should be several connections on the back, one of which will be a place to connect a push button. Connect one wire from the positive power connection to the relay 1 center connection on the relay board. Connect another wire from the push button connection to the normally open connection of relay 1 on the relay board. Your switch is now complete!

Garage3
Figure 3: Wiring the relay to the garage door opener's motor

Conclusion

Creating a garage door opener with an Arduino is a relatively simple project that will add convenience to your life. Take this project further by adapting it for use with services like IFTTT (If This Than That) so your garage door will open automatically when you arrive home at night!



Related Articles

Downloads

Comments

  • Scott

    Posted by Link to php code on 08/11/2016 09:19pm

    Awesome stuff, I pretty much got everything set up, but I was unable to get the PHP code, is it possible to get a link to that?

    Reply
  • time sensitive automatic closer

    Posted by Dave on 05/18/2016 11:39am

    This is a nice controller. I was wondering if anyone has added the "forgot to close the door feature"? what I mean by that, if you are home all day, the door is open, you go to bed and the next morning you realize you "forgot" to close the door. I am looking to add a feature, or maybe just this feature that a settable time "say 10:00 PM, if the door is open, the Arduino will close the door.

    Reply
  • no access to php code download link

    Posted by Ree on 02/20/2016 08:58am

    For some reason link to php code download says no access. Can you please help me with the link? Thanks

    • IT Guy

      Posted by Scott on 08/11/2016 09:44pm

      Any luck getting the PHP webcode?

      Reply
    Reply
  • Driveway Gates San Diego

    Posted by Driveway Gates San Diego on 11/05/2015 03:32am

    Wow Great article! Connecting your garage door opener to your phone is a great idea. You could easily just close the door from your work if you for got. you have a great mind thanks for sharing with us.

    Reply
  • Manager

    Posted by James Bay on 09/23/2015 08:19am

    I have been needing to get a new garage door opener. I never knew that I could make one myself. I might give this a try! Thanks for the information.

    Reply
  • http://www.vadoor.com/garage_doors.htm

    Posted by Johnson McGee on 08/26/2015 11:08am

    This is a great guide for connecting your garage door to your smartphone, thanks! I think that doing this would make things much more convenient. You could easily just close the door from your bed if you forgot. I am thinking of putting one of these in, so this is very helpful. I am making some plans tomorrow, actually. What if I'm not very tech savvy?

    Reply
  • http://www.unlimiteddoors.org/services.html

    Posted by Gus Chiggins on 07/16/2015 01:55pm

    Connecting your garage door opener to your phone is a great idea! This would make it simple to close in many cases. Especially if you accidentally misplace the remote. This is a great added security device for today's modern world.

    Reply
  • Garage Doors

    Posted by Dave Thompson on 06/23/2015 12:24pm

    I've always wanted to make my very own garage door opener. It does look like you need a lot of coding experience to be able to get it to work. Good thing my brother is a coding master. I will have to see if he can help me get it set up and running. Thanks for the great article.

    Reply
  • Arduino garage door opener

    Posted by Dragan Nikolic on 02/17/2015 10:15am

    Wow, I've never realised that this could be done and from scratch, you've even compiled a simple app.These guys often pop up (URL REMOVED) when I search for garage remotes, but this is taking it to a whole new level.

    Reply
  • Php code

    Posted by Andrej on 02/13/2015 09:30am

    Hello, very promising project itself would need something like that to complete my project. Where can I find the whole php code? For which version of PHP code is written? greetings

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date