Motion-Activated Light (PIR + Arduino)
Turn an LED on when a PIR sensor detects movement. Learn digital input from a real-world sensor.
Beginner15 min
What you need
Wiring
Hover a pin or a wire to trace the connection.
Code
pir_light.ino
// Light an LED whenever the PIR sensor detects motion.
const int pirPin = 2;
const int ledPin = 13;
void setup() {
pinMode(pirPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
if (digitalRead(pirPin) == HIGH) { // motion detected
digitalWrite(ledPin, HIGH);
Serial.println("Motion!");
} else {
digitalWrite(ledPin, LOW);
}
delay(100);
}
Steps
- Wire the PIR: VCC to 5V, GND to GND, OUT to D2.
- Wire the LED on D13 through a 220Ω resistor, cathode to GND.
- Upload, then let the sensor settle ~30–60s.
- Wave your hand — the LED lights. Tune the sensor's two pots for range and hold time.