The light that switches on by itself when you walk into the hallway, alarm systems, and cameras that start recording when someone passes: most run on a PIR (passive infrared) sensor. It's one of the easiest sensors to use: an output pin that goes HIGH when there's motion.
How does it work?
Any warm body (a person or an animal) emits infrared. The sensor has two elements side by side, and the white dome-shaped lens divides the space in front of it into many zones.
When a warm body moves from one zone to another, the two elements see a difference, so the sensor outputs a signal.
"Passive" means it doesn't transmit anything; it only receives. That's why:
- It doesn't detect a person standing completely still for a long time.
- It can't see through ordinary glass, because glass blocks most of this radiation.
An important consequence of how it works: the sensor detects motion across it (passing from zone to zone) much better than motion straight towards it. So if you install it in a hallway, place it so people walk across its view rather than towards it.
The HC-SR501 module
- Supply: about 5 to 20 V (it has a regulator inside).
- Output: about 3.3 V HIGH, so it works with the Arduino and ESP32 directly.
- Two potentiometers:
- Sensitivity (distance): up to about 7 m.
- Time: how long the output stays HIGH after motion, from seconds to minutes.
- A jumper:
- H (repeat): as long as there's motion, the time is renewed. This is what you'll usually need.
- L (single): the output goes high once and returns, even if the motion continues.
A tip: if the Arduino will decide how long the light stays on, turn the time potentiometer to minimum (about 3 seconds), and let the code control the timing. That way you change the duration in code instead of taking the sensor apart.
The blocking period
After the output returns LOW, the sensor ignores any motion for about 2–3 seconds. This is so it doesn't trigger itself. So if someone moves during those seconds, it won't detect them. That's normal, but you need to know it while testing.
Field of view and mounting
- The angle is about 100–120° in front of the dome.
- The best height is about 2–2.5 m, tilted slightly downwards.
- If you want to narrow the area, cover part of the dome with tape. That stops the sensor seeing the street or a window, for example.
Wiring and code
VCC to 5V, GND, and OUT to any input pin:
const int PIR = 2;
void setup() { pinMode(PIR, INPUT); Serial.begin(9600); }
void loop() {
if (digitalRead(PIR) == HIGH) Serial.println("Motion!");
delay(200);
}
A light that switches itself off
The light switches on with motion and stays on for 30 seconds after the last motion, without the code stopping. Using millis:
const int PIR = 2, LIGHT = 7;
const unsigned long HOLD = 30000;
unsigned long lastMotion = 0;
void loop() {
if (digitalRead(PIR) == HIGH) lastMotion = millis();
bool on = (lastMotion != 0) && (millis() - lastMotion < HOLD);
digitalWrite(LIGHT, on);
}
And so the light doesn't come on during the day, add an LDR light sensor and only let it work when it's dark.
Important tips
- Warm-up time: the sensor needs about a minute after power-on to settle. Early readings may be random.
- False alarms: air conditioning, heaters, direct sunlight, and curtains moving in the breeze. Point the sensor away from heat sources.
- A clean supply: interference from a relay or Wi-Fi can produce false signals. Put a capacitor on the supply, and keep the sensor away from the ESP32's antenna.
- To switch a lamp: the output → a transistor and relay, or a ready-made relay module. And if it's on mains, mind safety.
Other motion sensors
| Sensor | Detects | Advantages | Drawbacks |
|---|---|---|---|
| HC-SR501 | Moving heat | Cheap, and adjustable by hand | A bit large |
| AM312 | Moving heat | Very small, and runs on 3.3 V | No adjustments |
| RCWL-0516 (radar) | Any motion | Sees through plastic and thin wood | More false alarms, and detects motion behind walls |
The radar can be mounted inside a sealed box, which is a big advantage. But it detects things like a spinning fan or someone behind a thin wall, so it needs testing in place.
On a battery
A PIR draws very little current while waiting. And with an ESP32 in deep sleep, you can let the sensor wake the board when there's motion, so the battery lasts for months. Connect OUT to an RTC pin and use esp_sleep_enable_ext0_wakeup with a HIGH level. The details are in the ESP32 deep sleep article.
Common faults
| Fault | Usual cause |
|---|---|
| Always on | Sensitivity too high, a heat source in front of it, or still warming up |
| Triggers by itself now and then | Interference from a relay or Wi-Fi on the supply |
| Doesn't detect someone walking towards it | Motion towards the sensor is weaker; change its angle |
| The output stays HIGH for minutes | The time potentiometer is set high |
| It can't detect through a window | Glass blocks the radiation |
FAQ
Is it suitable for a burglar alarm? For a learning project, yes, and it's the principle inside real devices. For serious protection, commercial devices have more than one sensor and tamper protection.
Do pets trigger it? Yes, because they're warm and they move. Reduce the sensitivity, or point the sensor slightly upwards.