Course Content
IoT Engineering Course using ESP32 with 12 Real-World Projects

 


📘 Lesson 11.3 – Home Security System ( Bluetooth Motion Alert Version )


🎯 Objective of This Lesson

Students will learn:

  • How to enable Bluetooth in ESP32

  • How to send motion alert to mobile

  • How to monitor intrusion wirelessly

  • Real-time motion notification system

Only:

👉 ESP32
👉 PIR Sensor
👉 Mobile Bluetooth Terminal App


📱 Required Mobile App

Install:

  • Serial Bluetooth Terminal (Android)
    or

  • Any Bluetooth Terminal App


🔌 Hardware Reminder

PIR Pin ESP32 Pin
VCC 5V
GND GND
OUT GPIO 27

🧾 Complete ESP32 Code (Bluetooth Version – Copy & Paste)

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

#define PIR_PIN 27

int motionState = 0;
int lastState = 0;

void setup() {

Serial.begin(115200);
SerialBT.begin("Home_Security"); // Bluetooth device name

pinMode(PIR_PIN, INPUT);

Serial.println("Home Security System Started...");
SerialBT.println("Connected to Home Security System");

Serial.println("Warming up PIR Sensor...");
SerialBT.println("Warming up PIR Sensor...");
delay(30000);

Serial.println("System Ready!");
SerialBT.println("System Ready!");
}

void loop() {

motionState = digitalRead(PIR_PIN);

if (motionState == HIGH && lastState == LOW) {

Serial.println("⚠ MOTION DETECTED! Intruder Alert!");
SerialBT.println("⚠ MOTION DETECTED! Intruder Alert!");

lastState = HIGH;
}

else if (motionState == LOW && lastState == HIGH) {

Serial.println("Area Clear - No Motion");
SerialBT.println("Area Clear - No Motion");

lastState = LOW;
}

delay(200);
}

🧠 Code Explanation (Step-by-Step)


1️⃣ Include Bluetooth Library

 
#include “BluetoothSerial.h”
 

This enables Bluetooth communication in ESP32.


2️⃣ Create Bluetooth Object

 
BluetoothSerial SerialBT;
 

Used to send data to mobile device.


3️⃣ Start Bluetooth

 
SerialBT.begin(“Home_Security”);
 

Sets device name visible in Bluetooth list.

Mobile will detect:

👉 Home_Security


4️⃣ Warm-Up Delay

 
delay(30000);
 

PIR requires 30 seconds stabilization time.


5️⃣ Read PIR Output

 
motionState = digitalRead(PIR_PIN);
 

Returns:

HIGH → Motion detected
LOW → No motion


6️⃣ Motion Alert Logic

 
if (motionState == HIGH && lastState == LOW)
 

Motion just started → Send alert once.

Prevents repeated spam messages.


7️⃣ Motion End Detection

 
else if (motionState == LOW && lastState == HIGH)
 

Motion stopped → Send clear message.


8️⃣ Why Use Both Serial & SerialBT?

  • Serial → Computer debugging

  • SerialBT → Mobile monitoring

Professional development practice.


📱 How to Connect Mobile

1️⃣ Upload code
2️⃣ Turn ON Bluetooth
3️⃣ Open Bluetooth Terminal app
4️⃣ Search for device
5️⃣ Select Home_Security
6️⃣ Connect

Now motion alerts will appear on mobile.


📊 Example Bluetooth Output

System Ready!

⚠ MOTION DETECTED! Intruder Alert!

Area Clear – No Motion


🎓 Learning Outcomes

Students now understand:

  • Bluetooth communication

  • Wireless motion alert system

  • State change detection

  • Real-time intrusion monitoring



Scroll to Top