0
0
Raspberry Piprogramming~10 mins

Digital input (GPIO.input) 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 read the state of a GPIO pin.

Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
state = GPIO.[1](17)
print(state)
Drag options to blanks, or click blank then click option'
Ainput
Boutput
Cinput_read
Dread
Attempts:
3 left
💡 Hint
Common Mistakes
Using GPIO.output() instead of GPIO.input()
Trying to read a pin not set as input
2fill in blank
medium

Complete the code to check if the button connected to pin 22 is pressed (assuming active low).

Raspberry Pi
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_UP)
if GPIO.[1](22) == GPIO.LOW:
    print("Button pressed")
Drag options to blanks, or click blank then click option'
Aoutput
Binput
Cinput_read
Attempts:
3 left
💡 Hint
Common Mistakes
Using GPIO.output() instead of GPIO.input()
Not setting pull-up resistor for active low button
3fill in blank
hard

Fix the error in the code to correctly read a pin state.

Raspberry Pi
GPIO.setup(5, GPIO.IN)
pin_state = GPIO.[1](5)
if pin_state == 1:
    print("Pin is HIGH")
Drag options to blanks, or click blank then click option'
Aread
Binput_read
Cinput
Doutput
Attempts:
3 left
💡 Hint
Common Mistakes
Using GPIO.output() or GPIO.read() which do not exist
Confusing pin setup with reading
4fill in blank
hard

Fill both blanks to create a dictionary of pin states for pins 10 and 11.

Raspberry Pi
pins = [10, 11]
states = {pin: GPIO.[1](pin) for pin in pins if GPIO.[2](pin) == GPIO.HIGH}
Drag options to blanks, or click blank then click option'
Ainput
Boutput
Attempts:
3 left
💡 Hint
Common Mistakes
Using GPIO.output() to read pin state
Mixing input and output functions
5fill in blank
hard

Fill all three blanks to create a dictionary of pin states for pins 7, 8, and 9 where the state is HIGH.

Raspberry Pi
pins = [7, 8, 9]
high_pins = {pin: GPIO.[1](pin) for pin in pins if GPIO.[2](pin) == GPIO.[3]
Drag options to blanks, or click blank then click option'
Ainput
CHIGH
DLOW
Attempts:
3 left
💡 Hint
Common Mistakes
Using GPIO.output() instead of GPIO.input()
Comparing to GPIO.LOW instead of GPIO.HIGH