ESP8266 module (Wemos or Nodemcu)with PIR Motion Sensor

Tharindu Darshana Peiris
2 min readFeb 5, 2019

--

In this project you are going to create a simple circuit ESP8266 module and PIR Motion Sensor which will find movement .

An alarm will ON ,when the movement is detected. This sensor will be ideal to detect nearby human motion in a given range

— Brief Introduction with PIR Motion Sensor —

PIR meaning is passive infrared sensor.

PIR sensor is an electronic sensor ,that measures infrared (IR) lighting radiating from object in its field of view. They are most of used in PIR-based.

This motion sensor is used to sense movement of animals, people or other objects.

They commonly used in burglar alarms and automatically ON /OFF lighting systems.

How to connect PIR Sensor to ESP8266 module

The sensor has only three pins.

VCC → connect to 3v or 5v line.

GND →connect to Ground line.

OUT →connect to ESP digital pin.(D3)

The buzzer has two wire

RED → connect to digital pin in ESP module .(D4)

BLACK →connect to Ground pin.

Code →

int buzzer = D4; // the pin that the buzzer is atteched to
int sensor =D3; // the pin that the sensor is atteched to
int state = LOW; // by default, no motion detected
int val = 0; // variable to store the sensor status (value)

void setup() {
pinMode(buzzer, OUTPUT); // initalize buzzer as an output
pinMode(sensor, INPUT); // initialize sensor as an input
Serial.begin(9600); // initialize serial
}

void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(buzzer, HIGH); // turn buzzer ON
delay(100); // delay 100 milliseconds

if (state == LOW) {
Serial.println(“motion detected!!!!!”);
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(buzzer, LOW); // turn buzzer OFF
delay(100); // delay 200 milliseconds

if (state == HIGH){
Serial.println(“motion stopped!!!!!!”);
state = LOW; // update variable state to LOW
}
}
}

After uploading the above code to the ESP module, it will be programmed to ring a buzzer in response to nearby human motions.
You can use this system for the security purposes in you home and your workplaces as well……

--

--