Hi, I want to control Door Lock/Unlock from Blynk App. But whenever I connect my project, the relay will auto switch on and the solenoid door stay unlocked.
Problem:
The relay will always on and solenoid door stay unlocked.
The PIR Motion sensor always detect motion, ( but I do not really mind it because I just want it to show notification and then I will put delay for 30 mins for the demo notifications pop-up, but would appreciate anyone help to set pir motion sensor to detect properly)
Objective:
I want to control the solenoid door lock from phone via button, and when there is motion detected, a notification should appear from Blynk app.
#define BLYNK_PRINT Serial
/* Fill in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME "smart door"
#define BLYNK_AUTH_TOKEN ""
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
// Wi-Fi credentials
char ssid[] = "";
char pass[] = ";
const int RELAY_PIN = 16; // GPIO16 for the relay
const int PIR_PIN = 13; // GPIO13 for the PIR motion sensor
bool isLocked = true; // Initial lock state
BlynkTimer timer; // Use Blynk's non-blocking timer
// Function to check motion and send notification
void checkMotion() {
int pirState = digitalRead(PIR_PIN);
Blynk.virtualWrite(V0, pirState); // Send PIR state to Virtual Pin V0
if (pirState == HIGH) {
Serial.println("Motion detected!");
Blynk.logEvent("detectMotion", "Motion Detected Near the Door!");
}
}
BLYNK_WRITE(V1) { // Virtual pin V1 for lock control
int lockState = param.asInt(); // Get the value from the app (0 or 1)
if (lockState == 1) {
digitalWrite(RELAY_PIN, HIGH); // Unlock the door (Relay OFF)
Serial.println("Door Unlocked");
isLocked = false;
} else {
digitalWrite(RELAY_PIN, LOW); // Lock the door (Relay ON)
Serial.println("Door Locked");
isLocked = true;
}
}
void setup() {
Serial.begin(115200);
delay(10);
// Configure GPIO pins
pinMode(RELAY_PIN, OUTPUT);
pinMode(PIR_PIN, INPUT);
// Default state: Door locked (Relay ON)
digitalWrite(RELAY_PIN, LOW);
isLocked = true;
Serial.println("System Initialized: Door is Locked");
// Connect to Wi-Fi
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("\nWiFi Connected");
// Initialize Blynk
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
// Set up the motion check function to run every second
timer.setInterval(1000L, checkMotion);
}
void loop() {
Blynk.run();
timer.run(); // Run BlynkTimer
}
unfortunately, for now i did not add any transistors. But if necessary, i will add. I thought this will be simple project just like Youtube and Google show but I got lost haha…
Update: i have found the solution which is, i added a green LED with anode to the "IN" pin and cathode it to the 3.3V ESP32. Well, i am not good at explaining the logic behind, but it works and if anyone having the same problem, maybe these schematic would help you: