Background story / motivation
One night, I opened my garage door to take out the trash. Then I came back in and went to bed. The next morning, as I was strapping my son into his car seat, he asked me, “Daddy, why is that exercise ball over there?” “What? What exercise ball?” “That one. Who put that ball there?” I replied without looking, “I don’t know, I didn’t put it there.” He said, “Maybe mama put it there.” “Maybe she did.” “Maybe it fell.” I looked back and saw it in the middle of the floor. Strange. Didn’t seem like her to leave it like that. Then I looked around. My toolbox was gone. I looked left. My bicycle was gone. I peeked into my wife’s car. Every compartment was open and trashed.
I had left the garage door open and we had been robbed.
Since then, whenever I left the house, I found myself checking and double-checking the garage door. And sometimes even after triple-checking… I would drive back to check ONE MORE TIME. Because you can’t be too sure. Even when I was home, I found myself constantly peeking into the garage to make sure I didn’t leave the door open again.
It was driving me nuts. So I decided to do something about it. If you’re like me, this weekend project will finally give you some peace of mind. If you’re handy with electronics and code, you could even get it done in one evening. When you’re done, you’ll get to open your garage door from your phone. And best of all, you’ll always know if your door is open or closed.
(By the way, before going for this solution to the garage door problem, I considered leaving my garage door open again. I really wanted to set a trap to destroy whoever dared to rob me again. But my wife said no. So here’s just some instructions for a garage door opener… instead of an automated crossbow turret.)
Parts list
- 5V Keyes Relay Module from Deal Extreme ($3)
- Ultrasonic sensor from Deal Extreme ($3.50)
- MINI USB cable + wall charger from eBay ($2) Note: MINI USB, not micro USB like what you usually use for smartphones
- Electric Imp + April breakout board from DigiKey ($25 + $10 + lowest shipping I found)
- 22-gauge hookup wire ($0.06 / foot)
- Mini breadboard ($1.25 each)
- 0.1″ Header pins
Total: $44.75
Tools list
- Soldering iron ($7)
- 60/40 Rosin core solder ($8)
- INNOVA 3320 Digital-ranging multimeter ($23) optional
Finished pictures





Circuit diagram

