How to Build a Motion Detection System with PIR Sensor and NodeMCU

Motion detection is a critical feature in many security systems and automation projects. One of the most common sensors used for this purpose is the Passive Infrared (PIR) sensor. PIR sensors are reliable, cost-effective, and easy to integrate into your projects. In this tutorial, we will show you how to set up a motion detection system using a PIR sensor and NodeMCU. The system will trigger an alert when motion is detected, perfect for applications like home security and automatic lighting systems.

If you're new to working with NodeMCU or IoT, you can first check out our guide on setting up a basic NodeMCU Wi-Fi server, which will help you get started with Wi-Fi connectivity.

Understanding PIR Sensors

A PIR sensor detects infrared radiation emitted by objects, particularly humans, within its field of view. Since humans emit infrared radiation due to body heat, the sensor detects changes in this radiation as we move, triggering the sensor’s output. The sensor is passive, meaning it does not emit any radiation itself but detects the heat emitted by objects.

PIR sensor

Components of a PIR sensor include:

  • A pyroelectric sensor that detects infrared radiation.
  • A Fresnel lens that focuses infrared signals onto the sensor.

When the infrared radiation changes due to movement, the PIR sensor outputs a digital signal, typically HIGH, indicating that motion has been detected.

PIR sensor component

Components Needed

For this project, you will need:

  • NodeMCU (ESP8266)
  • HC-SR501 PIR Sensor
  • Buzzer
  • LED
  • Breadboard and jumper wires

PIR Sensor Specifications:

  • Operating Voltage: 5V to 20V DC
  • Current Drain: <60uA
  • Detection Range: Up to 7 meters
  • Detection Angle: 120 degrees
  • Output Type: Digital (HIGH when motion is detected)

Connection Details:

connection between PIR and NodeMCU

  • 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

Software Setup: Arduino IDE Code

The following code sets up the NodeMCU to connect to a Wi-Fi network, monitor the PIR sensor, and send motion detection alerts using MQTT.

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

// MQTT server details
#define server "io.adafruit.com"
#define port 1883
#define IO_USERNAME "AIO_USERNAME"
#define IO_KEY "AIO_KEY"

String ssid = "YOUR_WIFI_NAME"; // Your WiFi SSID
String password = "YOUR_WIFI_PASSWORD"; // Your WiFi Password

WiFiClient esp; // Create a WiFi Client

// Create an MQTT client
Adafruit_MQTT_Client mqtt(&esp, server, port, IO_USERNAME, IO_KEY);
// Create MQTT publish feed for PIR sensor data
Adafruit_MQTT_Publish feed = Adafruit_MQTT_Publish(&mqtt, IO_USERNAME"/feeds/pir");

int PIR = D6; // PIR sensor output pin
int LED = D4; // LED pin to indicate motion detection
int PIR_val = 0; // Store PIR output value

void setup() {
  Serial.begin(115200); // Initialize serial communication
  pinMode(PIR, INPUT); // Set PIR sensor as input
  pinMode(LED, OUTPUT); // Set LED as output

  // Connect to WiFi
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");

  // Connect to MQTT
  while (!mqtt.connect()) {
    delay(500);
    Serial.print(".");
  }
}

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

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

      // Publish motion data to Adafruit feed
      if (feed.publish("Movement Detected")) {
        Serial.println("Data uploaded to Adafruit.");
      } else {
        Serial.println("Data upload failed.");
      }
      delay(2000); // Delay to prevent multiple triggers
    } else { // No motion detected
      Serial.println("No Movement Detected.");
      digitalWrite(LED, LOW);
    }
  }
}

Key Points in the Code:

  • WiFi Connection: The NodeMCU connects to your Wi-Fi network using the provided SSID and password.
  • MQTT Setup: The Adafruit MQTT Client library is used to send motion detection data to Adafruit’s IoT platform. You’ll need to sign up for Adafruit and get your username and key to use their MQTT service.
  • PIR Sensor: The PIR sensor’s output is monitored in the loop(). When motion is detected, it triggers the LED and publishes the data to the MQTT feed.

Conclusion

By combining a PIR sensor with a NodeMCU, you can easily build a simple and effective motion detection system. This setup is ideal for security applications, automated lighting, and presence detection in IoT projects. You can further expand this project to include remote monitoring or even integrate it with other IoT devices.

If you're interested in optimizing power consumption in your IoT devices, check out our IoT Power Consumption Calculator here. For more on wireless range calculations for IoT systems, refer to our IoT Wireless Range Calculator here.

For more in-depth tutorials on NodeMCU and IoT projects, don’t forget to check out these related articles:

Post a Comment

Previous Post Next Post