IoT Home Intruder Detection System Using ESP8266/ESP32 and Blynk
Build an IoT home intruder detection system using ESP8266 or ESP32, PIR and IR sensors, and Blynk for movement detection, entry monitoring, security status, and remote intrusion alerts.
- ESP8266
- ESP32
- Nodemcu
- PIR Sensor
- IR Sensor
- Intruder Detection
- Home Security
- Blynk
- IOT
- Motion Detection
- Published
- Updated
- Reading time
- 4 min read
- Author
- Saroj Chaudhary
- Role
- IoT & Embedded Systems Engineer
This project uses a PIR motion sensor and an IR sensor with either an ESP8266 NodeMCU or ESP32 development board to detect movement or entry in a monitored area.
When either sensor is triggered, the controller updates the security status in Blynk and sends a remote intrusion notification to the owner’s phone.
The same blog and program can be used for both controllers. The main differences are the Wi-Fi/Blynk libraries, GPIO pin names, and Arduino IDE board selection, which are explained below.
Important: This is an educational IoT security prototype and should not replace a certified alarm or life-safety security system.
Components Required
| Component | Quantity |
|---|---|
| ESP8266 NodeMCU or ESP32 Dev Board | 1 |
| PIR Motion Sensor | 1 |
| IR Sensor Module | 1 |
| Breadboard | 1 |
| Jumper Wires | As required |
| USB Cable / Suitable Power Supply | 1 |
How It Works
PIR Sensor ----\
\
> ESP8266 / ESP32 ---> Wi-Fi ---> Blynk
/
IR Sensor -----/
The PIR sensor detects movement inside the monitored area.
The IR sensor can be positioned near a door, window, or entry point to detect an object or person passing through its detection area.
PIR triggered OR IR triggered
↓
Intrusion detected
↓
Security Status = ALERT
↓
Blynk Notification
ESP8266 and ESP32 Differences
The project logic is the same for both boards, but a few hardware and software settings are different.
| Item | ESP8266 | ESP32 |
|---|---|---|
| Wi-Fi Library | ESP8266WiFi.h |
WiFi.h |
| Blynk Library | BlynkSimpleEsp8266.h |
BlynkSimpleEsp32.h |
| PIR Pin | D5 / GPIO14 | GPIO27 |
| IR Pin | D6 / GPIO12 | GPIO25 |
| Board Example | NodeMCU 1.0 | ESP32 Dev Module |
ESP32 normally uses GPIO numbers directly, such as
27and25. Do not use NodeMCU labels such asD5orD6on a normal ESP32 board.
Circuit Connections
Option A: ESP8266 NodeMCU
| Device | ESP8266 |
|---|---|
| PIR OUT | D5 / GPIO14 |
| IR OUT | D6 / GPIO12 |
| PIR GND | GND |
| IR GND | GND |
| PIR VCC | Suitable sensor supply |
| IR VCC | Suitable sensor supply |
Option B: ESP32
| Device | ESP32 |
|---|---|
| PIR OUT | GPIO27 |
| IR OUT | GPIO25 |
| PIR GND | GND |
| IR GND | GND |
| PIR VCC | Suitable sensor supply |
| IR VCC | Suitable sensor supply |
Always connect the sensor grounds and microcontroller ground together.
Before connecting a sensor output to the ESP8266 or ESP32, verify that the sensor output voltage is safe for the 3.3V GPIO input.
Arduino IDE Setup
For ESP8266
Select:
Tools
→ Board
→ ESP8266 Boards
→ NodeMCU 1.0 (ESP-12E Module)
The program will automatically use:
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
and:
PIR -> D5
IR -> D6
For ESP32
Select:
Tools
→ Board
→ ESP32 Arduino
→ ESP32 Dev Module
The program will automatically use:
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
and:
PIR -> GPIO27
IR -> GPIO25
Install the Blynk library from Arduino IDE Library Manager before compiling.
Blynk Setup
Create these Virtual Pin datastreams:
| Datastream | Virtual Pin | Purpose |
|---|---|---|
| PIR Motion | V0 | Shows motion status |
| IR Entry | V1 | Shows IR sensor status |
| Security Status | V2 | Shows normal or alert state |
Suggested values:
0 -> Normal
1 -> Detected / Alert
Also create a Blynk Event with this exact event code:
intruder_alert
Enable notification for the event in the Blynk Console.
Code for Both ESP8266 and ESP32
The program below automatically selects the correct library and GPIO configuration depending on whether it is compiled for ESP8266 or ESP32.
#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "YOUR_TEMPLATE_ID"
#define BLYNK_TEMPLATE_NAME "Home Intruder Detection"
#define BLYNK_AUTH_TOKEN "YOUR_AUTH_TOKEN"
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
const int PIR_PIN = D5; // GPIO14
const int IR_PIN = D6; // GPIO12
#elif defined(ESP32)
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
const int PIR_PIN = 27;
const int IR_PIN = 25;
#else
#error "Select an ESP8266 or ESP32 board in Arduino IDE."
#endif
char ssid[] = "YOUR_WIFI_NAME";
char pass[] = "YOUR_WIFI_PASSWORD";
// Many IR sensor modules are active LOW.
// Change to false if your sensor behaves in the opposite way.
const bool IR_ACTIVE_LOW = true;
// Prevent continuous notification while a sensor remains triggered.
const unsigned long ALERT_COOLDOWN = 30000;
unsigned long lastAlertTime = 0;
BlynkTimer timer;
bool readIR()
{
int state = digitalRead(IR_PIN);
if (IR_ACTIVE_LOW)
return state == LOW;
return state == HIGH;
}
void checkSecurity()
{
bool motionDetected = digitalRead(PIR_PIN) == HIGH;
bool entryDetected = readIR();
bool intrusion = motionDetected || entryDetected;
if (Blynk.connected())
{
Blynk.virtualWrite(V0, motionDetected ? 1 : 0);
Blynk.virtualWrite(V1, entryDetected ? 1 : 0);
Blynk.virtualWrite(V2, intrusion ? 1 : 0);
}
Serial.print("PIR: ");
Serial.print(motionDetected ? "DETECTED" : "NORMAL");
Serial.print(" | IR: ");
Serial.print(entryDetected ? "DETECTED" : "NORMAL");
Serial.print(" | Security: ");
Serial.println(intrusion ? "ALERT" : "NORMAL");
if (intrusion)
{
if (lastAlertTime == 0 ||
millis() - lastAlertTime >= ALERT_COOLDOWN)
{
if (Blynk.connected())
{
Blynk.logEvent(
"intruder_alert",
"Movement or entry detected by the security system."
);
}
lastAlertTime = millis();
}
}
}
void setup()
{
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
pinMode(IR_PIN, INPUT);
Blynk.begin(
BLYNK_AUTH_TOKEN,
ssid,
pass
);
timer.setInterval(500L, checkSecurity);
Serial.println();
Serial.println("IoT Intruder Detection System Started");
#if defined(ESP8266)
Serial.println("Controller: ESP8266");
#elif defined(ESP32)
Serial.println("Controller: ESP32");
#endif
}
void loop()
{
Blynk.run();
timer.run();
}
Why the Same Code Works on Both Boards
This part:
#if defined(ESP8266)
is compiled only when an ESP8266 board is selected.
This part:
#elif defined(ESP32)
is compiled only when an ESP32 board is selected.
Therefore the correct:
- Wi-Fi library
- Blynk library
- PIR GPIO
- IR GPIO
are selected automatically.
You only need to choose the correct board in Arduino IDE before uploading.
Testing
Test 1: PIR Motion
Move in front of the PIR sensor.
Expected result:
PIR Motion -> 1
Security Status -> ALERT
Blynk notification -> sent
Test 2: IR Entry
Trigger the IR sensor.
Expected result:
IR Entry -> 1
Security Status -> ALERT
Blynk notification -> sent
Test 3: Normal Condition
When neither sensor is triggered:
PIR Motion -> 0
IR Entry -> 0
Security Status -> NORMAL
Notification Cooldown
The program uses:
const unsigned long ALERT_COOLDOWN = 30000;
This means another notification will not be sent for approximately 30 seconds after an alert.
This prevents the owner’s phone from receiving continuous notifications while a person remains in front of the sensor.
Common Problems
PIR Always Shows Motion
PIR sensors may need a short stabilization period after power-up.
Wait for the sensor to stabilize and adjust its sensitivity controls if available.
IR Sensor Works in Reverse
Change:
const bool IR_ACTIVE_LOW = true;
to:
const bool IR_ACTIVE_LOW = false;
ESP32 Compilation Error
Make sure an ESP32 board is selected in Arduino IDE and that the ESP32 board package is installed.
The ESP32 version uses:
#include <WiFi.h>
#include <BlynkSimpleEsp32.h>
ESP8266 Compilation Error
Make sure an ESP8266 NodeMCU board is selected and the ESP8266 board package is installed.
The ESP8266 version uses:
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
Blynk Notification Does Not Arrive
Check:
- Wi-Fi SSID and password
- Blynk Template ID
- Blynk Auth Token
intruder_alertevent code- event notification settings in Blynk
Final Result
The same project can now be built using either microcontroller:
ESP8266 NodeMCU
OR
ESP32 Dev Board
↓
PIR + IR Sensors
↓
Intrusion Detection
↓
Blynk Monitoring
↓
Remote Notification
Students learn how the same IoT application can be implemented on two different microcontroller platforms while adapting the required libraries and GPIO configuration.
Safety and Privacy Note
Install the system only in locations where you have permission to monitor activity.
For real home or commercial security, use properly designed alarm equipment with reliable backup power and communication methods.