Consider this Python code controlling an LED connected to GPIO pin 17 on a Raspberry Pi. What will be the state of the LED after running this code?
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.output(17, GPIO.HIGH) time.sleep(1) GPIO.output(17, GPIO.LOW) GPIO.cleanup()
GPIO.HIGH turns the LED on, GPIO.LOW turns it off.
The code sets the LED pin high (on), waits 1 second, then sets it low (off) and cleans up. So the LED lights up for 1 second only.
In Raspberry Pi Python code, what does GPIO.setmode(GPIO.BCM) mean?
BCM refers to the chip's internal numbering.
GPIO.BCM means the code uses the Broadcom SOC channel numbers, not the physical pin numbers on the board.
What error will this Raspberry Pi LED control code produce?
import RPi.GPIO as GPIO GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) GPIO.output(17, 2) GPIO.cleanup()
GPIO.output expects GPIO.HIGH or GPIO.LOW (0 or 1), not 2.
GPIO.output expects 0 or 1 (GPIO.LOW or GPIO.HIGH). Passing 2 causes a ValueError for invalid output value.
Fix the syntax error in this Raspberry Pi LED control code snippet:
GPIO.setup(17 GPIO.OUT)
Function arguments must be separated by commas.
The correct syntax uses a comma between arguments: GPIO.setup(17, GPIO.OUT).
Given this Raspberry Pi Python code, how many times will the LED blink?
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.OUT) for i in range(3): GPIO.output(17, GPIO.HIGH) time.sleep(0.5) GPIO.output(17, GPIO.LOW) time.sleep(0.5) GPIO.cleanup()
Each loop cycle turns the LED on and off once.
The loop runs 3 times, each time turning the LED on then off once, so 3 blinks total.