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 (Bluetooth Terminal)

ESP32 + Servo + Solenoid Lock + 1-Channel Relay


πŸ“Œ Bluetooth Commands (From Mobile)

Open Bluetooth Terminal app and send:

Command Action
1 Unlock (Servo open + Solenoid ON)
0 Lock (Servo close + Solenoid OFF)
S Show status

βœ… ESP32 Bluetooth Door Lock CodeΒ 

Β 

Β 
#include <BluetoothSerial.h>
#include <Servo.h>

// ---------------- Bluetooth ----------------
BluetoothSerial SerialBT;

// ---------------- Pins ----------------
#define SERVO_PIN 13
#define RELAY_PIN 26

// ---------------- Servo ----------------
Servo lockServo;

// Servo angles (you can change)
int LOCK_ANGLE = 0;
int UNLOCK_ANGLE = 90;

// Lock status
bool isUnlocked = false;

void setup() {
  Serial.begin(115200);

  // Start Bluetooth
  SerialBT.begin("ESP32_DoorLock");  // Bluetooth Name
  Serial.println("Bluetooth Door Lock Started!");
  Serial.println("Pair with: ESP32_DoorLock");

  // Relay pin
  pinMode(RELAY_PIN, OUTPUT);
  digitalWrite(RELAY_PIN, LOW); // Relay OFF initially (solenoid OFF)

  // Servo attach
  lockServo.attach(SERVO_PIN);

  // Start in LOCK position
  lockServo.write(LOCK_ANGLE);
  isUnlocked = false;

  SerialBT.println("Welcome!");
  SerialBT.println("Send:");
  SerialBT.println("1 = Unlock");
  SerialBT.println("0 = Lock");
  SerialBT.println("S = Status");
}

void loop() {

  // Check if Bluetooth received something
  if (SerialBT.available()) {
    char cmd = SerialBT.read();  // read 1 character

    // Remove unwanted new line / 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 {
      SerialBT.println("❌ Invalid Command!");
      SerialBT.println("Use: 1 / 0 / S");
    }
  }
}

// ---------------- Functions ----------------

void unlockDoor() {
  SerialBT.println("πŸ”“ Unlocking Door...");

  // Step 1: Servo rotate to unlock latch
  lockServo.write(UNLOCK_ANGLE);
  delay(700);

  // Step 2: Turn ON relay (Solenoid ON)
  digitalWrite(RELAY_PIN, HIGH);
  delay(200);

  isUnlocked = true;

  SerialBT.println("βœ… Door Unlocked!");
}

void lockDoor() {
  SerialBT.println("πŸ”’ Locking Door...");

  // Step 1: Turn OFF relay (Solenoid OFF)
  digitalWrite(RELAY_PIN, LOW);
  delay(200);

  // Step 2: Servo rotate to lock latch
  lockServo.write(LOCK_ANGLE);
  delay(700);

  isUnlocked = false;

  SerialBT.println("βœ… Door Locked!");
}

void showStatus() {
  if (isUnlocked) {
    SerialBT.println("πŸ“Œ Status: Door is UNLOCKED πŸ”“");
  } else {
    SerialBT.println("πŸ“Œ Status: Door is LOCKED πŸ”’");
  }
}

βœ… Code Explanation (Very Simple & Clear)

πŸ”Ή 1) Libraries

Β 
#include <BluetoothSerial.h>
#include <Servo.h>
  • BluetoothSerial.h β†’ makes ESP32 Bluetooth work like serial communication

  • Servo.h β†’ controls servo motor angle


πŸ”Ή 2) Bluetooth Start

Β 
SerialBT.begin("ESP32_DoorLock");

This creates Bluetooth device name:

πŸ“Œ ESP32_DoorLock
You will see this name in phone Bluetooth list.


πŸ”Ή 3) Pins Used

Β 
#define SERVO_PIN 13
#define RELAY_PIN 26
  • Servo signal connected to GPIO 13

  • Relay IN connected to GPIO 26


πŸ”Ή 4) Servo Lock Angles

Β 
int LOCK_ANGLE = 0;
int UNLOCK_ANGLE = 90;

You can change these depending on your lock setup.

Example:

  • If your servo rotates wrong direction:

    • change 90 to 180

    • or change 0 to 180


πŸ”Ή 5) Relay Control

Β 
digitalWrite(RELAY_PIN, HIGH);

Relay ON β†’ Solenoid ON β†’ lock pin moves.

Β 
digitalWrite(RELAY_PIN, LOW);

Relay OFF β†’ Solenoid OFF.


πŸ”Ή 6) Bluetooth Command Reading

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

This reads the command from your phone.


πŸ”Ή 7) Unlock Function

Β 
void unlockDoor()

It does:

  1. Servo rotates to unlock latch

  2. Relay ON β†’ solenoid activates

  3. Door unlocks


πŸ”Ή 8) Lock Function

Β 
void lockDoor()

It does:

  1. Relay OFF β†’ solenoid stops

  2. Servo rotates back to lock angle

  3. Door locks


πŸ“± How to Use Bluetooth Terminal App

Use this app:

βœ… Serial Bluetooth Terminal (Android)

Steps:

  1. Upload code to ESP32

  2. Turn ON Bluetooth in phone

  3. Pair with ESP32_DoorLock

  4. Open app β†’ connect

  5. Send command:

    • 1 unlock

    • 0 lock


⚠️ IMPORTANT NOTES (Must Read)

βœ… Servo Power

  • Servo must get 5V external

  • Do not use ESP32 3.3V

βœ… Relay Power

  • Relay VCC = 5V

  • Relay GND = ESP32 GND

βœ… Solenoid Power

  • Solenoid uses 12V

  • Never connect solenoid directly to ESP32

Scroll to Top