How to wire a PIR motion sensor to an ESP32
To wire a PIR motion sensor to an ESP32, connect VCC to VIN (5 V from USB), OUT to GPIO 4 (D4) and GND to GND. The module needs 5 V for power but its output is 3.3 V, so OUT connects straight to the ESP32 with no divider. The sketch below uses no library beyond what the board includes. It prints Motion detected whenever the sensor’s output goes high.
- 3.3 V logic
- Library: none
- 3 wires
PIR motion sensor to ESP32 wiring table
Every wire in the circuit above, with the part’s pin names and the board’s own labels. The wire colors are the ones the circuit uses; on a real bench any colors work.
| PIR motion sensor pin | ESP32 pin | Wire color | Why |
|---|---|---|---|
| VCC | VIN (5 V from USB) | red | The HC-SR501 takes 4.5 to 20 V and regulates it down to 3.3 V on board. |
| OUT | GPIO 4 (D4) | amber | A 3.3 V high while it sees motion, then low. |
| GND | GND | blue | Common ground. |
Power and logic levels for PIR motion sensor and ESP32
Logic level
The ESP32’s GPIO pins are 3.3 V logic and are not 5 V tolerant. Nothing wired to a pin may go above 3.3 V.
Supply
The PIR motion sensor takes 5 V from VIN (5 V from USB). That is the ESP32’s 5 V rail, so the part shares the current the board can supply.
This pair
The module needs VIN (5 V from USB) for power, but its output is 3.3 V, so OUT connects straight to the ESP32 with no divider. That makes the PIR one of the few 5 V modules that is a perfect match for a 3.3 V board.
Pins to leave alone
GPIO 34, 35, 36 (VP) and 39 (VN) are input only: they cannot drive a pin or take an internal pull-up. GPIO 0, 2, 5, 12 and 15 are strapping pins, read at power-on, and GPIO 6 to 11 are the flash chip.
The working code for the ESP32
Library: none, the sketch reads OUT with digitalRead(). This is the sketch the circuit above runs.
const int PIR_PIN = 4; // OUT, GPIO 4 (D4)
int lastState = LOW;
void setup() {
Serial.begin(115200);
pinMode(PIR_PIN, INPUT);
Serial.println("Watching for motion");
}
void loop() {
int state = digitalRead(PIR_PIN);
if (state != lastState) {
lastState = state;
Serial.println(state == HIGH ? "Motion detected" : "Motion ended");
}
delay(50);
}
What you should see
Open the serial monitor at 115200 baud. The sketch prints Watching for motion once; each time the sensor sees movement, a Motion detected line follows, and Motion ended when it settles. In the editor, click the sensor to trigger it.
Real PIR modules need about a minute to settle after power-up and then hold their output high for a few seconds per trigger.
The serial monitor prints this, recorded from the circuit above with motion triggered after 300 ms:
Watching for motion
Motion detectedCommon mistakes wiring a PIR motion sensor to an ESP32
Not waiting for it to settle
A real HC-SR501 needs up to a minute after power-up to learn the room, and gives false triggers until then. Ignore it for the first 30 to 60 seconds.
The hold time knob
The orange trimmer next to the sensitivity one sets how long OUT stays high, from about 3 seconds to several minutes. A sensor that seems stuck on is usually turned all the way up. The simulated part has a hold property for the same thing.
Powering it from 3V3
Its regulator wants 4.5 V or more; at 3.3 V it triggers at random or not at all. Power from VIN (5 V from USB); the output is already 3.3 V.
Build your own PIR motion sensor project
Open this circuit in the editor, change the wiring or the code, and keep your version in a free account.