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 (Blynk IOT)

ESP32 + Servo + Solenoid Lock + 1-Channel Relay

βœ… ESP32 code (Servo + Relay + Solenoid)
βœ… Full code explanation
βœ… Blynk Dashboard setup
βœ… Virtual Pins (V0, V1 etc.)
βœ… Buttons + Status display


βœ… 1) What You Will Control in Blynk

We will control:

πŸ”˜ Button 1 (V0)

  • ON = Unlock

  • OFF = Lock

πŸ“Œ Status (V1)

  • Shows β€œLOCKED / UNLOCKED”


βœ… 2) Blynk Setup (Step-by-Step)

πŸ”Ή Step 1: Create New Template

  1. Open Blynk IoT App

  2. Go to Developer Zone

  3. Click New Template

  4. Template Name: Smart Door Lock

  5. Hardware: ESP32

  6. Connection Type: WiFi

  7. Create


πŸ”Ή Step 2: Create Datastreams (Virtual Pins)

Go to your template β†’ Datastreams β†’ Add New:

βœ… Datastream 1 (Button)

  • Name: Door Lock

  • Type: Virtual Pin

  • Pin: V0

  • Data Type: Integer

  • Min: 0

  • Max: 1

  • Default: 0

βœ… Datastream 2 (Status)

  • Name: Lock Status

  • Type: Virtual Pin

  • Pin: V1

  • Data Type: String


πŸ”Ή Step 3: Create Web Dashboard / Mobile Dashboard

Go to template β†’ Web Dashboard (or Mobile Dashboard):

Add Widget 1: Button

  • Type: Button

  • Datastream: Door Lock (V0)

  • Mode: Switch

  • ON = 1

  • OFF = 0

Add Widget 2: Label / Value Display

  • Type: Label

  • Datastream: Lock Status (V1)


πŸ”Ή Step 4: Create Device

  1. Go to Devices

  2. Add New Device

  3. Choose your Template

  4. Create

  5. Copy:
    βœ… Auth Token


βœ… 3) ESP32 Blynk CodeΒ 

Β 

#define BLYNK_PRINT Serial

// ------------ Blynk Credentials ------------
#define BLYNK_TEMPLATE_ID "PASTE_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "Smart Door Lock"
#define BLYNK_AUTH_TOKEN "PASTE_AUTH_TOKEN"

// ------------ Libraries ------------
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
#include <Servo.h>

// ------------ WiFi Credentials ------------
char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";

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

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

// Servo angles (change for your door)
int LOCK_ANGLE = 0;
int UNLOCK_ANGLE = 90;

// Door status
bool isUnlocked = false;

// ------------ Function Prototypes ------------
void lockDoor();
void unlockDoor();
void sendStatusToBlynk();

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

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

// Servo
lockServo.attach(SERVO_PIN);
lockServo.write(LOCK_ANGLE);

// Start Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

Serial.println("βœ… ESP32 Door Lock with Blynk Started!");

sendStatusToBlynk();
}

void loop() {
Blynk.run();
}

// ---------------- BLYNK BUTTON CONTROL ----------------
// V0 = Door Lock Button
BLYNK_WRITE(V0) {
int value = param.asInt();

if (value == 1) {
unlockDoor();
} else {
lockDoor();
}
}

// ---------------- FUNCTIONS ----------------

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

// Servo unlock
lockServo.write(UNLOCK_ANGLE);
delay(700);

// Relay ON (Solenoid ON)
digitalWrite(RELAY_PIN, HIGH);
delay(200);

isUnlocked = true;

sendStatusToBlynk();
}

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

// Relay OFF (Solenoid OFF)
digitalWrite(RELAY_PIN, LOW);
delay(200);

// Servo lock
lockServo.write(LOCK_ANGLE);
delay(700);

isUnlocked = false;

sendStatusToBlynk();
}

void sendStatusToBlynk() {
if (isUnlocked) {
Blynk.virtualWrite(V1, "UNLOCKED πŸ”“");
} else {
Blynk.virtualWrite(V1, "LOCKED πŸ”’");
}
}

βœ… 4) Code Explanation (Very Clear)


πŸ”Ή 1) Blynk Credentials

Β 
#define BLYNK_TEMPLATE_ID "..."
#define BLYNK_AUTH_TOKEN "..."

These connect your ESP32 to your Blynk project.


πŸ”Ή 2) WiFi Connection

Β 
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

This automatically:
βœ… connects WiFi
βœ… connects Blynk cloud


πŸ”Ή 3) Blynk Virtual Pin Control (V0)

Β 
BLYNK_WRITE(V0)

Whenever you press the Blynk button, ESP32 receives:

  • 1 β†’ unlock

  • 0 β†’ lock


πŸ”Ή 4) Sending Status to Blynk (V1)

Β 
Blynk.virtualWrite(V1, "LOCKED");

This sends door status text to your Blynk dashboard label widget.


πŸ”Ή 5) Unlock Process

Inside unlockDoor():

  1. Servo rotates to unlock angle

  2. Relay ON β†’ solenoid activates

  3. Status updated in app


πŸ”Ή 6) Lock Process

Inside lockDoor():

  1. Relay OFF

  2. Servo back to lock angle

  3. Status updated in app


⚠️ IMPORTANT NOTE (Relay Logic)

Some relay modules work reverse.

If your solenoid activates when it should be OFF:

Swap these in code:

Relay ON:

Β 
digitalWrite(RELAY_PIN, LOW);

Relay OFF:

Β 
digitalWrite(RELAY_PIN, HIGH);


Scroll to Top