Codey OnlineArduino & ESP32 component library › HC-SR501 PIR Motion Sensor
HC-SR501 PIR Motion Sensor
Sensors

HC-SR501 PIR Motion Sensor

HC-SR501 Infrared PIR Motion Sensor Module for Arduino and

Width: 42.3 mm

Pinout — HC-SR501 PIR Motion Sensor

Pin Signal Description
VCC power Power input for the PIR module, typically 5V DC to the onboard regulator.
OUT digital Digital motion output; goes high when PIR motion is detected.
GND ground Ground reference for power and the OUT signal.

Example wiring diagram — HC-SR501 PIR Motion Sensor

From To Wire
board:5V x1:VCC red
board:GND x1:GND black
board:D2 x1:OUT green

Example Arduino code — HC-SR501 PIR Motion Sensor

This example was written and compile-checked for the arduino-uno. Codey Online adapts it to any other supported board for you.

// HC-SR501 PIR Motion Sensor
// Reads the PIR motion output and prints when motion is detected.

const int pirPin = 2;

void setup() {
  pinMode(pirPin, INPUT);
  Serial.begin(9600);
  while (!Serial) {
    ;
  }
  Serial.println("HC-SR501 PIR motion sensor ready");
}

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

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

  delay(500);
}

Related components

18650 Battery Shield with USB-C Charger and 5V Boost Converter

18650 Battery Shield with USB-C Charger and 5V Boost Converter

2.4 inch OLED Display I2C 3.3V

2.4 inch OLED Display I2C 3.3V

3.5 inch TFT LCD Shield

3.5 inch TFT LCD Shield

50kg Load Cell Weight Sensor

50kg Load Cell Weight Sensor

Build your Arduino or ESP32 project with AI

Describe what you want to build. Codey Online writes the code, draws the wiring diagram, compiles it and flashes your board straight from the browser.

Start for free