Build a Wemos D1 Mini smart garage door controller with a relay shield, two reed switches, an HC-SR04 car sensor, warning buzzer, auto-close and a PIN-protected web app. Wiring table, diagram and full code.
Need it ready-made? Buy Ready Project
This Wemos D1 Mini smart garage door controller lets you open and close your existing garage door from your phone, see whether the door is closed, open or moving, and check if the car is parked inside. It also beeps a warning before every remote close and can close the door on its own if someone forgets it open. Everything runs on the cheap ESP8266-based Wemos D1 Mini, with a relay shield, two magnetic reed switches, an HC-SR04 ultrasonic sensor and a buzzer.
It is a big combination build: Wi-Fi web server, relay control, position sensing, distance measurement and a safety timer all in one sketch. It works with almost any sectional or roller garage door opener that has a wall push button, because the relay simply “presses” that button for half a second. It does not touch the opener’s mains wiring.

How It Works
- Opener control: most openers start, stop or reverse the motor when their two push-button wires are shorted. The relay contacts (COM and NO) are wired in parallel with that button, and the code closes them for 500 ms each time you tap the button on the web page.
- Door position: one reed switch is fixed where the door rests when fully closed and another where it rests when fully open. A magnet on the door closes each switch, pulling D5 or D6 LOW. Neither switch active means the door is moving or stopped half way.
- Car detection: the HC-SR04 is mounted on the ceiling, facing down. Once a second the code measures the distance. A reading under 80 cm (adjustable) means the roof or bonnet of a car is under the sensor.
- Safety warning: before every remote close and every auto-close, the active buzzer beeps for about 3 seconds so anyone under the door has time to move. Your opener’s own photo-eye safety sensors keep working as normal.
- Auto-close: if the door stays fully open for more than 10 minutes (set by
AUTO_CLOSE_MIN), the controller warns and closes it. - Web app: the D1 Mini serves a phone-friendly page at its IP address or at
http://garage.local. The page refreshes the status every 2 seconds, and the Open/Close button only works with the correct PIN.
Components Required
- Wemos D1 Mini (ESP8266) – 1
- Wemos D1 Mini relay shield (relay on D1) – 1, or a 3.3V-compatible 1-channel relay module
- Magnetic reed switches (door/window contact type, normally open) – 2
- HC-SR04 ultrasonic distance sensor – 1
- Active buzzer (3.3V–5V) – 1
- Resistors: 1k – 1, 2k (or 2.2k) – 1, for the ECHO voltage divider
- 5V 2 A USB adapter and USB cable
- Two-core cable (bell wire) to the opener’s push-button terminals and to the reed switches
- Mini breadboard or small perfboard, jumper wires and a plastic enclosure
Wiring Table
| Part / pin | Wemos D1 Mini pin | Notes |
|---|---|---|
| Relay shield signal | D1 (GPIO5) | Stack the shield, or wire IN of a relay module to D1 |
| Relay COM and NO | Opener push-button terminals | In parallel with the existing wall button; polarity does not matter |
| Reed switch (closed position) | D5 (GPIO14) and GND | Internal pull-up; LOW when the door is closed |
| Reed switch (open position) | D6 (GPIO12) and GND | LOW when the door is fully open |
| HC-SR04 VCC | 5V | |
| HC-SR04 GND | G (GND) | |
| HC-SR04 TRIG | D7 (GPIO13) | 3.3V trigger works fine |
| HC-SR04 ECHO | D8 (GPIO15) through 1k | 2k from D8 to GND drops the 5V echo to about 3.3V |
| Active buzzer (+) | D2 (GPIO4) | |
| Active buzzer (-) | G (GND) | |
| 5V USB adapter | USB socket | Powers the board, relay and sensor |
Connection Diagram
The illustration above shows the parts and how they fit together. It is a guide for placement only. The pin-accurate drawing below matches the wiring table exactly, so follow it while you build.
Code
Install the ESP8266 board package in the Arduino IDE (Boards Manager, search “esp8266”) and choose LOLIN(WEMOS) D1 R2 & mini. No extra libraries are needed. Replace the Wi-Fi placeholders and change the PIN before uploading. Open the Serial Monitor at 115200 baud to see the IP address, then open it (or http://garage.local) in your phone’s browser.
/*
Wemos D1 Mini Smart Garage Door Controller
- Relay shield pulses the opener's push-button input (open / stop / close)
- Two reed switches report the closed and fully open positions
- HC-SR04 ultrasonic sensor detects whether a car is parked inside
- Active buzzer warns before every remote close and before auto-close
- Web page (phone friendly) with live status and a PIN-protected button
- Auto-close if the door stays open longer than AUTO_CLOSE_MIN minutes
Board: LOLIN(WEMOS) D1 R2 & mini (ESP8266 core, no extra libraries)
*/
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
// ---------- User settings (placeholders) ----------
const char* WIFI_SSID = "YOUR_WIFI_SSID";
const char* WIFI_PASS = "YOUR_WIFI_PASSWORD";
const char* DOOR_PIN = "1234"; // change this PIN before use
const unsigned long AUTO_CLOSE_MIN = 10; // 0 = auto-close disabled
const int CAR_DISTANCE_CM = 80; // closer than this = car present
// ---------- Pins ----------
const uint8_t RELAY_PIN = D1; // GPIO5 - relay shield (active HIGH)
const uint8_t BUZZER_PIN = D2; // GPIO4 - active buzzer (+)
const uint8_t REED_CLOSED = D5; // GPIO14 - reed switch at closed position
const uint8_t REED_OPEN = D6; // GPIO12 - reed switch at fully open position
const uint8_t TRIG_PIN = D7; // GPIO13 - HC-SR04 TRIG
const uint8_t ECHO_PIN = D8; // GPIO15 - HC-SR04 ECHO (through 1k/2k divider)
ESP8266WebServer server(80);
enum DoorState { DOOR_CLOSED, DOOR_OPEN, DOOR_MOVING };
unsigned long openSince = 0; // when the door left the closed position
int carDistance = -1; // last ultrasonic reading in cm
unsigned long lastSonar = 0;
DoorState readDoor() {
bool closed = digitalRead(REED_CLOSED) == LOW; // magnet near = LOW
bool open = digitalRead(REED_OPEN) == LOW;
if (closed) return DOOR_CLOSED;
if (open) return DOOR_OPEN;
return DOOR_MOVING;
}
const char* doorText(DoorState s) {
return s == DOOR_CLOSED ? "Closed" : (s == DOOR_OPEN ? "Open" : "Moving / part open");
}
int readDistanceCm() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
unsigned long us = pulseIn(ECHO_PIN, HIGH, 30000UL); // 30 ms timeout (~5 m)
if (us == 0) return -1; // no echo
return us / 58; // microseconds to cm
}
// Beep a warning, then press the opener button for 500 ms
void pulseOpener(bool warnFirst) {
if (warnFirst) {
for (int i = 0; i < 6; i++) { // about 3 seconds of beeps
digitalWrite(BUZZER_PIN, HIGH); delay(250);
digitalWrite(BUZZER_PIN, LOW); delay(250);
}
}
digitalWrite(RELAY_PIN, HIGH);
delay(500);
digitalWrite(RELAY_PIN, LOW);
}
void handleRoot() {
String html = F("<!DOCTYPE html><html><head><meta name='viewport' content='width=device-width,initial-scale=1'>"
"<title>Garage Door</title><style>body{font-family:Arial;text-align:center;background:#f2f4f7;margin:0;padding:20px}"
".card{background:#fff;border-radius:14px;padding:20px;max-width:360px;margin:auto;box-shadow:0 2px 8px #0002}"
"h1{font-size:22px}.s{font-size:28px;font-weight:bold;margin:10px}input{font-size:18px;padding:8px;width:120px;text-align:center}"
"button{font-size:20px;padding:14px 30px;margin-top:14px;border:0;border-radius:10px;background:#1565c0;color:#fff}</style></head><body>"
"<div class='card'><h1>Garage Door</h1><div class='s' id='d'>...</div><div id='c'>...</div><div id='t'></div>"
"<input id='p' type='password' placeholder='PIN'><br><button onclick='go()'>Open / Close</button><div id='m'></div></div>"
"<script>function u(){fetch('/status').then(r=>r.json()).then(j=>{d.textContent=j.door;"
"c.textContent='Car inside: '+(j.car?'Yes':'No')+(j.cm>0?' ('+j.cm+' cm)':'');"
"t.textContent=j.openMin>=0?'Open for '+j.openMin+' min':''})}"
"function go(){fetch('/toggle?pin='+encodeURIComponent(p.value)).then(r=>r.text()).then(x=>{m.textContent=x;u()})}"
"u();setInterval(u,2000);</script></body></html>");
server.send(200, "text/html", html);
}
void handleStatus() {
DoorState s = readDoor();
long openMin = (s == DOOR_CLOSED) ? -1 : (long)((millis() - openSince) / 60000UL);
bool car = carDistance > 0 && carDistance < CAR_DISTANCE_CM;
String json = String("{\"door\":\"") + doorText(s) + "\",\"car\":" + (car ? "true" : "false") +
",\"cm\":" + carDistance + ",\"openMin\":" + openMin + "}";
server.send(200, "application/json", json);
}
void handleToggle() {
if (server.arg("pin") != DOOR_PIN) {
server.send(403, "text/plain", "Wrong PIN");
return;
}
bool closing = readDoor() != DOOR_CLOSED; // warn people before closing
server.send(200, "text/plain", closing ? "Closing (warning beeps)" : "Opening");
pulseOpener(closing);
}
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT); digitalWrite(RELAY_PIN, LOW);
pinMode(BUZZER_PIN, OUTPUT); digitalWrite(BUZZER_PIN, LOW);
pinMode(REED_CLOSED, INPUT_PULLUP);
pinMode(REED_OPEN, INPUT_PULLUP);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT); // D8 has an on-board pull-down
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASS);
Serial.print("Connecting to Wi-Fi");
while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); }
Serial.print("\nOpen http://"); Serial.println(WiFi.localIP());
if (MDNS.begin("garage")) Serial.println("or http://garage.local");
server.on("/", handleRoot);
server.on("/status", handleStatus);
server.on("/toggle", handleToggle);
server.begin();
if (readDoor() != DOOR_CLOSED) openSince = millis();
}
void loop() {
server.handleClient();
MDNS.update();
static DoorState last = DOOR_CLOSED;
DoorState now = readDoor();
if (now != last) { // door started opening
if (last == DOOR_CLOSED) openSince = millis();
Serial.printf("Door: %s\n", doorText(now));
last = now;
}
if (millis() - lastSonar > 1000) { // read the car sensor once a second
lastSonar = millis();
carDistance = readDistanceCm();
}
// Auto-close only when the door is fully open and has been for too long
if (AUTO_CLOSE_MIN > 0 && now == DOOR_OPEN &&
millis() - openSince > AUTO_CLOSE_MIN * 60000UL) {
Serial.println("Auto-close");
pulseOpener(true);
openSince = millis(); // wait a full period before retrying
}
}
Installation and Testing
- Test on the bench first: connect a small LED to the relay contacts with a battery, open the page and check that the relay clicks for half a second after the warning beeps.
- Find the opener’s push-button terminals (usually two screw terminals marked PB, PUSH or WALL). Connect the relay COM and NO there, alongside the existing wall button wires.
- Stick the closed-position reed switch on the frame and its magnet on the door so they meet when the door is shut. Repeat for the fully-open position near the top of the track.
- Mount the HC-SR04 on the ceiling above where the car bonnet or roof sits. Check the distance on the page with and without the car and adjust
CAR_DISTANCE_CM. - Keep the D1 Mini near a good Wi-Fi signal, away from the opener motor, and inside a plastic box.
Troubleshooting
| Problem | Fix |
|---|---|
| Relay clicks but the door does not move | Make sure the relay is on the push-button terminals, not the photo-eye terminals. Some openers with coded wall panels need a spare remote’s button wired to the relay instead. |
| Status always shows “Moving” | Bring the magnet closer to the reed switch (under 15 mm) or check the switch is normally open, not normally closed. |
| Distance reads -1 | Check the ECHO divider and TRIG wiring, and that the sensor faces a flat surface less than 4 m away. |
| Board will not boot | D8 must not be pulled HIGH at power-up. Keep the 2k resistor from D8 to GND and check the ECHO wire. |
| Page does not load | Use the IP address from the Serial Monitor; some phones do not support .local names. |
Safety Notes
- Never remove or bypass the opener’s own safety photo-eyes or obstacle reversal. This project only adds a remote button.
- Keep the web page on your home network. Do not port-forward it to the internet without proper authentication.
- Change the default PIN in the code before installing.
Ideas to Extend
- Send a phone alert when the door is left open, using a free push service such as ntfy.
- Add a DHT22 to show the garage temperature and humidity on the same page.
- Turn on a garage light relay automatically for 2 minutes whenever the door opens.
5 thoughts on “Wemos D1 Mini Smart Garage Door Controller with Reed Switches, Ultrasonic Car Sensor & Web App”