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.


O level January 2025 practical Exam question

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.

Solution

void 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:


Latest Current Affairs 2026 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad