Bird
Raised Fist0
Raspberry Piprogramming~10 mins

Single LED control in Raspberry Pi - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Single LED control
Start Program
↓
Setup GPIO Pin as Output
↓
Turn LED ON
↓
Wait for a moment
↓
Turn LED OFF
↓
Cleanup GPIO
↓
End Program
The program sets up the GPIO pin, turns the LED on, waits, turns it off, cleans up, then ends.
Execution Sample
Raspberry Pi
import RPi.GPIO as GPIO
import time

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

GPIO.output(18, GPIO.HIGH)  # LED ON

time.sleep(1)  # wait 1 second

GPIO.output(18, GPIO.LOW)   # LED OFF

GPIO.cleanup()
This code turns an LED connected to pin 18 on for 1 second, then turns it off and cleans up.
Execution Table
StepActionPin 18 StateOutput/Effect
1Set GPIO mode to BCMN/AGPIO library ready to use BCM numbering
2Setup pin 18 as outputN/APin 18 ready to control LED
3Set pin 18 HIGHHIGHLED turns ON
4Wait 1 secondHIGHLED stays ON for 1 second
5Set pin 18 LOWLOWLED turns OFF
6Cleanup GPION/AGPIO pins reset to default
7Program endsN/ALED remains OFF, program stops
💡 Program ends after cleaning up GPIO, LED is OFF
Variable Tracker
VariableStartAfter Step 3After Step 5Final
Pin 18 StateN/AHIGHLOWLOW
Key Moments - 3 Insights
Why do we need to call GPIO.cleanup() at the end?
GPIO.cleanup() resets the pins so they don't stay in output mode, preventing unexpected behavior. See step 6 in execution_table.
What happens if we skip the time.sleep(1) line?
The LED would turn ON and immediately OFF, so you might not see it light up. This is shown between steps 3 and 5.
Why do we use GPIO.setmode(GPIO.BCM)?
It tells the program to use the Broadcom pin numbering, matching the physical pin 18. This is step 1 in execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the state of pin 18 after step 3?
ALOW
BN/A
CHIGH
DUndefined
💡 Hint
Check the 'Pin 18 State' column at step 3 in the execution_table.
At which step does the LED turn OFF?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look for when 'Pin 18 State' changes to LOW in the execution_table.
If we remove GPIO.cleanup(), what would likely happen?
AGPIO pins remain set, possibly causing issues later
BProgram will not run
CLED stays ON forever
DLED blinks automatically
💡 Hint
Refer to key_moments about why cleanup is important.
Concept Snapshot
Single LED control on Raspberry Pi:
- Use GPIO.setmode(GPIO.BCM) to select pin numbering
- Setup pin as output with GPIO.setup(pin, GPIO.OUT)
- Turn LED ON with GPIO.output(pin, GPIO.HIGH)
- Use time.sleep() to keep LED ON
- Turn LED OFF with GPIO.output(pin, GPIO.LOW)
- Always call GPIO.cleanup() at the end
Full Transcript
This program controls a single LED connected to Raspberry Pi pin 18. First, it sets the GPIO mode to BCM to use Broadcom pin numbers. Then it sets pin 18 as an output pin. The program turns the LED ON by setting pin 18 HIGH, waits for one second so the LED stays lit, then turns the LED OFF by setting pin 18 LOW. Finally, it calls GPIO.cleanup() to reset the pins to their default state. This prevents pins from staying in output mode and avoids unexpected behavior. The program then ends with the LED off.

Practice

(1/5)
1. What is the main purpose of setting a GPIO pin as output when controlling a single LED on a Raspberry Pi?
easy
A. To send signals that turn the LED on or off
B. To read the LED's brightness level
C. To connect the LED to the internet
D. To power the Raspberry Pi itself

Solution

  1. Step 1: Understand GPIO pin modes

    GPIO pins can be set as input or output. Output pins send signals to devices like LEDs.
  2. Step 2: Purpose of output mode for LED

    Setting a pin as output allows the Raspberry Pi to send HIGH or LOW signals to turn the LED on or off.
  3. Final Answer:

    To send signals that turn the LED on or off -> Option A
  4. Quick Check:

    Output mode controls LED state [OK]
Hint: Output pins send signals to control devices like LEDs [OK]
Common Mistakes:
  • Confusing input with output mode
  • Thinking GPIO controls power supply
  • Assuming GPIO reads LED brightness
2. Which of the following is the correct way to import the GPIO library and set pin 18 as output in Python for Raspberry Pi?
easy
A. import RPi.GPIO as GPIO\nGPIO.setup(18, GPIO.OUT)
B. import RPi.GPIO as GPIO\nGPIO.setup(18, GPIO.IN)
C. import gpiozero\nGPIO.setup(18, GPIO.OUT)
D. import RPi.GPIO as GPIO\nGPIO.output(18, GPIO.OUT)

