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
  }
}
Output


Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad