Project

โ— Beginner ยท HC-SR501 PIR Motion Sensor

Detecting Motion with HC-SR501 PIR Sensor on ESP32

Overview

Learn how to use the HC-SR501 PIR motion sensor with an ESP32 to detect movement. This project is useful for security systems, automatic lights, and motion-activated alarms.

Instructions

Step by step

1. Connect the PIR VCC pin to 3.3V. 2. Connect the PIR GND pin to GND. 3. Connect the PIR OUT pin to D15. 4. Run the code and open the Serial Monitor. 5. Click or toggle the PIR sensor in the simulator to change its motion state. 6. When motion is detected, the output should print "Motion detected!". 7. When there is no motion, the output should print "No motion".

Code

Source code

code
const int pirPin = 15;

void setup() {
  Serial.begin(115200);
  pinMode(pirPin, INPUT);

  Serial.println("HC-SR501 PIR Motion Sensor Test");
}

void loop() {
  int motionState = digitalRead(pirPin);

  if (motionState == HIGH) {
    Serial.println("Motion detected!");
  } else {
    Serial.println("No motion");
  }

  delay(1000);
}

Related

More projects with this sensor