Component Guide

Input Devices Beginner

HC-SR501 PIR Motion Sensor

Passive infrared motion sensor module that gives an ESP32 a digital HIGH signal when warm objects move through its field of view.

BeginnerDifficulty 14 minReading time 20-35 minBench time ESP32Compatible
Share
HC-SR501 PIR Motion Sensor

Plain-English Overview

A PIR sensor watches for changes in heat. People, pets, and warm objects give off infrared energy. When a warm body moves across the sensor view, the module notices the change and sends a signal to the ESP32.

It does not see pictures like a camera. It only tells your board that motion happened. That makes it useful for simple alarms, lights, and wake-up triggers without recording anyone.

Use it when you care about movement, not exact distance or identity.

Where You Use It

  • Motion security alert
  • Automatic light trigger
  • Occupancy sensing
  • Visitor counter experiment
  • Wake-up trigger for battery nodes
  • Classroom infrared demo
  • Smart hallway light
  • Camera trap trigger

Quick Facts

  • Detects Moving warm bodies
  • Output Digital HIGH/LOW
  • Power Usually 5 V module input
  • Warm-up 30-60 seconds
  • Adjustments Sensitivity and delay trimpots
  • Best use Motion trigger

How It Works

PIR means passive infrared. Passive means the sensor does not send energy out like an ultrasonic sensor. It only observes infrared energy already coming from objects.

The white dome is a Fresnel lens. It splits the room into zones and focuses infrared energy onto two sensing elements. When a warm body moves from one zone to another, the two elements see a changing heat pattern.

The module amplifies that tiny change and turns it into a digital output. HIGH means motion was detected. LOW means no recent motion. The onboard delay control decides how long the output stays HIGH after motion.

Because PIR detects change, a person standing perfectly still may stop triggering it. Heat sources, sunlight, HVAC airflow, and pets can also trigger it if the sensor points at them.

Technical Specifications

Arduino library: Built-in Arduino digitalRead

SpecificationValueWhy it matters
Supply voltage Typically 5 V to 20 V module input Common HC-SR501 boards include regulation; use module documentation.
Output logic Digital HIGH on motion ESP32 can read the OUT pin as a digital input.
Output voltage Often about 3.3 V HIGH Usually ESP32-safe, but verify your module if uncertain.
Detection range Up to about 7 m Depends on sensitivity setting, lens, target size, and environment.
Field of view Roughly 100 degrees Wide coverage but not precise direction sensing.
Warm-up time 30-60 seconds Readings can be unstable immediately after power-up.
Delay time Adjustable, often seconds to minutes Controls how long OUT remains HIGH after motion.
Sensitivity Adjustable by trimpot Helps reduce false triggers or extend range.
Trigger mode Repeatable or non-repeatable on some modules Jumper behavior changes output timing.
Best targets Moving warm bodies The sensor responds to infrared change, not visible motion.

Pinout

  • VCC Module power 5 V typical Powers the PIR module and onboard regulator.
  • OUT Digital motion output ESP32 GPIO27 Usually HIGH when motion is detected.
  • GND Ground reference ESP32 GND Required for ESP32 to read OUT correctly.
  • Delay trimpot Output hold adjustment Turn gently with small screwdriver Changes how long OUT stays HIGH.
  • Sensitivity trimpot Range adjustment Turn gently with small screwdriver Changes detection range and false trigger tendency.

Wiring Diagram

The PIR module has a control side only: VCC, OUT, and GND. OUT is a digital signal the ESP32 reads.

After powering the sensor, wait at least 30 seconds before judging behavior. During warm-up, random triggers are normal.

Wiring Diagram HC-SR501 PIR VCC to 5 V, GND to ESP32 GND, OUT to ESP32 GPIO27
  1. 1

    Unplug USB before wiring.

  2. 2

    Connect PIR VCC to 5 V or VIN on the ESP32 board.

  3. 3

    Connect PIR GND to ESP32 GND.

  4. 4

    Connect PIR OUT to ESP32 GPIO27.

  5. 5

    Point the dome away from windows, heaters, and moving fans.

  6. 6

    Power the ESP32 and wait 30-60 seconds for warm-up.

  7. 7

    Open Serial Monitor at 115200 baud.

  8. 8

    Walk across the sensor view and watch for motion messages.

