Bird
0
0

What will be printed by this code if the button connected to pin 21 is pressed (assuming pull-down resistor setup)?

medium📝 Predict Output Q4 of 15
Raspberry Pi - GPIO Basics with Python
What will be printed by this code if the button connected to pin 21 is pressed (assuming pull-down resistor setup)?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
if GPIO.input(21):
    print("Button Pressed")
else:
    print("Button Released")
AError: Pin not set as input
BButton Pressed
CButton Released
DNo output
Step-by-Step Solution
Solution:
  1. Step 1: Understand pull-down resistor effect

    Pull-down resistor keeps pin LOW (0) when button is not pressed.
  2. Step 2: Button press changes pin state

    Pressing button connects pin to HIGH (1), so GPIO.input(21) returns True.
  3. Final Answer:

    Button Pressed -> Option B
  4. Quick Check:

    Pressed button reads HIGH = prints 'Button Pressed' [OK]
Quick Trick: Pressed button with pull-down reads HIGH (1) [OK]
Common Mistakes:
  • Confusing pull-up with pull-down
  • Expecting 'Button Released' when pressed
  • Not setting pull resistor
  • Ignoring GPIO.input return value

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes