Bird
0
0

What will be printed by this Python code when the PIR sensor detects motion?

medium📝 Predict Output Q4 of 15
Raspberry Pi - gpiozero Library

What will be printed by this Python code when the PIR sensor detects motion?

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(4, GPIO.IN)

if GPIO.input(4):
    print("Motion detected!")
else:
    print("No motion")
AMotion detected!
BNo motion
CGPIO.input error
DSyntax error
Step-by-Step Solution
Solution:
  1. Step 1: Understand GPIO.input behavior

    GPIO.input(4) returns True if motion is detected by PIR sensor on pin 4.
  2. Step 2: Check the if condition

    If True, prints "Motion detected!"; else prints "No motion".
  3. Final Answer:

    Motion detected! -> Option A
  4. Quick Check:

    GPIO.input True means motion detected [OK]
Quick Trick: GPIO.input returns True when PIR detects motion [OK]
Common Mistakes:
  • Assuming GPIO.input returns string
  • Confusing pin number or mode
  • Ignoring indentation causing syntax errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes