PIR Motion Detector
Light an LED whenever motion is detected.
- Beginner
- Sensors
- 5 parts
Opens in your browser with the parts, wiring and code already in place. Press Run to watch it work, then change anything you like.
How it works
A PIR (passive infrared) sensor detects the heat given off by moving people and animals. Its output goes high while it senses motion and low when the scene is still. Here the output is wired to pin 7, and an LED with a resistor sits on pin 13.
loop() reads pin 7 twice a second. High means motion: the LED switches on and “Motion detected” is printed. Low switches the LED off and prints “No motion”. Select the sensor in the editor and toggle its motion to try both cases. Real PIR sensors usually hold their output high for a few seconds after movement, and need about a minute to settle after power-up.
Parts
- Arduino Uno
- PIR Motion Sensor
- Resistor
- LED
- GND
Wiring
- Arduino Uno 5V → PIR Motion Sensor VCC
- PIR Motion Sensor OUT → Arduino Uno D7
- PIR Motion Sensor GND → Arduino Uno GND
- Arduino Uno D13 → Resistor A
- Resistor B → LED Anode (+)
- LED Cathode (-) → GND
Arduino code
// PIR Motion Detector
void setup() {
pinMode(7, INPUT);
pinMode(13, OUTPUT);
Serial.begin(9600);
}
void loop() {
int motion = digitalRead(7);
if (motion == HIGH) {
digitalWrite(13, HIGH);
Serial.println("Motion detected");
} else {
digitalWrite(13, LOW);
Serial.println("No motion");
}
delay(500);
}
Try changing it
- Count detections: add int count = 0; at the top, then count++ and a Serial.println(count); inside the motion branch.
- Keep the light on for three seconds after motion by adding delay(3000); inside the motion branch.