0
0
Raspberry Piprogramming~10 mins

Button press detection 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'
Atime
BRPi.GPIO
Cgpiozero
Dos
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'time' instead of the GPIO library.
Using 'os' which is unrelated to GPIO control.
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.BOARD
CGPIO.OUT
DGPIO.IN
Attempts:
3 left
💡 Hint
Common Mistakes
Using GPIO.BOARD which refers to physical pin numbers.
Using GPIO.OUT or GPIO.IN which are for pin direction.
3fill in blank
hard

Fix the error in setting up pin 17 as input with 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.IN
BGPIO.PUD_DOWN
CGPIO.OUT
DGPIO.PUD_OFF
Attempts:
3 left
💡 Hint
Common Mistakes
Using GPIO.OUT instead of GPIO.IN.
Confusing pull-up/down constants with pin direction.
4fill in blank
hard

Fill both blanks to read the button state and print if pressed.

Raspberry Pi
button_state = GPIO.input([1])
if button_state == [2]:
    print('Button pressed!')
Drag options to blanks, or click blank then click option'
A17
BGPIO.LOW
CGPIO.HIGH
D18
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for GPIO.HIGH instead of GPIO.LOW.
Using wrong pin number like 18.
5fill in blank
hard

Fill all three blanks to set up a button press event detection with a callback.

Raspberry Pi
def button_callback(channel):
    print('Button was pressed!')

GPIO.setup([1], GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect([2], GPIO.FALLING, callback=[3], bouncetime=200)
Drag options to blanks, or click blank then click option'
A17
Bbutton_callback
CGPIO.RISING
DGPIO.FALLING
Attempts:
3 left
💡 Hint
Common Mistakes
Using GPIO.RISING instead of GPIO.FALLING for button press.
Using different pin numbers in setup and event detection.
Not passing the callback function correctly.