top of page
Writer's pictureRamesh G

IoT Based Motion Detector

Updated: Jul 2, 2021

In this tutorial. To learn how to interface PIR(Passive infrared) motion/intrusion detection sensor with nodemcu WiFi module, Buzzer alarm. PIR motion sensor will instantly detect the presence of it and rings a buzzer alarm to notify you about an intrusion.


The project can be taken as a small security system and could be installed in garage, garden(to check the presence of wild animals dogs, cats, pigs, birds etc), house, pet house, shops and stores. Live body emits heat waves and heat waves contains infrared rations. The more the body emitting heat the more the infrared radiations. Generally the living bodies emit more heat than the raw materials. So pir sensor can be used to detect the motion of living bodies.


Circuit Diagram


Components Required

  • NodeMCU ESP8266 12E

  • Motion Sensor HC-SR501 (PIR Sensor)

  • LCD 16x2 with I2C Module

  • Buzzer

  • Jumper Wires


PIR motion sensor

The HC-SR501 module uses a passive infrared sensor and an integrated circuit (IC) to detect motion. It features adjustable sensitivity that allows for a motion detection range from 3 meters to 7 meters. The module also includes time delay adjustments and trigger selection for fine tuning.

Connecting the PIR sensor

The PIR sensor typically has 3 leads attached: VCC, OUT and GND.

  • Connect the VCC PIR sensor pin to a 3v3 pin on the NodeMCU.

  • Connect the GND PIR sensor pin to a GND pin on the NodeMCU.

  • Connect the OUT PIR sensor pin to a D0 pin on the NodeMCU.Active Passive Buzzer

Active Passive Buzzer

Positive: Identified by (+) symbol or longer terminal lead. Can be Connected to NodeMCU pin D4.

Negative: Identified by short terminal lead. Typically connected to the ground of the circuit.


Buzzer Features and Specifications

  • Rated Voltage: 6V DC

  • Operating Voltage: 4-8V DC

  • Rated current: <30mA

  • Sound Type: Continuous Beep

  • Resonant Frequency: ~2300 Hz

  • Small and neat sealed package

  • Breadboard and Perf board friendly






LCD 16x2 (I2C module)

This is a 16x2 LCD display screen with I2C interface. It is able to display 16x2 characters on 2 lines, white characters on blue background.

This I2C 16x2 Arduino LCD Screen is using an I2C communication interface. It means it only needs 4 pins for the LCD display: VCC, GND, SDA, SCL.

Connecting the LCD to NodeMCU


  • Connect the VCC I2C pin to a Vin pin on the NodeMCU.

  • Connect the GND I2C pin to a GND pin on the NodeMCU.

  • Connect the SCL I2C pin to a D1 pin on the NodeMCU.

  • Connect the SDA I2C pin to a D2 pin on the NodeMCU.


Open Arduino IDE. Make sure you have the Nodemcu 1.0 ESP-12E board selected, and then, Copy and Paste code in Arduino IDE.

Subscribe and Download code.

Arduino Code

#include <ESP8266WiFi.h>

#include <WiFiClient.h>

#include <ESP8266WebServer.h>

const char* ssid = "TP-Link_3200"; // your SSID

const char* password = "9500112137"; //your WIFI Password


#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x27,16,2);


ESP8266WebServer server(80); //Server on port 80


int buzzer = 2; //Buzzer alarm connected to GPIO-14 or D5 of nodemcu

int PIRsensor = 16; //PIR sensor output connected to GPIO-5 or D1 of nodemcu

String Message;

const char MAIN_page[] PROGMEM = R"=====(

<!doctype html>

<html>

<head>

<title>Data Logger</title>

<h1 style="text-align:center; color:#090902;">Iot Based Motion Detector</h1>

<h3 style="text-align:center;">Real Time Data Logger</h3>

<style>

canvas{

-moz-user-select: none;

-webkit-user-select: none;

-ms-user-select: none;

}

/* Data Table Styling*/

font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;

border-collapse: collapse;

