Difficulty: Intermediate
Time Required: 2 hours
Cost: $15-20
ESP Board: ESP32
What You’ll Need
| Component | Cost | Where to Buy |
|---|---|---|
| ESP32 Dev Board | $5-8 | Amazon |
| 5V Relay Module | $3-5 | Amazon |
| Current Sensor (Optional) | $3-5 | Amazon |
| Jumper Wires | $2 | Amazon |
Circuit Diagram
| ESP32 Pin | Component |
|---|---|
| 5V | Relay VCC |
| GND | Relay GND |
| GPIO 26 | Relay IN |
| GPIO 25 | Button (for manual control) |
The Complete Code
/*********************************************************************
* ESP32 Smart Garage Door Opener
*
* Control garage door via WiFi and Telegram
* Hardware: ESP32, 5V Relay, Push Button
*********************************************************************/
#include
#include
#include
// ============== WIFI ==============
const char* wifi_ssid = "YOUR_WIFI";
const char* wifi_password = "YOUR_PASSWORD";
// ============== TELEGRAM ==============
#define BOT_TOKEN "YOUR_BOT_TOKEN"
#define CHAT_ID "YOUR_CHAT_ID"
WiFiClientSecure client;
UniversalTelegramBot bot(BOT_TOKEN, client);
// ============== PINS ==============
#define RELAY_PIN 26
#define BUTTON_PIN 25
#define LED_PIN 2
// ============== CONFIG ==============
unsigned long lastTrigger = 0;
const unsigned long DOOR_TIME = 1000; // 1 second pulse
bool doorState = false; // false = closed, true = open
void setup() {
Serial.begin(115200);
pinMode(RELAY_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
digitalWrite(RELAY_PIN, HIGH); // Relay OFF
digitalWrite(LED_PIN, LOW);
connectToWiFi();
client.setCACert(TELEGRAM_CERTIFICATE_ROOT);
Serial.println(F("Garage Door Opener Active!"));
}
void loop() {
// Check manual button
if (digitalRead(BUTTON_PIN) == LOW) {
toggleDoor();
delay(500); // Debounce
}
// Check for Telegram commands
handleTelegram();
delay(100);
}
void toggleDoor() {
unsigned long now = millis();
if (now - lastTrigger < 5000) {
return; // Prevent rapid triggers
}
lastTrigger = now;
Serial.println(F("Toggling door..."));
// Pulse relay
digitalWrite(RELAY_PIN, LOW);
delay(DOOR_TIME);
digitalWrite(RELAY_PIN, HIGH);
// Flash LED
for (int i = 0; i < 3; i++) {
digitalWrite(LED_PIN, HIGH);
delay(200);
digitalWrite(LED_PIN, LOW);
delay(200);
}
String status = doorState ? "CLOSED" : "OPEN";
doorState = !doorState;
bot.sendMessage(CHAT_ID, "Garage door is now " + status, "");
}
void handleTelegram() {
int numNewMessages = bot.getUpdates(bot.last_message_received + 1);
for (int i = 0; i < numNewMessages; i++) {
String chat_id = String(bot.messages[i].chat_id);
String text = bot.messages[i].text;
if (chat_id == CHAT_ID) {
if (text == "/open" || text == "/close" || text == "/toggle") {
toggleDoor();
bot.sendMessage(chat_id, "Door command received!", "");
} else if (text == "/status") {
String status = doorState ? "OPEN" : "CLOSED";
bot.sendMessage(chat_id, "Garage door is " + status, "");
} else if (text == "/start") {
bot.sendMessage(chat_id, "Garage Door Bot\n/open - Open door\n/close - Close door\n/status - Check status", "");
}
}
}
}
void connectToWiFi() {
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(F("."));
}
Serial.println(F(" Connected!"));
}
Official Documentation
Safety Warning
⚠️ Important: When controlling AC garage doors, ensure proper isolation between ESP32 and door wiring. Use an optocoupler for the relay control circuit.

