HC-SR501 PIR Motion Sensor with ESP8266

MuhammadMuhammadESP82662 months ago23 Views

Introduction

The HC-SR501 PIR (Passive Infrared) Motion Sensor is one of the most widely used sensors for detecting human and animal movement. It works by detecting changes in infrared radiation (heat) emitted by living beings. When motion is detected, the sensor outputs a HIGH signal, which can be read by a microcontroller like the ESP8266.

By connecting the PIR sensor to the ESP8266, you can create smart motion detection systems such as security alarms, intruder alerts, and IoT-based motion monitoring projects.

In this tutorial, you will learn how to interface the HC-SR501 PIR motion sensor with ESP8266 and monitor motion detection using the Serial Monitor.

Components Required

  • ESP8266 (NodeMCU / Wemos D1 Mini)
  • HC-SR501 PIR Motion Sensor
  • Breadboard & Jumper Wires
  • USB Cable

How PIR Sensor Works

  • The HC-SR501 detects motion using infrared radiation from warm bodies.
  • It has two adjustment knobs:
    • Sensitivity (range: 3m to 7m)
    • Delay Time (how long the output stays HIGH after motion is detected)
  • Output Pin → HIGH (1) when motion detected, LOW (0) when idle.

Circuit Diagram

  • HC-SR501 VCC → 3.3V (or 5V supported)
  • HC-SR501 GND → GND
  • HC-SR501 OUT → D2

Arduino Code

// HC-SR501 PIR Motion Sensor with ESP8266 (Serial Output Only)

int pirPin = D2;   // PIR sensor output pin
int pirState = LOW; // Current PIR state

void setup() {
  pinMode(pirPin, INPUT);     // Set PIR as input
  Serial.begin(115200);       // Start Serial Monitor
}

void loop() {
  int motion = digitalRead(pirPin); // Read PIR sensor state

  if (motion == HIGH) {
    if (pirState == LOW) {
      Serial.println("Motion detected!");
      pirState = HIGH;
    }
  } else {
    if (pirState == HIGH) {
      Serial.println("Motion ended!");
      pirState = LOW;
    }
  }
}

Step-by-Step Code Explanation

PIR Pin Setup

int pirPin = D2;

Reading Motion

int motion = digitalRead(pirPin);

Reads whether motion is detected → HIGH or LOW.

State Change Detection

  • Prints “Motion detected!” only when motion starts.
  • Prints “Motion ended!” when no motion is detected.
  • Prevents repeated messages when state doesn’t change.

Real-Life Applications

  • Home Security Systems – Detect intruders and log events.
  • Smart Door Alerts – Detect when someone approaches.
  • IoT Surveillance Systems – Send motion alerts to cloud dashboards.
  • Energy Saving – Monitor room activity to optimize lights/appliances.

Troubleshooting

IssueCauseSolution
PIR not detecting motionWrong wiring or insufficient powerCheck connections; use 5V if unstable
Always HIGH outputSensor delay set too longAdjust delay knob
Always LOW outputSensor not aligned properlyPoint sensor toward motion area
False triggersHeat sources (sunlight, heater)Lower sensitivity or reposition sensor

Can I power the PIR sensor with 3.3V from ESP8266?

Yes, it works with 3.3V, but using 5V ensures more stable operation.

What is the detection range of HC-SR501?

It can detect motion within 3 to 7 meters, depending on sensitivity adjustment.

Can ESP8266 send alerts when motion is detected?

Yes, you can integrate ESP8266 with Blynk, MQTT, or IFTTT to send smartphone notifications.

Why does my PIR sensor give false triggers?

It may detect heat from sunlight, heaters, or moving curtains. Adjust sensitivity or shield the sensor.

Can PIR detect stationary humans?

No. PIR detects motion only, not static presence.

Leave a reply

Loading Next Post...
Follow
Search Trending
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...