Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the GPIO library.
Raspberry Pi
import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing 'time' instead of the GPIO library.
Using 'os' which is unrelated to GPIO control.
✗ Incorrect
We use RPi.GPIO to control GPIO pins on Raspberry Pi.
2fill in blank
mediumComplete 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'
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.
✗ Incorrect
BCM mode uses the Broadcom chip pin numbers.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using GPIO.OUT instead of GPIO.IN.
Confusing pull-up/down constants with pin direction.
✗ Incorrect
Pin 17 must be set as input to detect button presses.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Checking for GPIO.HIGH instead of GPIO.LOW.
Using wrong pin number like 18.
✗ Incorrect
We read pin 17 and check if it is LOW because the button pulls the pin to ground when pressed.
5fill in blank
hardFill 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'
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.
✗ Incorrect
Pin 17 is set up as input with pull-up. Event detection triggers on falling edge (button press) and calls button_callback.