B. GPIO.setup should use GPIO.IN instead of GPIO.OUT
C. time.sleep cannot be used with GPIO.output
D. Pin 22 cannot be used for output
Solution
Step 1: Check usage of HIGH and LOW constants
HIGH and LOW must be written as GPIO.HIGH and GPIO.LOW to be recognized.
Step 2: Verify other code parts
GPIO.setup with GPIO.OUT is correct; time.sleep works fine; pin 22 is valid.
Final Answer:
HIGH and LOW should be prefixed with GPIO. -> Option A
Quick Check:
HIGH/LOW require GPIO. prefix = D [OK]
Hint: Always prefix HIGH/LOW with GPIO. to avoid NameError [OK]
Common Mistakes:
Omitting GPIO. prefix for HIGH/LOW
Changing pin mode incorrectly
Thinking time.sleep causes error
5. You want to simulate a traffic light cycle with red, yellow, and green LEDs connected to GPIO pins 5, 6, and 13 respectively. Which Python code snippet correctly cycles through the lights with 2 seconds red, 1 second yellow, and 3 seconds green, repeating forever?
B. for light in [5,6,13]:
GPIO.output(light, GPIO.HIGH)
time.sleep(2)
GPIO.output(light, GPIO.LOW)
C. while True:
GPIO.output(5, GPIO.HIGH)
time.sleep(2)
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
time.sleep(1)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
time.sleep(3)
GPIO.output(13, GPIO.LOW)
D. while True:
GPIO.output(13, GPIO.HIGH)
time.sleep(3)
GPIO.output(13, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
time.sleep(1)
GPIO.output(6, GPIO.LOW)
GPIO.output(5, GPIO.HIGH)
time.sleep(2)
GPIO.output(5, GPIO.LOW)
Solution
Step 1: Check order and timing of lights
Traffic lights cycle red (2s), yellow (1s), green (3s) in that order repeatedly.
Step 2: Verify code matches timing and order
while True:
GPIO.output(5, GPIO.HIGH)
time.sleep(2)
GPIO.output(5, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
time.sleep(1)
GPIO.output(6, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
time.sleep(3)
GPIO.output(13, GPIO.LOW) uses while True loop, turns on red (pin 5) for 2s, then yellow (pin 6) for 1s, then green (pin 13) for 3s, turning off each after delay.
Final Answer:
while True loop with red (2s), yellow (1s), green (3s) -> Option C
Quick Check:
Correct order and timing in loop = A [OK]
Hint: Use while True and sequence GPIO.output with time.sleep [OK]
Common Mistakes:
Wrong order of lights
Using for loop without timing per light
Turning all LEDs on at once
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.