Bird
0
0

Given the following code snippet, what will be printed when the button connected to GPIO 22 is pressed once?

medium📝 Predict Output Q4 of 15
Raspberry Pi - LED and Button Projects

Given the following code snippet, what will be printed when the button connected to GPIO 22 is pressed once?

import RPi.GPIO as GPIO
import time

def on_button_press(channel):
    print('Pressed')

GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(22, GPIO.FALLING, callback=on_button_press, bouncetime=200)

while True:
    time.sleep(1)
ANothing will print because the callback is not called
BThe message 'Pressed' will print once when the button is pressed
CThe message 'Pressed' will print continuously in a loop
DAn error will occur due to missing GPIO.cleanup()
Step-by-Step Solution
Solution:
  1. Step 1: Setup analysis

    GPIO pin 22 is set as input with pull-up resistor.
  2. Step 2: Event detection

    Detects falling edge (button press) and calls on_button_press callback.
  3. Step 3: Callback effect

    Callback prints 'Pressed' once per press, with debounce of 200ms.
  4. Final Answer:

    The message 'Pressed' will print once when the button is pressed -> Option B
  5. Quick Check:

    Callback triggers once per press with debounce [OK]
Quick Trick: Falling edge triggers callback once per press [OK]
Common Mistakes:
  • Assuming callback never triggers
  • Thinking callback prints repeatedly without press
  • Expecting error without cleanup

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Raspberry Pi Quizzes