Code Examples

Use the same wiring with Arduino IDE, PlatformIO, or ESP-IDF. Start with Arduino, then graduate when you need a larger project structure.

pir_esp32_motion_test.ino
#define PIR_PIN 27

int lastState = LOW;

void setup() {
  Serial.begin(115200);
  pinMode(PIR_PIN, INPUT);
  Serial.println("PIR motion sensor warming up...");
  delay(30000);
  Serial.println("PIR ready. Walk across the sensor view.");
}

void loop() {
  int currentState = digitalRead(PIR_PIN);

  if (currentState != lastState) {
    if (currentState == HIGH) {
      Serial.println("Motion detected");
    } else {
      Serial.println("Motion cleared");
    }
    lastState = currentState;
  }

  delay(100);
}

The PIR output is digital, so no special library is needed. The code watches for state changes instead of printing continuously.

setup() gives the sensor time to stabilize. loop() compares the current reading with the previous reading, then prints only when motion starts or clears. This keeps Serial Monitor readable.

Expected Output

After a warm-up message and 30 second delay, Serial Monitor prints Motion detected when someone moves across the sensor view. Depending on the delay trimpot, it may print Motion cleared several seconds later. If it triggers while nobody is moving, check heat sources and sensitivity.

Common Mistakes

  • Judging the sensor during warm-up.
  • Pointing it at windows or sunlight.
  • Pointing it at heaters, vents, or fans.
  • Expecting it to detect still people.
  • Using it as a distance sensor.
  • Forgetting common ground.
  • Turning trimpots aggressively.
  • Mounting behind glass or thick plastic.
  • Using delay setting without understanding hold time.
  • Printing continuously instead of detecting state changes.

Troubleshooting

ProblemPossible causeSolution
Triggers randomly Heat source, sunlight, HVAC airflow, pets, or high sensitivity. Point away from heat changes and reduce sensitivity.
Never triggers No power, wrong OUT pin, still target, or sensor blocked. Check wiring, wait for warm-up, and walk across the lens.
Always HIGH Delay setting high, retrigger mode, or constant motion/heat. Wait for timeout, reduce delay, and move away from heat sources.
Always LOW Power missing or OUT not connected to the coded GPIO. Confirm VCC, GND, GPIO27, and Serial output.
Works only sometimes Target movement direction or range is poor. Walk across the field of view, not straight toward the sensor.
Triggers through window Sunlight/heat changes affect the sensor. Avoid aiming at windows.
Does not detect through glass Glass blocks much infrared energy. Mount with clear view of the room.
Motion cleared takes too long Delay trimpot set high. Turn delay adjustment lower carefully.
Range too short Sensitivity low or lens obstructed. Increase sensitivity and check dome orientation.
Pets trigger it PIR sees warm moving animals. Aim higher or mask part of the lens.
ESP32 reads unstable Floating input or poor ground. Use common ground and stable wiring.
False trigger when relay switches Electrical noise or power dip. Separate relay power and add filtering.
No output after boot Warm-up period not complete. Wait 30-60 seconds.
Board resets Weak power supply. Use stable USB power.
Cannot tell direction PIR only gives motion/no motion. Use multiple sensors or another technology for direction.

Related Guides

Related Projects

FAQ

Review, Testing, and References

Author: Abdul Mubeen and the ESP32 Engine editorial team. Last updated: 2026-07-05. Reviewed: wiring, code, beginner safety, and ESP32 compatibility. Educational level: Beginner.

Use this component page as an educational starting point. Check official documentation before using the part in production, high-current, outdoor, battery, or safety-critical hardware.

Downloads

HC-SR501 module references vary by manufacturer. Check your module markings, jumper mode, delay range, and sensitivity trimpot direction.

Download Datasheet (PDF)