3. What will be the output on the LED if the following Python code runs on a Raspberry Pi with 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.LOW)
medium
A. The LED will turn OFF
B. The LED will blink
C. The code will cause an error
D. The LED will turn ON
Solution
Step 1: Analyze the GPIO setup
Pin 18 is set as an output pin with GPIO.setup(18, GPIO.OUT).
Step 2: Check the output value
GPIO.output(18, GPIO.LOW) sets the pin to low voltage, turning the LED off.
Final Answer:
The LED will turn OFF -> Option A
Quick Check:
GPIO.LOW means LED off = D [OK]
Hint: LOW means off, HIGH means on for LEDs [OK]
Common Mistakes:
Thinking LOW turns LED on
Assuming code blinks LED without loop
Expecting an error from correct code
4. The following code is intended to turn on an LED when a button is pressed, but it does not work. What is the main error?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(22, GPIO.IN)
GPIO.setup(23, GPIO.OUT)
if GPIO.input(23) == GPIO.HIGH:
GPIO.output(22, GPIO.HIGH)
medium
A. The button pin is not set as output.
B. GPIO.setmode is missing and causes error.
C. The code uses GPIO.LOW instead of GPIO.HIGH.
D. The input and output pins are swapped in the code.
Solution
Step 1: Check pin setup roles
Pin 22 is set as input (button), pin 23 as output (LED).
Step 2: Analyze the if condition and output
The code reads GPIO.input(23) (LED pin) instead of button pin 22, and tries to write to input pin 22, which is wrong.
Final Answer:
The input and output pins are swapped in the code. -> Option D
Quick Check:
Input pin must be read, output pin must be written = A [OK]
Hint: Input pins read button, output pins control LED [OK]
Common Mistakes:
Reading output pin instead of input pin
Writing to input pin instead of output pin
Confusing pin numbers for button and LED
5. You want to build a project where pressing a button toggles an LED on and off each time. Which approach best builds hardware confidence using Raspberry Pi?
hard
A. Set the LED pin as input and button pin as output to simplify wiring.
B. Write code that turns LED on and off randomly without reading the button.
C. Use a loop to read the button state and change LED state only when button press is detected.
D. Connect the LED directly to 5V without using GPIO pins.
Solution
Step 1: Understand the toggle behavior
Pressing the button should change the LED state from on to off or off to on, requiring reading button input and controlling LED output.
Step 2: Identify correct wiring and code logic
Setting LED as output and button as input is correct. Using a loop to detect button press and toggle LED builds understanding of input-output interaction.
Final Answer:
Use a loop to read the button state and change LED state only when button press is detected. -> Option C
Quick Check:
Button input controls LED output toggle = B [OK]
Hint: Toggle needs reading button and changing LED state in loop [OK]
Common Mistakes:
Swapping input/output pin roles
Ignoring button input in code
Connecting LED directly to power without GPIO
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.