Electric Imp setup/prep
Electric Imp Agent code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
garageDoorState <- "closed"; function requestHandler(request, response) { try { response.header("Access-Control-Allow-Origin", "*"); if ("garageDoor" in request.query) { local code = request.query.garageDoor; local dataKey = request.query.dataKey; local url = "http://yourgaragedoorapp.appspot.com/rollcode" + "?verify=" + code + "&dataKey=" + dataKey; // Verify rolling code provided against the saved code in your webapp's DB // (and only allow authorized users to save rolling codes in DB). // This extra security measure is optional. If you just want to // open the door without extra checks, call: // device.send("pulseDoor", code); http.get(url).sendasync(function(resp) { server.log(url); server.log(resp.body); local data = http.jsondecode(resp.body); if (!device.isconnected()) { server.log("garageDoor: Imp offline"); } else if ("verify" in data && data.verify == "success") { device.send("pulseDoor", code); } else { server.log("garageDoor: Bad code " + code); } }); } else if ("getStatus" in request.query) { device.send("getDoorState", ""); } else if ("showStatus" in request.query) { response.send(200, garageDoorState); return; } // send a response back saying everything was OK. response.send(200, "OK"); } catch (ex) { response.send(500, "Internal Server Error: " + ex); } } // register the HTTP handler http.onrequest(requestHandler); function saveDoorState(state) { garageDoorState <- state; } device.on("doorState", saveDoorState); |
Electric Imp Device code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
function pulseDoor(data) { server.log("PULSE DOOR START"); doorSwitch.write(1); imp.wakeup(3, pulseDoorStop); } function pulseDoorStop() { server.log("PULSE DOOR STOP"); doorSwitch.write(0); } function getDoorState(data) { range <- Ultrasonic(trig, echo); local isOpen = 0; if (range.read_cm() < 20) { isOpen = 1; } server.log("Door distance: " + range.read_cm() + " isOpen: " + isOpen); server.log("Door " + doorStateAsString(isOpen)); agent.send("doorState", doorStateAsString(isOpen)); } function doorStateAsString(state) { if (state == 1) return "open"; return "closed"; } class Ultrasonic { // consts static TO = 500; // timeout in ms // pins _trig = null; _echo = null; // aliased methods _tw = null; _er = null; _hu = null; _hm = null; // vars _es = null; // echo start time _ee = null; // echo end time constructor(trig, echo) { _trig = trig; _echo = echo; _hu = hardware.micros.bindenv(hardware); _hm = hardware.millis.bindenv(hardware); _tw = _trig.write.bindenv(_trig); _er = _trig.read.bindenv(_echo); } function read_cm() { local st = _hm(); // start time for timeout // Quickly pulse the trig pin _tw(0); _tw(1); _tw(0); // Wait for the rising edge on echo while (_er() == 0 && (_hm() - st) < TO); _es = _hu(); // Time to the falling edge on echo while (_er() == 1 && (_hm() - st) < TO); _ee = _hu(); //if ((_hm() - st) >= TO) return -1; return (_ee - _es)/58.0; } } trig <- hardware.pin1; echo <- hardware.pin2; doorSwitch <- hardware.pin7; trig.configure(DIGITAL_OUT,0); echo.configure(DIGITAL_IN); doorSwitch.configure(DIGITAL_OUT, 0); agent.on("pulseDoor", pulseDoor); agent.on("getDoorState", getDoorState); server.log("Imp online @ " + imp.getssid() + "!"); |
(Optional) rolling authentication code
I made a Google AppEngine app to be a gatekeeper and for the smartphone UI to open/close the door. The idea is:
- Take advantage of AppEngine’s built-in authentication to control who can access your app
- When an authorized person taps “Open”, save a random “rolling code” into your web app’s DB
- Then, tell your Electric Imp to “Open” and pass it the rolling code you just generated
- When your Electric Imp Agent receives the “Open” command, it checks the received rolling code with your AppEngine webapp
- If the rolling code matches the latest code in the DB, the webapp destroys the code (so it cannot be reused) and the Imp Agent tells the Imp Device to open the door
If you want to go this route and need more info, email me at aaron@secretsciencelab.com. If enough of you want it I’ll put it here. I’m just too lazy right now!
Installation
The hardest part is soldering header pins to the Imp, and soldering wires to the Ultrasonic sensor. But other than that, everything else is pretty much plug and play. That’s the beauty of using the Imp, the Keyes Relay and an Ultrasonic sensor. All the electronics are nicely packaged in each of them, so all you need to do is connect them with wires.
I stuffed everything into a small cardboard box and tied it to the frame of the garage door control box. I plugged the Imp’s USB adapter into the same outlet that powered the garage door control box. Lastly, I secured the Ultrasonic sensor to a bolt under the garage door rail using a twist-tie. Dirty, but simple!
Bonus (advanced)
- Push your sensor data to data.sparkfun.com for free:
E.g., door open, door close - Convert your SparkFun CSV stream into an Atom RSS using my URL: http://secretsciencelab.com/wp-content/scripts/phant/feed.php?csv=http://data.sparkfun.com/output/YOUR_PUBLIC_KEY.csv&key=state
(replace the bolded parts). - Plug your Atom RSS URL above into IFTTT as a Feed recipe.
- Now you can tell IFTTT to: “If new Feed item, then do something.” E.g., “If my garage door opens or closes, text me.”
Happy hacking 🙂
aaron@secretsciencelab.com
Dear Secretsciencelab Team
Greetings for the day,
We feel glad to inform you that your website/blog titled (DIY Electric Imp WiFi Garage Opener + Sensor For Under $50 (Weekend Project) and hyperlink”http://secretsciencelab.com/diy-electric-imp-wifi-garage-opener-sensor-for-under-50-weekend-project/”) was covered by our teams on our website www.engineersgarage.com
The coverage can be locate at (DIY Electric Imp WiFi Garage Opener and hyperlink”https://www.engineersgarage.com/mygarage/diy-electric-imp-wifi-garage-opener”). You may use the same link to share the story among your community/followers.
If you would like to submit more recently developed projects for review, you may contact us using the link (DIY Reviews, https://www.engineersgarage.com/submit/diy-reviews).
About EG:Â
EngineersGarage is a community of Electrical & Electronics Engineers which interacts with each other to learn and share technical knowledge. We publish high quality technical content which includes experiments, circuit design, tutorials and articles for the electronics fraternity. Our community comprises of active and loyal audience of design, research and product engineers from industries and hobbyists.Â
Key Statistics
1. EG currently serves 550k+ visitors every month.Â
2. Registered database of 300k+ engineers.
3. 1Million+ followers on Facebook.
Best Wishes
Team EG