Bird
0
0

What is wrong with this code snippet for detecting a button press on GPIO 12?

medium📝 Debug Q7 of 15
Raspberry Pi - LED and Button Projects
What is wrong with this code snippet for detecting a button press on GPIO 12? import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(12, GPIO.OUT, pull_up_down=GPIO.PUD_UP) if GPIO.input(12) == 0: print("Pressed")
APin set as output instead of input
BPull-up resistor cannot be used with GPIO.OUT
CIncorrect comparison value in if statement
DMissing GPIO.cleanup() call
Step-by-Step Solution
Solution:
  1. Step 1: Check GPIO setup mode

    Pin 12 is set as output (GPIO.OUT), but button detection requires input mode.
  2. Step 2: Understand pull-up resistor usage

    Pull-up resistor applies only to input pins, so setting it with output is incorrect.
  3. Final Answer:

    Pin set as output instead of input -> Option A
  4. Quick Check:

    Button detection needs GPIO.IN, not GPIO.OUT [OK]
Quick Trick: Use GPIO.IN for button pins, not GPIO.OUT [OK]
Common Mistakes:
  • Setting output mode for input detection
  • Using pull-up resistor with output pin
  • Ignoring cleanup but that is not error here

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes