0
0
Raspberry Piprogramming~20 mins

Setting pin mode (IN, OUT) in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GPIO Pin Mode Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this GPIO pin mode setup code?

Consider this Python code using the RPi.GPIO library to set a pin mode and read its state:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN)
state = GPIO.input(18)
print(state)

What will this code print if the pin 18 is connected to ground?

Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN)
state = GPIO.input(18)
print(state)
A0
BRaises RuntimeError
CGPIO.IN
D1
Attempts:
2 left
💡 Hint

Remember, when a pin is set as input and connected to ground, its state reads as low.

Predict Output
intermediate
2:00remaining
What happens when you set a pin as output and write HIGH?

Given this code snippet:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
GPIO.output(23, GPIO.HIGH)
print(GPIO.input(23))

What will be printed?

Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
GPIO.output(23, GPIO.HIGH)
print(GPIO.input(23))
A0
BRaises RuntimeError
C1
DGPIO.HIGH
Attempts:
2 left
💡 Hint

Output pins reflect the value you set on them.

🔧 Debug
advanced
2:00remaining
Identify the error in this pin setup code

Look at this code snippet:

import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, 1)
GPIO.setup(17, GPIO.IN)
print(GPIO.input(17))

What error will this code cause when run?

Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, 1)
GPIO.setup(17, GPIO.IN)
print(GPIO.input(17))
ANo error, prints 0 or 1 depending on pin state
BRuntimeWarning: Channel already in use
CRuntimeError: You must setup GPIO before using it
DTypeError: invalid argument type
Attempts:
2 left
💡 Hint

Think about setting up the same pin twice without cleanup.

🧠 Conceptual
advanced
1:30remaining
Which statement about GPIO pin modes is true?

Choose the correct statement about setting GPIO pin modes on Raspberry Pi:

APins set as output automatically detect input voltage
BPins set as output can read external voltage levels
CPins set as input can drive voltage to external devices
DPins set as input can only read voltage levels, not drive voltage
Attempts:
2 left
💡 Hint

Think about the difference between input and output pins.

Predict Output
expert
2:30remaining
What is the output of this GPIO pin toggle code?

Consider this code that toggles a pin state:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.OUT)
GPIO.output(27, GPIO.LOW)
time.sleep(0.1)
GPIO.output(27, not GPIO.input(27))
print(GPIO.input(27))

What will be printed?

Raspberry Pi
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(27, GPIO.OUT)
GPIO.output(27, GPIO.LOW)
time.sleep(0.1)
GPIO.output(27, not GPIO.input(27))
print(GPIO.input(27))
A1
B0
CRaises RuntimeError
DRaises TypeError
Attempts:
2 left
💡 Hint

Think about toggling the pin state from LOW to HIGH.