Bird
0
0

Identify the error in this code snippet for detecting a button press on GPIO 5: import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(5, GPIO.IN) if GPIO.input(5) == True: print("Button Pressed")

medium📝 Debug Q6 of 15
Raspberry Pi - LED and Button Projects
Identify the error in this code snippet for detecting a button press on GPIO 5: import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(5, GPIO.IN) if GPIO.input(5) == True: print("Button Pressed")
AIncorrect GPIO mode set
BWrong pin number used
CMissing pull-up or pull-down resistor setup
DSyntax error in if statement
Step-by-Step Solution
Solution:
  1. Step 1: Check GPIO setup

    Pin is set as input but no pull-up or pull-down resistor specified.
  2. Step 2: Understand effect of missing resistor

    Without resistor, pin state is undefined (floating), causing unreliable detection.
  3. Final Answer:

    Missing pull-up or pull-down resistor setup -> Option C
  4. Quick Check:

    Input pin needs pull resistor to avoid floating [OK]
Quick Trick: Always set pull-up/down resistor for input pins [OK]
Common Mistakes:
  • Ignoring pull resistor causes unstable readings
  • Thinking GPIO mode is wrong
  • Assuming syntax error in correct if statement

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes