Course Content
πŸ“˜ MODULE 4 – Bridge Health Monitoring System
0/2
πŸ“˜ MODULE 6 – Smart Pet Feeder
0/1
πŸ“˜ MODULE 7 – Smart Water Management System
0/2
πŸ“˜ MODULE 8 – Water Quality Monitoring System
0/2
πŸ“˜ MODULE 10 – Gas Leakage Detection System
0/2
Mastering IoT with 11 Real-World Projects and 1 mega project

βœ… 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

Β 
#define SERVO_PIN 13
#define RELAY_PIN 26
  • Servo signal β†’ GPIO 13

  • Relay IN β†’ GPIO 26


πŸ”Ή 2) Serial Monitor Start

Β 
Serial.begin(115200);

This starts serial communication with PC.

πŸ“Œ That’s why we choose 115200 baud in Serial Monitor.


πŸ”Ή 3) Default Lock at Start

Β 
lockServo.write(LOCK_ANGLE);
digitalWrite(RELAY_PIN, LOW);

Means:

  • Servo stays locked

  • Relay OFF β†’ Solenoid OFF


πŸ”Ή 4) Reading command from Serial Monitor

Β 
if (Serial.available()) {
char cmd = Serial.read();
}

This checks if user typed anything and reads it.


πŸ”Ή 5) Unlock function

Β 
unlockDoor();

Unlock process:

  1. Servo rotates to unlock latch

  2. Relay ON β†’ solenoid activates

  3. Door unlocks


πŸ”Ή 6) Lock function

Β 
lockDoor();

Lock process:

  1. Relay OFF

  2. Servo rotates back

  3. Door locked




Scroll to Top