Using ESP32 with PIR Motion Sensor HC-SR501 and Alarm Buzzer
Using ESP32 with PIR Motion Sensor HC-SR501 and Alarm Buzzer
This guide explains how to use ESP32 with the HC-SR501 PIR sensor to detect movement. When motion is detected, the ESP32 triggers an Active Buzzer to sound an alarm and displays the detection status on the Serial Monitor.
Required Components
- ESP32 NodeMCU ESP-WROOM-32 (Dual Core board with Wi-Fi and Bluetooth)
- HC-SR501 PIR Motion Sensor
- Active Buzzer Module (3.3V - 5V compatible)
- Breadboard 170 points
- Male-to-Male and Male-to-Female jumper wires
- Micro USB cable for computer connection
- Micro USB Power Adapter 5V 2A (for powering the board)
Wiring ESP32 to HC-SR501 and Buzzer
Connection Table
| ESP32 | HC-SR501 | Active Buzzer |
|---|---|---|
| 3V3 | VCC | VCC |
| D2 | OUT | - |
| D1 | - | OUT |
| GND | GND | GND |
- HC-SR501 VCC connects to ESP32 3V3
- HC-SR501 OUT connects to ESP32 D2 (receives digital signal)
- Buzzer OUT connects to ESP32 D1 (sends signal to buzzer)
- GND pins of both devices connect to ESP32 GND in common
Arduino Code for Motion Detection
const int pirPin = 2; // D2 receives signal from HC-SR501
const int buzzerPin = 1; // D1 sends signal to Buzzer
void setup() {
Serial.begin(9600);
pinMode(pirPin, INPUT);
pinMode(buzzerPin, OUTPUT);
// Turn off buzzer initially
digitalWrite(buzzerPin, LOW);
Serial.println("ESP32 PIR Motion Detector Ready");
}
void loop() {
int motion = digitalRead(pirPin);
if (motion == HIGH) {
Serial.println("Motion Detected");
digitalWrite(buzzerPin, HIGH); // Turn on alarm
} else {
Serial.println("No Motion");
digitalWrite(buzzerPin, LOW); // Turn off alarm
}
delay(500); // Read every 500 milliseconds
}
How to Upload Code to ESP32
- Open Arduino IDE and paste the code above
- Go to Tools -> Board and select ESP32 Dev Module
- Go to Tools -> Port and select the COM port where ESP32 is connected
- Click the Upload button and wait for “Done uploading”
- Open Serial Monitor (Tools -> Serial Monitor)
- Set Baud Rate to 9600
Testing the Setup
After uploading the code successfully, observe the Serial Monitor:
- When no movement is detected in front of the sensor, it shows “No Motion” and digital value is 0
- When you wave your hand or walk past the HC-SR501, it shows “Motion Detected” and digital value becomes 1, with the buzzer sounding an alarm
Adjusting HC-SR501 Sensitivity
The sensor module has two adjustment potentiometers:
- Time Delay: Adjusts how long the output stays HIGH after detecting motion
- Sensitivity: Adjusts detection range (near to far)
Turn right to increase values, left to decrease
How PIR Sensors Work
PIR (Passive Infrared) sensors work by detecting changes in infrared radiation emitted by humans and animals. When movement passes in front of the sensor, the changing heat signature is detected and a HIGH signal is output from the OUT pin.
Reference Video
Summary
In this article, we learned how to use ESP32 with the HC-SR501 PIR sensor by reading the digital signal from the OUT pin and using it to control an Active Buzzer that sounds an alarm when motion is detected. The code is simple and suitable for beginners studying IoT and basic alert systems.
อยากทำโปรเจคแบบนี้?
รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน
If you need Arduino project service or urgent IoT development, see full service details on the home page
จ้างทำโปรเจคเลย