Bird
0
0

How would you modify this code to avoid random readings on an input pin 20 connected to a button?

hard📝 Application Q9 of 15
Raspberry Pi - GPIO Basics with Python
How would you modify this code to avoid random readings on an input pin 20 connected to a button?
GPIO.setup(20, GPIO.IN)
print(GPIO.input(20))
AAdd GPIO.output(20, GPIO.LOW) before reading
BSet pin 20 as output instead of input
CUse pull-up or pull-down resistor in setup: GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
DCall GPIO.cleanup() before setup
Step-by-Step Solution
Solution:
  1. Step 1: Understand floating input problem

    Input pins without pull-up/down resistors can read random values.
  2. Step 2: Use pull-up/down resistor in setup

    Adding pull_up_down=GPIO.PUD_UP or GPIO.PUD_DOWN stabilizes input readings.
  3. Final Answer:

    Use pull-up or pull-down resistor in setup: GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP) -> Option C
  4. Quick Check:

    Pull-up/down resistor stabilizes input pin readings [OK]
Quick Trick: Add pull_up_down parameter to stabilize input [OK]
Common Mistakes:
  • Setting input pin as output
  • Trying to write output on input pin

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes