Detect Motion with PIR sensor and NodeMCU

 Motion detection is a critical aspect of security systems and automation projects. Passive Infrared (PIR) sensors are widely used for detecting motion due to their reliability, cost-effectiveness, and ease of integration. This blog post explores a project that utilizes a PIR sensor to detect motion and trigger an alert. The PIR sensor detects the infrared radiation emitted by objects in its field of view, making it ideal for detecting human presence.

Project Motivation

The primary motivation for this project is to create a simple yet effective motion detection system using a PIR sensor. This system can be employed in various applications such as home security, automatic lighting systems, and presence detection. By integrating the PIR sensor with a microcontroller like Arduino or ESP32 or NodeMCU and other components, we can build a functional motion detection system that can alert users when motion is detected​.

Understanding PIR Sensors

PIR sensors detect infrared radiation, which is emitted by all objects with a temperature above absolute zero. The sensor is passive because it does not emit any radiation itself but instead detects the infrared radiation emitted by objects. When an object, such as a human, moves within the sensor's field of view, the infrared radiation detected by the sensor changes, triggering the sensor to send an output signal.

PIR sensors consist of a pyroelectric sensor, which can detect levels of infrared radiation, and a Fresnel lens, which focuses the infrared signals onto the sensor. The sensor outputs a digital signal when it detects motion, which can be used to trigger other devices or alerts​.

Components and Specifications

For this project, we will use the HC-SR501 PIR sensor, which is a popular choice for motion detection applications due to its high sensitivity and wide range.

PIR sensor component

 The specifications of the HC-SR501 PIR sensor are as follows:

  • Operating Voltage: 5V to 20V DC
  • Current Drain: <60uA
  • Detection Range: Up to 7 meters
  • Detection Angle: 120 degrees
  • Trigger Time: Adjustable (default 2 seconds)
  • Block Time: Adjustable (default 2.5 seconds)
  • Output Type: Digital (HIGH when motion is detected)​

Connection Details:

  • NodeMCU 3V3: PIR Sensor VCC
  • NodeMCU GND: PIR Sensor GND
  • NodeMCU D1: PIR Sensor OUT
  • NodeMCU D2: Buzzer +ve pin
  • NodeMCU GND: Buzzer -ve pin


connection between PIR and NodeMCU

Software Setup

Arduino IDE Code:

#include <ESP8266WiFi.h> //Library for WiFi functionalities
#include <Adafruit_MQTT_Client.h> //Library for Adafruit MQTT Client

//Adafruit Details
#define server ""
#define port 1883
#define IO_KEY       "AIO_KEY"

String ssid = "YOUR WIFI NAME"; //SSID of your WiFi
String password = "YOUR WIFI PASSWORD"; //Password of your WiFi

WiFiClient esp; //Create a WiFi Client

//Creation of Adafruit MQTT Client
Adafruit_MQTT_Client mqtt(&esp, server, port, IO_USERNAME, IO_KEY);
//Variable for publishing data to Adafruit Feed
Adafruit_MQTT_Publish feed = Adafruit_MQTT_Publish(&mqtt, IO_USERNAME"/feeds/pir");

int PIR = D6; //Initialize the PIR output pin
int LED = D4; //Initialize the LED pin
int PIR_val = 0; //Used for storing PIR output value

void setup()
  Serial.begin(115200); //Initialisation of NodeMCU Serial
  pinMode(PIR, INPUT); //Defining the direction of PIR sensor
  pinMode(LED, OUTPUT); //Defining the direction of LED
  //Connecting NodeMCU to WiFi
  Serial.print("Connecting to ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());  //Print the local IP address of NodeMCU

  //Connecting to MQTT
  Serial.print("Connecting to MQTT");
  while (mqtt.connect())

void loop()
  if(mqtt.connected()) {
    PIR_val = digitalRead(PIR); //Read the PIR output

    if(PIR_val) { //If movement is detected
      Serial.println("Movement Detected.");
      digitalWrite(LED, HIGH);

      //Publish data to the Adafruit feed
      if(feed.publish("Movement Detected")){
        Serial.println("Successfully data is Uploaded to the Adafruit.\n");
      } else
        Serial.println("Sorry, data cannot be Uploaded.\n");
    } else { //If movement is not detected
      Serial.println("No Movement Detected.");
      digitalWrite(LED, LOW);


The PIR sensor is a versatile component for motion detection projects. By combining the PIR sensor with a NodeMCU and a buzzer, we can create an effective motion detection system for security and automation purposes. This project demonstrates the simplicity and effectiveness of using PIR sensors in IoT applications. Start building your own motion detection system today to enhance security and automation in your home or workplace!

Post a Comment

Previous Post Next Post