0
0
Raspberry Piprogramming~20 mins

GPIO cleanup on exit in Raspberry Pi - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
GPIO Cleanup Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
1:30remaining
What is the output when GPIO cleanup is called?
Consider this Python code running on a Raspberry Pi using the RPi.GPIO library. What will be printed when the program exits?
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)

try:
    GPIO.output(18, GPIO.HIGH)
    print("LED ON")
    time.sleep(1)
finally:
    GPIO.cleanup()
    print("GPIO cleaned up")
ALED ON
BNo output
CGPIO cleaned up
DLED ON\nGPIO cleaned up
Attempts:
2 left
💡 Hint
Look at what happens in the finally block after the try block.
Predict Output
intermediate
1:00remaining
What happens if GPIO.cleanup() is omitted?
What is the likely effect if a Raspberry Pi Python script using GPIO pins does NOT call GPIO.cleanup() before exiting?
AGPIO pins remain set and may cause warnings on next run
BProgram crashes immediately with an error
CGPIO pins reset automatically with no issues
DThe Raspberry Pi shuts down
Attempts:
2 left
💡 Hint
Think about what happens to GPIO pin states after the program ends.
🔧 Debug
advanced
2:00remaining
Identify the error preventing GPIO cleanup on exit
This code aims to clean up GPIO pins on program exit but fails to do so. What is the error?
Raspberry Pi
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)

try:
    GPIO.output(17, GPIO.HIGH)
    time.sleep(2)
except KeyboardInterrupt:
    print("Interrupted")
finally:
    GPIO.cleanup()
    print("Cleanup done")
AThe except block should be after finally
BGPIO.cleanup is missing parentheses, so cleanup is not called
Ctime.sleep should be inside finally block
DGPIO.setup is called after setmode, which is incorrect
Attempts:
2 left
💡 Hint
Check how functions are called in Python.
🧠 Conceptual
advanced
1:30remaining
Why use try-finally for GPIO cleanup?
Why is it recommended to use a try-finally block when working with GPIO pins in Python on Raspberry Pi?
ATo ensure GPIO.cleanup() runs even if an error or interrupt occurs
BTo speed up the GPIO pin setup process
CTo prevent the program from printing output
DTo automatically detect hardware failures
Attempts:
2 left
💡 Hint
Think about what finally blocks guarantee in Python.
Predict Output
expert
2:30remaining
What is the output of this GPIO cleanup signal handler code?
This Python code sets a signal handler to clean up GPIO on Ctrl+C. What will be printed when the user presses Ctrl+C?
Raspberry Pi
import RPi.GPIO as GPIO
import signal
import sys

GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.OUT)

GPIO.output(22, GPIO.HIGH)
print("LED ON")

def cleanup_handler(sig, frame):
    GPIO.cleanup()
    print("Cleaned up and exiting")
    sys.exit(0)

signal.signal(signal.SIGINT, cleanup_handler)

print("Press Ctrl+C to exit")
while True:
    pass
APress Ctrl+C to exit\nCleaned up and exiting
BLED ON\nCleaned up and exiting
CLED ON\nPress Ctrl+C to exit\nCleaned up and exiting
DNo output
Attempts:
2 left
💡 Hint
Consider the order of print statements and when the signal handler runs.