Motion
Interfacing HC-SR501 Motion with ESP32 DevKit
What is the HC-SR501?
The HC-SR501 is a passive infrared (PIR) motion-sensor module. Its Fresnel lens focuses infrared changes onto a pyroelectric sensing element; when a warm body moves across the detection area, the module drives OUT HIGH. When the output hold time expires and no new trigger extends it, OUT returns LOW. It provides a digital motion event—not a distance, temperature, or image reading.
This reference covers the common HC-SR501 module, its three pins, trigger modes, adjustable delay and sensitivity, warm-up behavior, and practical design considerations. Module revisions and clone boards can differ, so confirm the labels and behavior of your own board.
Warning
**Power-up warm-up matters.** Allow roughly 30–60 seconds after applying power before judging the sensor. Initial triggers or unstable readings can occur while it settles. Do not use a PIR module as a safety-rated presence detector or the sole protection against hazardous motion.
HC-SR501 specifications
- Sensor type
- Passive infrared (PIR) motion detector
- Supply
- Common module range about 4.5–20 V DC; 5 V is a typical choice
- Output
- Digital active-HIGH; commonly about 3.3 V when HIGH
- Detection range
- Often up to around 5–7 m, depending on module, lens, environment and settings
- Detection angle
- Wide Fresnel-lens field of view; exact angle varies by lens and listing
- Delay adjustment
- Commonly several seconds to a few minutes; exact range varies by module
- Trigger mode
- Jumper-selectable single-trigger (L) or repeatable-trigger (H) on common boards
- Interface
- One digital output; no library or communication bus required
HC-SR501 pinout
| Pin | Connection | Purpose |
|---|---|---|
| VCC | 5 V recommended for beginner circuits | Module power input; check the rating of your revision. |
| OUT | Digital GPIO input | Typically LOW at idle and HIGH when motion is detected/held. |
| GND | Board GND | Common ground reference. |
Swipe sideways to see every column →
How the HC-SR501 works
The lens divides the scene into multiple detection zones. As a person moves through those zones, the infrared pattern changes and the module's onboard electronics interpret the change as motion. A stationary person may stop producing repeated triggers even while still in view; PIR sensors are generally more responsive to movement across the field than to movement directly toward the lens.
The module's OUT pin is an active-HIGH digital signal. In a typical setup, LOW means idle and HIGH means the output timer is active. The HIGH period depends on the delay trim pot and trigger mode; it is not necessarily the exact time that a person remains in the sensing area.
Sensitivity, delay and trigger mode
- Sensitivity trim pot: adjusts how readily the module detects movement and the practical detection range. Start at a moderate setting and tune it in the installed environment.
- Time-delay trim pot: controls how long OUT stays HIGH after a trigger. The exact minimum and maximum vary among boards and clones.
- Trigger jumper: common HC-SR501 boards label the modes H and L. In repeatable mode (H), new motion during the HIGH period can retrigger/extend the output. In single-trigger mode (L), additional movement during that period does not keep extending it in the same way. Verify the markings on your board.
- Warm-up: allow about 30–60 seconds after power-up. Avoid adjusting multiple settings at once; change one variable, test, and record the result.
What you can build
- Motion-activated light: use OUT as an input and control a properly rated driver; do not power a lamp or relay coil directly from the sensor output.
- Room occupancy indicator: combine motion events with a timeout in software. A PIR alone cannot guarantee that a room is empty because a motionless person may not trigger it.
- Doorway or corridor alert: use the output to log an event or sound a low-voltage buzzer through an appropriate driver.
- Security prototype: add an alarm indicator or notification system, but treat the HC-SR501 as a hobby sensor, not a tamper-resistant security device.
- Energy-saving automation: use motion to request lighting, then apply a separate software timeout and manual override.
Wiring and installation best practices
- Check the pin labels. Common modules often place VCC, OUT and GND along a three-pin header, but do not rely on a generic image if your board is laid out differently.
- Use a stable supply and common ground. Power the module from a suitable 5 V rail for typical Uno/ESP32 examples and connect GND to the controller's GND.
- Protect the GPIO. The common module output is about 3.3 V HIGH, which is suitable for many 3.3 V inputs, including ESP32. Measure or verify your particular module before connection; never assume an unknown clone has the same output circuit.
- Avoid heat and movement sources. Sunlight, heaters, hot air, moving curtains, and rapid temperature changes can cause false triggers. Mount the sensor firmly and aim the lens at the intended zone.
- Do not expect a rangefinder. The output says motion was detected; it does not identify the person, provide distance, or indicate direction.
- Drive loads correctly. Use a transistor/MOSFET, relay driver, or suitable interface circuit for external loads. Respect the controller's current and voltage limits.
- Handle mains safely. Keep mains wiring physically isolated from low-voltage electronics and use an appropriately enclosed, rated driver. If unsure, use a low-voltage demonstration load.
Troubleshooting
| Symptom | Likely cause | What to check |
|---|---|---|
| OUT stays HIGH for a long time | Delay pot set high, repeatable mode, or repeated motion | Reduce delay carefully, check jumper mode, and test after leaving the field of view. |
| No detection | Incorrect pinout, inadequate supply, lens obstructed, or sensor not settled | Check VCC/GND, allow warm-up, remove obstructions, and walk across the sensing zones. |
| Triggers when nobody is present | Heating/airflow changes, sunlight, vibration, or an overly sensitive setting | Reposition away from HVAC vents and heat sources; lower sensitivity and retest. |
| Detects people inconsistently | Person moves directly toward sensor, poor mounting angle, or range setting too low | Test lateral movement across the lens zones and adjust mounting/sensitivity. |
| ESP32 input reads unpredictably | Missing common ground, long/noisy wires, or output voltage unlike expected | Verify ground and measure OUT HIGH; keep wiring short and confirm GPIO voltage safety. |
| Trigger occurs immediately after power-up | Normal warm-up behavior on some modules | Wait 30–60 seconds before evaluating steady-state operation. |
Frequently asked questions
Does the HC-SR501 need an Arduino library? No. Read OUT with digitalRead(); the module performs its own PIR signal processing.
Is OUT 5 V? The common HC-SR501 output is typically around 3.3 V HIGH, but clones and variants can differ. Verify your module's output before connecting it to a 3.3 V-only GPIO.
Can an ESP32 read the output directly? Usually yes with a standard HC-SR501 module whose OUT is about 3.3 V HIGH. Connect grounds together and verify the actual output voltage. Do not connect an unknown 5 V logic output directly to an ESP32.
Why does it remain HIGH after motion stops? The delay pot sets an output hold time, and repeatable-trigger mode may extend that time if movement continues.
Can it detect a person through glass? Standard PIR sensors generally do not reliably detect body motion through ordinary glass because the relevant infrared wavelengths are blocked or altered.
Does the simulator reproduce real infrared sensing? No. The saved project lets you explore the wiring and code; its motion toggle represents a digital event and does not model optics, temperature, range, or timing.
Continue with a board integration: HC-SR501 with Arduino Uno · HC-SR501 with ESP32 · Explore SolderHub Simulator
Setting Up HC-SR501 with ESP32 DevKit
This guide reads the HC-SR501 digital output with GPIO27 on a classic ESP32 DevKit. The common module's OUT signal is typically about 3.3 V HIGH, which is generally suitable for ESP32 logic. Verify the output level of your particular board or clone before connecting it; ESP32 GPIOs are not 5 V tolerant.
Warning
**Voltage check:** the wiring below assumes OUT is about 3.3 V HIGH. If your module can output 5 V, use a suitable level shifter or divider before connecting it to an ESP32 GPIO. Connect module GND and ESP32 GND together.
Wiring: HC-SR501 to ESP32
| Sensor pin | ESP32 connection | Purpose |
|---|---|---|
| VCC | 5V / VIN | Module power input. |
| OUT | GPIO27 | Digital input; common output is around 3.3 V HIGH. |
| GND | GND | Common ground. |
Swipe sideways to see every column →
Try this circuit — SolderHub Simulator
The circuit is preloaded for exploration. Confirm simulator behavior against the real wiring and sensor datasheet before connecting hardware.
- 1Check your exact ESP32 pinout; this example uses GPIO27 on a classic ESP32 DevKit.
- 2Connect VCC to 5V/VIN, OUT to GPIO27 and GND to GND.
- 3Verify that OUT is 3.3 V-safe before connecting it to the GPIO; level-shift if needed.
- 4Upload the sketch using the correct ESP32 board target and port.
- 5Open Serial Monitor at 115200 baud and allow 30–60 seconds for warm-up.
- 6Walk across the lens field of view and check the serial output.
Expected behavior
The Serial Monitor reports Motion detected while OUT is HIGH and No motion while LOW. GPIO2 is a common onboard LED pin, but LED wiring varies by ESP32 board; change LED_PIN or remove the LED lines if needed.
Troubleshooting
- GPIO reads erratically: check common ground, power stability and the actual OUT voltage; do not feed 5 V into the GPIO.
- Output stays HIGH: check the delay setting and jumper, then wait for the hold period to end.
- No detection: allow warm-up, verify the pinout and try lateral movement across the lens zones.
- GPIO27 unavailable: select a safe GPIO on your specific ESP32 variant and update
PIR_PIN.
FAQ
Do I need a pull-up? Normally not; the common HC-SR501 has a driven digital output, unlike the open-collector A3144.
Can ESP32 read OUT directly? Usually, if the module output is about 3.3 V HIGH. Verify the actual module before wiring.
Is GPIO27 available on all ESP32 boards? No; pin availability differs by family and module.
Related Components
Related Guides & Projects
MicroPython vs Arduino on ESP32: Which Should You Learn First?
An honest comparison for beginners: where MicroPython is easier, where Arduino C++ is faster, how libraries and memory differ, and how to choose based on your project instead of hype.
MicroPython ESP32 Web Server: Control an LED or Relay from Your Browser
Create a working web server with the socket module, parse requests, serve a responsive control page for an LED or a relay, keep the displayed state truthful, choose a safe relay startup state and make the server survive bad or repeated requests.
MicroPython on ESP32 and Raspberry Pi Pico W: The Complete Guide
A structured MicroPython learning path for ESP32 and Pico W. Set up your tools, control GPIO, read sensors, connect to WiFi, build web servers, publish MQTT data and make projects reliable. Start here, then follow the links by level.
boot.py vs main.py in MicroPython: What Runs, When and Why
Understand the difference between boot.py and main.py in MicroPython, startup order, Wi-Fi setup, project structure, and recovery from boot errors.