0
0
Raspberry Piprogramming~10 mins

Button with interrupt (GPIO.add_event_detect) 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.

Raspberry Pi
import [1]
Drag options to blanks, or click blank then click option'
Agpiozero
BRPi.GPIO
Ctime
Dos
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'gpiozero' instead of 'RPi.GPIO'.
Forgetting to import the GPIO library.
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.BOARD
BGPIO.IN
CGPIO.BCM
DGPIO.OUT
Attempts:
3 left
💡 Hint
Common Mistakes
Using GPIO.BOARD instead of GPIO.BCM.
Confusing input/output modes with numbering modes.
3fill in blank
hard

Fix the error in setting up the button pin as input with pull-up resistor.

Raspberry Pi
GPIO.setup(18, [1], pull_up_down=GPIO.PUD_UP)
Drag options to blanks, or click blank then click option'
AGPIO.PUD_UP
BGPIO.PUD_DOWN
CGPIO.OUT
DGPIO.IN
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the pin as output instead of input.
Confusing pull-up/down with pin mode.
4fill in blank
hard

Fill both blanks to add an event detect for a falling edge on pin 18 with 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.
Using the wrong callback function name.
5fill in blank
hard

Fill all three blanks to define a callback function that prints 'Button pressed!' when called.

Raspberry Pi
def [1](channel):
    print([2])

GPIO.add_event_detect(18, GPIO.FALLING, callback=[3])
Drag options to blanks, or click blank then click option'
Abutton_pressed
B"Button pressed!"
Dbutton_released
Attempts:
3 left
💡 Hint
Common Mistakes
Mismatching function name and callback.
Forgetting quotes around the print message.