ESP32 Tutorial 21/55 - Detecting Human with PIR Motion Sensor | SunFounder's ESP32 IoT Learning kit
In this tutorial, we will learn how to use a PIR motion sensor with the ESP32 to detect human presence. The ESP32 is a powerful microcontroller that offers both Wi-Fi and Bluetooth capabilities, making it ideal for IoT projects. By integrating a PIR sensor, we can build a system that activates an LED or buzzer whenever motion is detected, providing a simple yet effective way to monitor for human activity.
The PIR (Passive Infrared) sensor detects motion by sensing the infrared radiation emitted by objects, particularly warm-bodied ones like humans. When motion is detected, the sensor sends a signal to the ESP32, which can then trigger an output, such as lighting an LED or sounding a buzzer. This tutorial will guide you through the hardware setup, wiring, and code needed to make the system function correctly (in video at 00:45).
Hardware Explained
This project involves several key components. The ESP32 microcontroller serves as the brain of the operation, handling sensor data and controlling outputs. The PIR motion sensor detects movement by measuring changes in infrared radiation. The LED or buzzer provides a visual or audible alert when motion is detected.
The ESP32 is equipped with Wi-Fi and Bluetooth, allowing for extended functionalities such as cloud connectivity. The PIR sensor contains a lens that focuses infrared light onto a pyroelectric sensor, which generates a voltage signal when it detects changes in infrared levels, indicating movement.
Datasheet Details
| Manufacturer | SunFounder |
|---|---|
| Part number | PIR Sensor Module |
| Logic/IO voltage | 3.3 V |
| Supply voltage | 3.3 V |
| Output current (per channel) | 20 mA max |
| Peak current (per channel) | 50 mA max |
| PWM frequency guidance | N/A |
| Input logic thresholds | 0.3 V (low), 2.0 V (high) |
| Voltage drop / RDS(on) / saturation | N/A |
| Thermal limits | 0 to 70 °C |
| Package | Module |
| Notes / variants | Adjustable sensitivity and delay |
- Ensure proper power supply (3.3 V) to avoid damage.
- Use a resistor for the LED to limit current.
- Maintain proper wiring to prevent short circuits.
- Adjust PIR sensor knobs for sensitivity and delay as needed.
- Test the sensor in different environments for optimal performance.
Wiring Instructions

To wire the PIR motion sensor and LED to the ESP32, start by connecting the VCC pin of the PIR sensor to the 3.3 V output on the ESP32. Next, connect the ground pin of the PIR sensor to a ground pin on the ESP32. The signal pin (often labeled as S) from the PIR sensor should be connected to GPIO pin 14 on the ESP32.
For the LED, connect the longer leg (anode) to GPIO pin 26 through a 220 Ohm resistor, and connect the shorter leg (cathode) to ground. Make sure all connections are secure to ensure proper functionality. If you are using a buzzer instead of an LED, connect one pin of the buzzer to GPIO pin 26 and the other to ground, replacing the LED setup.
Code Examples & Walkthrough
In the code, we define the pin numbers for the PIR sensor and the LED. We use const int pirPin = 14; to set the PIR pin and const int ledPin = 26; for the LED pin. These constants ensure that the pin numbers remain unchanged throughout the execution of the program.
const int pirPin = 14; // the number of the pir pin
const int ledPin = 26; // LED pin
This snippet initializes the pins we will use. In the setup() function, we configure the PIR pin as an input and the LED pin as an output. This setup is crucial for reading the sensor's state and controlling the LED.
void setup() {
Serial.begin(115200);
pinMode(pirPin, INPUT); // initialize the pir pin as an input
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output
}
In the loop() function, we continuously read the state of the PIR sensor using digitalRead(pirPin);. If the sensor detects motion (indicated by a HIGH state), we turn the LED on by using digitalWrite(ledPin, HIGH);. Otherwise, we turn the LED off.
if (pirState == HIGH) {
digitalWrite(ledPin, HIGH); // turn LED on
} else {
digitalWrite(ledPin, LOW); // turn LED off
}
This logic allows the LED to respond to motion detected by the PIR sensor. Make sure to upload the full code provided below the article for full functionality.
Demonstration / What to Expect
Once everything is set up and the code is uploaded, the LED will light up when motion is detected by the PIR sensor. You can test the sensor by moving in front of it; the LED should turn on immediately. If the sensor is set to a delay, it will remain on for the specified time before turning off again (in video at 12:15).
Common pitfalls include incorrect wiring, which can prevent the sensor from functioning. Ensure that the PIR sensor is properly powered and that the signal pin is connected to the correct GPIO pin on the ESP32. Adjusting the sensitivity and delay knobs on the PIR sensor can also help optimize performance based on your environment.
Video Timestamps
- 00:00 Start
- 1:54 Introduction to PIR Sensor
- 6:38 Wiring explained
- 10:21 Arduino code explained
- 12:45 Selecting ESP32 board and COM port in Arduino IDE
- 14:27 Demonstration and calibration of PIR sensor
Common Course Links
Common Course Files
资源与参考
尚无可用资源。
文件📁
数据手册 (pdf)
-
RE200B_PRI motion sensor datasheet
RE200B_PRI_datasheet.pdf0.02 MB -
BISS0001 motion sensor chip datasheet
BISS0001_datasheet.pdf0.16 MB
其他文件
-
HC-SR501运动传感器用户手册
robojax-HC-SR501-motion-manual.pdf