Bird
Raised Fist0
Raspberry Piprogramming~20 mins

Digital output (GPIO.output) in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
GPIO Output Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output state of the LED after running this code?

Consider this Raspberry Pi Python code controlling an LED connected to GPIO pin 18.

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

What will be printed?

Raspberry Pi
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
state = GPIO.input(18)
print(state)
A1
B0
CGPIO.HIGH
DNone
Attempts:
2 left
💡 Hint

GPIO.HIGH sets the pin to ON, so reading it back returns 1.

🧠 Conceptual
intermediate
1:30remaining
Which GPIO.output call will turn OFF the LED connected to pin 23?

You have an LED connected to GPIO pin 23. Which command will turn the LED OFF?

AGPIO.output(23, True)
BGPIO.output(23, GPIO.HIGH)
CGPIO.output(23, 1)
DGPIO.output(23, GPIO.LOW)
Attempts:
2 left
💡 Hint

LOW means OFF, HIGH means ON.

🔧 Debug
advanced
2:30remaining
Why does this code raise a RuntimeError?

Look at this code snippet:

import RPi.GPIO as GPIO
GPIO.output(17, GPIO.HIGH)

When run, it raises a RuntimeError: 'You must setup() the GPIO channel first'. Why?

Raspberry Pi
import RPi.GPIO as GPIO
GPIO.output(17, GPIO.HIGH)
ABecause GPIO.setup(17, GPIO.OUT) was not called before output()
BBecause GPIO.setmode() was not called before output()
CBecause GPIO.cleanup() was called before output()
DBecause GPIO.output() cannot be called with GPIO.HIGH
Attempts:
2 left
💡 Hint

You must prepare the pin as output before setting its value.

📝 Syntax
advanced
1:30remaining
Which option contains a syntax error in using GPIO.output?

Identify the code snippet that will cause a syntax error when trying to set GPIO pin 5 to LOW.

AGPIO.output(5, 0)
BGPIO.output(5 GPIO.LOW)
CGPIO.output(5, GPIO.LOW)
DGPIO.output(5, False)
Attempts:
2 left
💡 Hint

Check for missing commas or parentheses.

🚀 Application
expert
3:00remaining
How many times will the LED blink in this code?

Given this code to blink an LED connected to GPIO pin 12:

import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
for i in range(5):
    GPIO.output(12, GPIO.HIGH)
    time.sleep(0.2)
    GPIO.output(12, GPIO.LOW)
    time.sleep(0.2)
GPIO.cleanup()

How many times does the LED turn ON (blink) before the program ends?

Raspberry Pi
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(12, GPIO.OUT)
for i in range(5):
    GPIO.output(12, GPIO.HIGH)
    time.sleep(0.2)
    GPIO.output(12, GPIO.LOW)
    time.sleep(0.2)
GPIO.cleanup()
A1
B10
C5
D0
Attempts:
2 left
💡 Hint

Each loop cycle turns the LED ON once.

Practice

(1/5)
1. What does GPIO.output(pin, GPIO.HIGH) do on a Raspberry Pi?
easy
A. It sets the pin mode to input.
B. It turns the specified pin ON by setting it to HIGH voltage.
C. It reads the current value from the specified pin.
D. It resets the Raspberry Pi GPIO pins.

Solution

  1. Step 1: Understand GPIO.output function

    The function GPIO.output(pin, value) sets the voltage level of the pin.
  2. Step 2: Interpret GPIO.HIGH

    GPIO.HIGH means turning the pin ON by applying a high voltage.
  3. Final Answer:

    It turns the specified pin ON by setting it to HIGH voltage. -> Option B
  4. Quick Check:

    GPIO.output with GPIO.HIGH = Pin ON [OK]
Hint: HIGH means ON, LOW means OFF for GPIO pins [OK]
Common Mistakes:
  • Confusing output with input reading
  • Thinking GPIO.HIGH reads pin value
  • Assuming it sets pin mode
2. Which of the following is the correct syntax to set GPIO pin 18 as an output pin?
easy
A. GPIO.setup(18, GPIO.INPUT)
B. GPIO.output(18, GPIO.HIGH)
C. GPIO.setup(18, GPIO.OUT)
D. GPIO.output(18, GPIO.OUT)

Solution

  1. Step 1: Identify the function to set pin mode

    GPIO.setup(pin, mode) is used to set pin mode.
  2. Step 2: Choose correct mode for output

    GPIO.OUT sets the pin as output, so GPIO.setup(18, GPIO.OUT) is correct.
  3. Final Answer:

    GPIO.setup(18, GPIO.OUT) -> Option C
  4. Quick Check:

    Pin mode output = GPIO.setup(pin, GPIO.OUT) [OK]
