0
0
Raspberry Piprogramming~10 mins

Button class with callbacks in Raspberry Pi - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to import the GPIO library for Raspberry Pi.

Raspberry Pi
import [1] as GPIO
Drag options to blanks, or click blank then click option'
Asys
Bgpiozero
Ctime
DRPi.GPIO
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'gpiozero' instead of 'RPi.GPIO' when the task expects the GPIO library import.
Importing unrelated libraries like 'time' or 'sys'.
2fill in blank
medium

Complete the code to set the GPIO mode to BCM numbering.

Raspberry Pi
GPIO.setmode([1])
Drag options to blanks, or click blank then click option'
AGPIO.BCM
BGPIO.OUT
CGPIO.BOARD
DGPIO.IN
Attempts:
3 left
💡 Hint
Common Mistakes
Using GPIO.BOARD instead of GPIO.BCM.
Using GPIO.OUT or GPIO.IN which are for pin direction, not mode.
3fill in blank
hard

Fix the error in setting up a GPIO pin 17 as input with a pull-up resistor.

Raspberry Pi
GPIO.setup(17, [1], pull_up_down=GPIO.PUD_UP)
Drag options to blanks, or click blank then click option'
AGPIO.OUT
BGPIO.IN
CGPIO.PUD_DOWN
DGPIO.PUD_UP
Attempts:
3 left
💡 Hint
Common Mistakes
Setting pin as GPIO.OUT when using pull-up resistor.
Confusing pull_up_down parameter with pin direction.
4fill in blank
hard

Fill both blanks to add an event detection on pin 18 for falling edge and assign a callback function.

Raspberry Pi
GPIO.add_event_detect(18, [1], callback=[2])
Drag options to blanks, or click blank then click option'
AGPIO.FALLING
BGPIO.RISING
Cbutton_pressed
Dbutton_released
Attempts:
3 left
💡 Hint
Common Mistakes
Using GPIO.RISING instead of GPIO.FALLING for falling edge detection.
Using wrong callback function name.
5fill in blank
hard

Fill all three blanks to define a Button class with an __init__ method that sets pin, callback, and configures GPIO input with pull-up.

Raspberry Pi
class Button:
    def __init__(self, pin, callback):
        self.pin = [1]
        self.callback = [2]
        GPIO.setup(self.pin, GPIO.IN, pull_up_down=[3])
Drag options to blanks, or click blank then click option'
Apin
Bcallback
CGPIO.PUD_UP
DGPIO.PUD_DOWN
Attempts:
3 left
💡 Hint
Common Mistakes
Assigning wrong variables to self.pin or self.callback.
Using pull-down resistor instead of pull-up.