The HC-SR501 is the white-domed motion sensor behind most automatic lights and security alarms. It costs about ₹70 in India and does all the detection on board, so the Uno only reads one pin: HIGH when something warm moves, LOW otherwise.
It runs from the Uno’s 5V pin. Its output is a 3.3 V HIGH, which is still above the Uno’s logic threshold, so it connects straight to D2 with no extra parts. D2 is also one of the Uno’s two external-interrupt pins, in case you later want motion to wake a sleeping Uno.
Most “it doesn’t work” reports are really about the two pots and the jumper on the back, so they are covered below. The sketch waits out the warm-up, then lights the on-board LED during motion and reports how long each event lasted.
Wiring
| Arduino Uno R3 | HC-SR501 PIR motion sensor | Wire | Notes |
|---|---|---|---|
| 5V | VCC | red | Rated 4.5–20 V. The on-board regulator turns it into 3.3 V for the sensor chip. |
| D2 | OUT | yellow | OUT swings 0–3.3 V; the Uno reads anything above about 3 V as HIGH. D2 can also take an interrupt. |
| GND | GND | black |
4.5–20 V (5 V typical) part, 5 V board. Power it from 5 V (VIN/5V pin). Its output is a 3.3 V logic signal, so it connects directly to ESP32, Pico and Uno GPIOs.
Board quirks
- Give it 60 seconds after power-up. During warm-up OUT goes HIGH at random — the sketch waits before reporting.
- The two orange pots: Tx (time) sets how long OUT stays HIGH, from about 3 s to 5 min; Sx (sensitivity) sets range, up to about 7 m. Start with both fully anticlockwise.
- The jumper: H (repeat) keeps OUT HIGH while there is movement; L (single) drops it after the time even if you keep moving. Use H for lights.
- After OUT goes LOW, the sensor ignores motion for about 3 s. That is built into the BISS0001 chip, not a bug in your code.
- PIR sees heat moving, not presence. A person sitting still at a desk disappears after the delay; a ceiling fan or sunlight on a wall can trigger it.
Code
// HC-SR501 PIR motion sensor on an Arduino Uno R3.
// VCC → 5V, OUT → D2, GND → GND. OUT is a 3.3 V HIGH, which the Uno reads fine.
// Lights the on-board "L" LED (D13) while motion is detected.
const uint8_t PIR_PIN = 2;
bool lastState = LOW;
unsigned long motionStart = 0;
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
Serial.println(F("PIR warming up for 60 s - keep still..."));
delay(60000); // the sensor calibrates to the room after power-up
Serial.println(F("PIR ready"));
}
void loop() {
bool state = digitalRead(PIR_PIN);
if (state != lastState) {
lastState = state;
digitalWrite(LED_BUILTIN, state);
if (state == HIGH) {
motionStart = millis();
Serial.println(F("Motion detected"));
} else {
Serial.print(F("Motion ended after "));
Serial.print((millis() - motionStart) / 1000);
Serial.println(F(" s"));
}
}
}// HC-SR501 PIR motion sensor on an Arduino Uno R3.
// VCC → 5V, OUT → D2, GND → GND. OUT is a 3.3 V HIGH, which the Uno reads fine.
// Lights the on-board "L" LED (D13) while motion is detected.
const uint8_t PIR_PIN = 2;
bool lastState = LOW;
unsigned long motionStart = 0;
void setup() {
pinMode(PIR_PIN, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
Serial.println(F("PIR warming up for 60 s - keep still..."));
delay(60000); // the sensor calibrates to the room after power-up
Serial.println(F("PIR ready"));
}
void loop() {
bool state = digitalRead(PIR_PIN);
if (state != lastState) {
lastState = state;
digitalWrite(LED_BUILTIN, state);
if (state == HIGH) {
motionStart = millis();
Serial.println(F("Motion detected"));
} else {
Serial.print(F("Motion ended after "));
Serial.print((millis() - motionStart) / 1000);
Serial.println(F(" s"));
}
}
}Expected serial output
PIR warming up for 60 s - keep still...
PIR ready
Motion detected
Motion ended after 7 sPIR warming up for 60 s - keep still... PIR ready Motion detected Motion ended after 7 s
Common errors
Triggers every few seconds with nobody there
Still warming up, sensitivity too high, or the sensor faces a window, AC vent or heater. Turn Sx down and point it at a wall.
OUT never goes HIGH
Check VCC is 5V (not 3.3V — it needs at least 4.5 V), and that the three wires match the labels. On many boards the labels are printed under the white dome: lift it off gently to read them.
Stays HIGH for minutes
The Tx pot is turned clockwise. Turn it fully anticlockwise for the shortest time (about 3 s).