β ESP32 Smart Door Lock (Serial Monitor Control)
ESP32 + Servo + Solenoid Lock + 1-Channel Relay
β Commands in Serial Monitor
Open Serial Monitor and send:
| Command | Action |
|---|---|
1 |
Unlock |
0 |
Lock |
S |
Status |
π Select:
β
115200 baud
β
Newline = No line ending (recommended)
β ESP32 Door Lock Code (Serial Monitor Control)
#include <Servo.h>
// ---------------- Pins ----------------
#define SERVO_PIN 13
#define RELAY_PIN 26
// ---------------- Servo ----------------
Servo lockServo;
// Servo angles (change if needed)
int LOCK_ANGLE = 0;
int UNLOCK_ANGLE = 90;
// Door status
bool isUnlocked = false;
void setup() {
Serial.begin(115200);
// Relay pin setup
pinMode(RELAY_PIN, OUTPUT);
digitalWrite(RELAY_PIN, LOW); // Relay OFF initially
// Servo attach
lockServo.attach(SERVO_PIN);
// Start in LOCK position
lockServo.write(LOCK_ANGLE);
isUnlocked = false;
// Messages
Serial.println("==================================");
Serial.println(" ESP32 Smart Door Lock Started ");
Serial.println("==================================");
Serial.println("Send Commands in Serial Monitor:");
Serial.println("1 = Unlock");
Serial.println("0 = Lock");
Serial.println("S = Status");
Serial.println("----------------------------------");
}
void loop() {
// Check if serial data is available
if (Serial.available()) {
char cmd = Serial.read();
// ignore newline / carriage return / spaces
if (cmd == '\n' || cmd == '\r' || cmd == ' ') return;
Serial.print("Command Received: ");
Serial.println(cmd);
if (cmd == '1') {
unlockDoor();
}
else if (cmd == '0') {
lockDoor();
}
else if (cmd == 'S' || cmd == 's') {
showStatus();
}
else {
Serial.println("β Invalid Command!");
Serial.println("Use: 1 / 0 / S");
}
}
}
// ---------------- Functions ----------------
void unlockDoor() {
Serial.println("π Unlocking Door...");
// Step 1: Rotate servo to unlock position
lockServo.write(UNLOCK_ANGLE);
delay(700);
// Step 2: Turn ON relay (Solenoid ON)
digitalWrite(RELAY_PIN, HIGH);
delay(200);
isUnlocked = true;
Serial.println("β Door Unlocked!");
}
void lockDoor() {
Serial.println("π Locking Door...");
// Step 1: Turn OFF relay (Solenoid OFF)
digitalWrite(RELAY_PIN, LOW);
delay(200);
// Step 2: Rotate servo back to lock position
lockServo.write(LOCK_ANGLE);
delay(700);
isUnlocked = false;
Serial.println("β Door Locked!");
}
void showStatus() {
if (isUnlocked) {
Serial.println("π Status: Door is UNLOCKED π");
} else {
Serial.println("π Status: Door is LOCKED π");
}
}
β Code Explanation (Easy)
πΉ 1) Servo + Relay pins
-
Servo signal β GPIO 13
-
Relay IN β GPIO 26
πΉ 2) Serial Monitor Start
This starts serial communication with PC.
π Thatβs why we choose 115200 baud in Serial Monitor.
πΉ 3) Default Lock at Start
Means:
-
Servo stays locked
-
Relay OFF β Solenoid OFF
πΉ 4) Reading command from Serial Monitor
This checks if user typed anything and reads it.
πΉ 5) Unlock function
Unlock process:
-
Servo rotates to unlock latch
-
Relay ON β solenoid activates
-
Door unlocks
πΉ 6) Lock function
Lock process:
-
Relay OFF
-
Servo rotates back
-
Door locked