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

PIR Sensor HC-SR501

The PIR Sensor HC-SR501 detects changes in infrared (IR) radiation caused by motion, typically from people or animals. It provides a digital output signal (OUT) suitable for direct reading by microcontrollers like Arduino or ESP32, using the standard power and ground pins (VCC and GND).

Width: 33.1 mm

Pinout — PIR Sensor HC-SR501

Pin Signal Description
GND GND Ground
OUT digital out
VCC VCC Power positive

Example wiring diagram — PIR Sensor HC-SR501

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

Example Arduino code — PIR Sensor HC-SR501

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

// PIR Sensor HC-SR501 - canonical beginner example
// Reads the sensor output on D2 and prints motion status to Serial Monitor.

const int pirPin = 2;

void setup() {
  pinMode(pirPin, INPUT);
  Serial.begin(9600);
  while (!Serial) {
    ;
  }
  Serial.println("PIR sensor warming up... give it about 30-60 seconds.");
}

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

  if (motion == 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