Solution

  1. Step 1: Import the correct library

    Use import RPi.GPIO as GPIO to access GPIO functions.
  2. Step 2: Set pin 18 as output

    Use GPIO.setup(18, GPIO.OUT) to configure pin 18 for output.
  3. Final Answer:

    import RPi.GPIO as GPIO\nGPIO.setup(18, GPIO.OUT) -> Option A
  4. Quick Check:

    GPIO setup output = correct syntax [OK]
Hint: Use GPIO.setup(pin, GPIO.OUT) to set output pin [OK]
Common Mistakes:
  • Using GPIO.IN instead of GPIO.OUT
  • Calling GPIO.output instead of GPIO.setup for pin mode
  • Importing wrong library like gpiozero
3. What will be the output on the LED when running this Python code on Raspberry Pi?
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, GPIO.HIGH)
time.sleep(2)
GPIO.output(17, GPIO.LOW)
GPIO.cleanup()
medium
A. LED stays off for 2 seconds, then turns on
B. LED blinks rapidly for 2 seconds
C. LED turns on for 2 seconds, then turns off
D. Code causes an error and LED does not light

Solution

  1. Step 1: Analyze GPIO output commands

    The code sets pin 17 HIGH, turning LED on, waits 2 seconds, then sets pin 17 LOW, turning LED off.
  2. Step 2: Understand timing and cleanup

    The LED stays on during time.sleep(2), then turns off before cleanup.
  3. Final Answer:

    LED turns on for 2 seconds, then turns off -> Option C
  4. Quick Check:

    GPIO.HIGH = LED on, GPIO.LOW = LED off [OK]
Hint: HIGH turns LED on, LOW turns it off [OK]
Common Mistakes:
  • Confusing HIGH and LOW signals
  • Ignoring the sleep delay effect
  • Forgetting GPIO.cleanup() importance
4. Identify the error in this Raspberry Pi LED control code:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, HIGH)
GPIO.cleanup()
medium
A. GPIO.cleanup() should be called before output
B. Missing import for time module
C. Pin 12 cannot be used as output
D. Incorrect use of GPIO.HIGH without prefix

Solution

  1. Step 1: Check GPIO output command

    The code uses HIGH without GPIO. prefix, causing a NameError.
  2. Step 2: Correct usage of HIGH signal

    It should be GPIO.output(12, GPIO.HIGH) to properly reference the constant.
  3. Final Answer:

    Incorrect use of GPIO.HIGH without prefix -> Option D
  4. Quick Check:

    Use GPIO.HIGH, not just HIGH [OK]
Hint: Always prefix HIGH with GPIO. [OK]
Common Mistakes:
  • Using HIGH instead of GPIO.HIGH
  • Calling cleanup before output
  • Confusing BOARD and BCM modes
5. You want to write a Python program on Raspberry Pi that turns an LED on for 1 second, off for 1 second, and repeats this 5 times. Which code snippet correctly implements this behavior?
hard
A. import RPi.GPIO as GPIO\nGPIO.setmode(GPIO.BCM)\nGPIO.setup(21, GPIO.OUT)\nGPIO.output(21, GPIO.HIGH)\ntime.sleep(5)\nGPIO.output(21, GPIO.LOW)\nGPIO.cleanup()
B. import RPi.GPIO as GPIO\nimport time\nGPIO.setmode(GPIO.BCM)\nGPIO.setup(21, GPIO.OUT)\nfor i in range(5):\n GPIO.output(21, GPIO.HIGH)\n time.sleep(1)\n GPIO.output(21, GPIO.LOW)\n time.sleep(1)\nGPIO.cleanup()
C. import RPi.GPIO as GPIO\nimport time\nGPIO.setmode(GPIO.BCM)\nGPIO.setup(21, GPIO.OUT)\nfor i in range(5):\n GPIO.output(21, GPIO.LOW)\n time.sleep(1)\n GPIO.output(21, GPIO.HIGH)\n time.sleep(1)\nGPIO.cleanup()
D. import RPi.GPIO as GPIO\nimport time\nGPIO.setmode(GPIO.BCM)\nGPIO.setup(21, GPIO.OUT)\nGPIO.output(21, GPIO.HIGH)\ntime.sleep(1)\nGPIO.output(21, GPIO.LOW)\ntime.sleep(1)

Solution

  1. Step 1: Understand the loop for repetition

    The code uses for i in range(5): to repeat the on/off cycle 5 times.
  2. Step 2: Correct on/off timing inside loop

    Inside the loop, the LED is turned on (GPIO.HIGH) for 1 second, then off (GPIO.LOW) for 1 second.
  3. Final Answer:

    Code snippet C correctly repeats LED on/off 5 times with 1 second intervals -> Option B
  4. Quick Check:

    Loop with GPIO.HIGH then GPIO.LOW and sleep [OK]
Hint: Use a loop with GPIO.HIGH, sleep, GPIO.LOW, sleep [OK]
Common Mistakes:
  • Swapping HIGH and LOW timing order
  • Missing import of time module
  • Not using a loop for repetition
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard