β 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
-
Open Blynk IoT App
-
Go to Developer Zone
-
Click New Template
-
Template Name:
Smart Door Lock -
Hardware:
ESP32 -
Connection Type:
WiFi -
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
-
Go to Devices
-
Add New Device
-
Choose your Template
-
Create
-
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
These connect your ESP32 to your Blynk project.
πΉ 2) WiFi Connection
This automatically:
β
connects WiFi
β
connects Blynk cloud
πΉ 3) Blynk Virtual Pin Control (V0)
Whenever you press the Blynk button, ESP32 receives:
-
1β unlock -
0β lock
πΉ 4) Sending Status to Blynk (V1)
This sends door status text to your Blynk dashboard label widget.
πΉ 5) Unlock Process
Inside unlockDoor():
-
Servo rotates to unlock angle
-
Relay ON β solenoid activates
-
Status updated in app
πΉ 6) Lock Process
Inside lockDoor():
-
Relay OFF
-
Servo back to lock angle
-
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: