Bird
0
0

Find the bug in this PIR sensor code snippet:

medium📝 Debug Q7 of 15
Raspberry Pi - gpiozero Library

Find the bug in this PIR sensor code snippet:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
if GPIO.input(21):
    print("Motion detected")
APin 21 should be set as input, not output
BGPIO.setmode is missing
Cprint statement syntax error
DGPIO.input cannot be used on pin 21
Step-by-Step Solution
Solution:
  1. Step 1: Check pin setup direction

    Pin connected to PIR sensor must be input, not output.
  2. Step 2: Identify mismatch in code

    Pin 21 is set as output but read as input, causing logic error.
  3. Final Answer:

    Pin 21 should be set as input, not output -> Option A
  4. Quick Check:

    PIR pin must be input to read sensor data [OK]
Quick Trick: Set PIR pin as input before reading with GPIO.input [OK]
Common Mistakes:
  • Setting PIR pin as output by mistake
  • Forgetting GPIO.setmode call
  • Misusing GPIO.input on output pins

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes