In this lesson you will learn how to use the HC-SR501 PIR motion sensor with your ESP32. The PIR sensor detects movement and can trigger an LED or buzzer.
What you will learn
- How a PIR sensor works
- How to wire the HC-SR501 to the ESP32
- How to read digital input from the sensor
- How to trigger an LED or buzzer when motion is detected
Components needed
Get All the Parts You Need
This tutorial is part of our comprehensive ESP32 learning series. Instead of buying components individually, save time and money with our ESP32 Basic Starter Kit. It includes everything you need for this lesson and 20+ other projects.
What's Included: ESP32 board, OLED display, sensors (DHT11, PIR, LDR), relay module, buzzers, LEDs, buttons, breadboard, resistors, and all cables, plus access to our complete lesson plans.
View ESP32 Starter Kit →Step 1 - Understanding the PIR sensor
The HC-SR501 detects infrared radiation from moving objects such as humans. It has three pins: VCC, GND, and OUT. The OUT pin goes HIGH when motion is detected and LOW when no motion is detected.


Step 2 - Wiring the PIR sensor and LED
- Place the PIR sensor on the breadboard
- Connect VCC to 5 V on the ESP32
- Connect GND to GND on the ESP32
- Connect the OUT pin to GPIO 33 pin on the ESP32
- Place the LED on the breadboard
- Connect a 220 Ω resistor to the LED anode (long leg)
- Connect the other end of the resistor to GPIO 32
- Connect the LED cathode (short leg) to GND
Step 3 - Writing the code to detect motion
Open Arduino IDE, enter and upload the following code. The LED turns on when motion is detected and off when no motion is detected.
int pirPin = 33; // GPIO connected to PIR OUT int ledPin = 32; // GPIO connected to LED int pirState = 0; void setup() { pinMode(pirPin, INPUT); pinMode(ledPin, OUTPUT); Serial.begin(115200); } void loop() { pirState = digitalRead(pirPin); // Read PIR sensor if (pirState == HIGH) { digitalWrite(ledPin, HIGH); // Turn LED on Serial.println("Motion detected"); } else { digitalWrite(ledPin, LOW); // Turn LED off Serial.println("No motion"); } delay(500); }
How the Code Works
This program reads the PIR motion sensor and turns the LED on when motion is detected.
int pirPin = 33;This line stores the GPIO pin number connected to the PIR sensor output.
int ledPin = 32;This line stores the GPIO pin number connected to the LED.
int pirState = 0;This variable stores the current state of the PIR sensor.
void setup()This function runs once when the ESP32 starts.
pinMode(pirPin, INPUT);Sets the PIR sensor pin as an input.
pinMode(ledPin, OUTPUT);Sets the LED pin as an output.
Serial.begin(115200);Starts serial communication so you can monitor motion detection in the Serial Monitor.
void loop()
This function runs continuously.
-
digitalRead(pirPin)reads the current state of the PIR sensor. HIGH means motion detected, LOW means no motion - If the sensor output is HIGH, the LED turns on and
Serial.println("Motion detected")prints a message to the Serial Monitor - If the sensor output is LOW, the LED turns off and
Serial.println("No motion")prints a message -
delay(500)pauses for 500 milliseconds before checking the sensor again
Step 4 - Testing motion detection

- Open the Serial Monitor at 115200 baud
- Move in front of the PIR sensor
- The LED should turn on and the Serial Monitor should display “Motion detected”
- When no movement is detected the LED should turn off and Serial Monitor should display “No motion”
Optional Challenge
Try replacing the LED with an active buzzer and alternate between HIGH and LOW on the digital pin to simulate a security system. We recommend chaning the ledPin variable name to a more appropriate name such as buzzerPin.
ESP32 Lessons
- ESP32 Tutorials Hub
- Lesson 1: ESP32 Setup using Arduino IDE
- Lesson 2: ESP32 Libraries and Using the Serial Monitor
- Lesson 3: LED Blink
- Lesson 4: Passive Buzzer
- Lesson 5: Reading a Photosensitive Resistor (LDR)
- Lesson 6: Using a Push Button with ESP32
- Lesson 7: Using DHT11 Temperature & Humidity Sensor
- Lesson 8: ESP32 Detecting Motion With HC-SR501 PIR Sensor
Lesson complete
You have successfully used the PIR sensor to detect motion and trigger outputs. In the next lesson you will learn how to use the IR sensor to detect objects or obstacles.