width: 100%;

text-align: center;

}

border: 1px solid #090902;

padding: 8px;

}

#dataTable tr:nth-child(even){background-color: #e2e1e1;}

#dataTable tr:hover {background-color: #ffff4d;}

padding-top: 12px;

padding-bottom: 12px;

text-align: center;

background-color: #ffc04d;

color: white;

}

</style>

</head>

<body>

<div>

<table id="dataTable">

<tr><th>Time</th><th>Activity</th></tr>

</table>

</div>

<br>

<br>

<script>

var Avalues = [];

//var timeStamp = [];

var dateStamp = [];

setInterval(function() {

// Call a function repetatively with 5 Second interval

getData();

}, 3000); //5000mSeconds update rate

function getData() {

var xhttp = new XMLHttpRequest();

xhttp.onreadystatechange = function() {

if (this.readyState == 4 && this.status == 200) {

//Push the data in array

// var time = new Date().toLocaleTimeString();

var date = new Date();

var txt = this.responseText;

var obj = JSON.parse(txt);

Avalues.push(obj.Activity);

// timeStamp.push(time);

dateStamp.push(date);

//Update Data Table

var table = document.getElementById("dataTable");

var row = table.insertRow(1); //Add after headings

var cell1 = row.insertCell(0);

var cell2 = row.insertCell(1);

cell1.innerHTML = date;

//cell2.innerHTML = time;

cell2.innerHTML = obj.Activity;

}

};

xhttp.open("GET", "readData", true); //Handle readData server on ESP8266

xhttp.send();

}

</script>

</body>

</html>


)=====";

void handleRoot() {

String s = MAIN_page; //Read HTML contents

server.send(200, "text/html", s); //Send web page

}

void readData() {

lcd.init(); // initialize the lcd

// Print a message to the LCD.

lcd.backlight();

int state = digitalRead(PIRsensor); //Continuously check the state of PIR sensor

delay(500); //Check state of PIR after every half second

lcd.setCursor(0,0);

lcd.print("PIR Scanning >>>");

lcd.setCursor(0,1);

lcd.print("No Activity :) ");

Serial.print(state);

if(state == HIGH){

digitalWrite (buzzer, HIGH); //If intrusion detected ring the buzzer

lcd.setCursor(0,1);

lcd.print("Motion Detected!");

delay(1000);

digitalWrite (buzzer, LOW);

lcd.setCursor(0,1);

lcd.print("Wait Reading...");

delay(1000);

Message = "Motion Detected";

String data = "{\"Activity\":\""+ String(Message) +"\"}";

server.send(200, "text/plane", data); //Send ADC value, temperature and humidity JSON to client ajax request

Serial.println("Motion detected!");

}

}

void setup() {

Serial.begin(9600);

lcd.init(); // initialize the lcd

// Print a message to the LCD.

lcd.backlight();

Serial.print("Connecting to Wifi Network");

Serial.println(ssid);

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

delay(500);

Serial.print(".");

}

Serial.println("");

Serial.println("Successfully connected to WiFi.");

Serial.println("IP address is : ");

Serial.println(WiFi.localIP());

server.on("/", handleRoot); //Which routine to handle at root location. This is display page

server.on("/readData", readData); //This page is called by java Script AJAX

server.begin(); //Start server

Serial.println("HTTP server started");

pinMode(PIRsensor, INPUT); // PIR sensor as input

pinMode(buzzer, OUTPUT); // Buzzer alaram as output

digitalWrite (buzzer, LOW);// Initially buzzer off

}

void loop(){

server.handleClient(); //Handle client requests

}


Then, upload the code to your NodeMCU board. Make sure you have selected the right board and COM port. Also, make sure you’ve inserted your WiFi Credentials in the code.

After a successful upload, open the Serial Monitor at a baud rate of 9600. Press the “EN/RST” button on the ESP8266 board. Now it should print its IP address. After That Open web address and Type IP address in address bar and get result.





588 views0 comments

Comments


bottom of page