Yarana Iot Guru
Published © MIT

Building a Home Motion Detection System: A DIY Guide

Detect motion using PIR sensor & ESP32 with instant alerts on Serial or Web dashboard. Smart Security made simple by YaranaIoT Guru!

BeginnerFull instructions provided8 hours67
Building a Home Motion Detection System: A DIY Guide

Things used in this project

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Code

💻 Code Example

C/C++
/*
  Home Motion Detection System with ESP32 and PIR Sensor
  Author: YaranaIoT Guru
*/

#include <WiFi.h>
#include <WebServer.h>

#define PIR_PIN 14
#define LED_PIN 27

const char* ssid = "YourWiFiName";
const char* password = "YourWiFiPassword";

WebServer server(80);
bool motionDetected = false;

void handleRoot() {
  String html = "<html><head><meta http-equiv='refresh' content='3'>";
  html += "<style>body{font-family:Arial;text-align:center;}</style>";
  html += "<h2>ESP32 Motion Detection</h2>";
  html += "<p>Status: ";
  html += (motionDetected ? "<b style='color:red;'>Motion Detected!</b>" : "<b style='color:green;'>No Motion</b>");
  html += "</p><p><small>by YaranaIoT Guru</small></p></html>";
  server.send(200, "text/html", html);
}

void setup() {
  Serial.begin(115200);
  pinMode(PIR_PIN, INPUT);
  pinMode(LED_PIN, OUTPUT);

  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected! IP: ");
  Serial.println(WiFi.localIP());

  server.on("/", handleRoot);
  server.begin();
}

void loop() {
  int motion = digitalRead(PIR_PIN);

  if (motion == HIGH) {
    motionDetected = true;
    digitalWrite(LED_PIN, HIGH);
    Serial.println("⚠️ Motion Detected!");
  } else {
    motionDetected = false;
    digitalWrite(LED_PIN, LOW);
  }

  server.handleClient();
  delay(200);
}

Credits

Yarana Iot Guru
35 projects • 2 followers
Yarana Iot Guru Yarana IoT Guru: Arduino, ESP32, GSM, NodeMCU & more. Projects, Tutorials & App Development. Innovate with us!
Thanks to YaranaIoT Guru.

Comments