Hint: Use GPIO.setup(pin, GPIO.OUT) to set output mode [OK]
Common Mistakes:
  • Using GPIO.INPUT instead of GPIO.OUT
  • Trying to set output with GPIO.output before setup
  • Using GPIO.output to set pin mode
3. What will be the output state of pin 23 after running this code?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(23, GPIO.OUT)
GPIO.output(23, GPIO.LOW)
medium
A. Code will cause a runtime error.
B. Pin 23 will be ON (HIGH voltage).
C. Pin 23 will be set as input.
D. Pin 23 will be OFF (LOW voltage).

Solution

  1. Step 1: Analyze pin setup

    Pin 23 is set as output using GPIO.setup(23, GPIO.OUT).
  2. Step 2: Check output command

    GPIO.output(23, GPIO.LOW) sets pin 23 to LOW voltage, turning it OFF.
  3. Final Answer:

    Pin 23 will be OFF (LOW voltage). -> Option D
  4. Quick Check:

    GPIO.output(pin, GPIO.LOW) = Pin OFF [OK]
Hint: LOW means OFF, so pin is off after output LOW [OK]
Common Mistakes:
  • Assuming LOW means ON
  • Forgetting to set pin as output first
  • Confusing pin number with mode
4. Identify the error in this code snippet:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.output(17, GPIO.HIGH)
medium
A. Pin 17 is not set as output before using GPIO.output.
B. GPIO.HIGH is not a valid value for GPIO.output.
C. GPIO.setmode(GPIO.BCM) is incorrect syntax.
D. Missing import statement for GPIO.

Solution

  1. Step 1: Check pin setup before output

    The code calls GPIO.output without first setting pin 17 as output using GPIO.setup(17, GPIO.OUT).
  2. Step 2: Confirm other parts are correct

    GPIO.HIGH and GPIO.setmode(GPIO.BCM) are correct, and import is present.
  3. Final Answer:

    Pin 17 is not set as output before using GPIO.output. -> Option A
  4. Quick Check:

    Must setup pin as output before GPIO.output [OK]
Hint: Always setup pin as output before writing HIGH/LOW [OK]
Common Mistakes:
  • Skipping GPIO.setup for output pins
  • Confusing GPIO.HIGH with pin mode
  • Incorrect setmode usage
5. You want to blink an LED connected to pin 12 five times. Which code snippet correctly turns the LED ON and OFF using GPIO.output inside a loop?
hard
A. for i in range(5): GPIO.output(12, GPIO.HIGH) time.sleep(1) GPIO.output(12, GPIO.LOW) time.sleep(1)
B. for i in range(5): GPIO.output(12, GPIO.LOW) time.sleep(1) GPIO.output(12, GPIO.HIGH) time.sleep(1)
C. GPIO.output(12, GPIO.HIGH) for i in range(5): time.sleep(1) GPIO.output(12, GPIO.LOW) time.sleep(1)
D. for i in range(5): GPIO.setup(12, GPIO.OUT) GPIO.output(12, GPIO.HIGH) GPIO.output(12, GPIO.LOW)

Solution

  1. Step 1: Understand blinking logic

    Blinking means turning LED ON (HIGH), waiting, then OFF (LOW), waiting, repeated 5 times.
  2. Step 2: Analyze each option

    for i in range(5): GPIO.output(12, GPIO.HIGH) time.sleep(1) GPIO.output(12, GPIO.LOW) time.sleep(1) correctly loops 5 times, sets pin HIGH, waits, sets LOW, waits. for i in range(5): GPIO.output(12, GPIO.LOW) time.sleep(1) GPIO.output(12, GPIO.HIGH) time.sleep(1) reverses ON/OFF order. GPIO.output(12, GPIO.HIGH) for i in range(5): time.sleep(1) GPIO.output(12, GPIO.LOW) time.sleep(1) turns ON once before loop. for i in range(5): GPIO.setup(12, GPIO.OUT) GPIO.output(12, GPIO.HIGH) GPIO.output(12, GPIO.LOW) wrongly calls setup inside loop.
  3. Final Answer:

    for i in range(5): GPIO.output(12, GPIO.HIGH) time.sleep(1) GPIO.output(12, GPIO.LOW) time.sleep(1) -> Option A
  4. Quick Check:

    Blink = ON + wait + OFF + wait in loop [OK]
Hint: Turn ON then OFF with delays inside loop to blink [OK]
Common Mistakes:
  • Calling GPIO.setup inside loop repeatedly
  • Reversing ON/OFF order
  • Not including delays between ON and OFF
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard