Programming Examples
Write a program to interface a PIR motion sensor and an LED with the Arduino. The LED should turn on for 5 seconds whenever motion is detected.

Write a program to interface a PIR motion sensor and an LED with the Arduino. The LED should turn on for 5 seconds whenever motion is detected.
Solutionvoid setup() {
pinMode(2, INPUT); // Set PIR sensor as input
pinMode(13, OUTPUT); // Set LED as output
Serial.begin(9600); // Start Serial Monitor
}
void loop() {
int motion = digitalRead(2); // Read PIR sensor
if (motion == HIGH) { // Motion detected
Serial.println("Motion Detected!");
digitalWrite(13, HIGH); // Turn ON LED
delay(5000); // Keep LED ON for 5 seconds
digitalWrite(13, LOW); // Turn OFF LED
}
}
▶ RUN Output/ Explanation: 