You want to run a GPIO program that blinks an LED and always cleans up pins on exit, even on errors. Which code snippet correctly achieves this?
Aimport RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT)
for _ in range(5):
GPIO.output(21, True)
time.sleep(0.5)
GPIO.output(21, False)
time.sleep(0.5)
GPIO.cleanup()
Bimport RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
try:
GPIO.setup(21, GPIO.OUT)
for _ in range(5):
GPIO.output(21, True)
time.sleep(0.5)
GPIO.output(21, False)
time.sleep(0.5)
finally:
GPIO.cleanup()
Cimport RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
try:
GPIO.setup(21, GPIO.OUT)
while True:
GPIO.output(21, True)
time.sleep(0.5)
GPIO.output(21, False)
time.sleep(0.5)
except:
GPIO.cleanup()
Dimport RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
try:
GPIO.setup(21, GPIO.OUT)
for _ in range(5):
GPIO.output(21, True)
time.sleep(0.5)
GPIO.output(21, False)
time.sleep(0.5)