📖 Scenario: You have a Raspberry Pi with an LED connected to GPIO pin 17. You want to write a Python program to turn the LED on and off.
🎯 Goal: Build a Python program that sets up the GPIO pin, turns the LED on, waits, then turns it off.
📋 What You'll Learn
Create a variable called led_pin and set it to 17
Create a variable called delay_time and set it to 2
Use GPIO.setup(led_pin, GPIO.OUT) to set the pin as output
Use GPIO.output(led_pin, GPIO.HIGH) to turn the LED on
Use GPIO.output(led_pin, GPIO.LOW) to turn the LED off
Print "LED is ON" when the LED turns on
Print "LED is OFF" when the LED turns off
💡 Why This Matters
🌍 Real World
Controlling hardware like LEDs, motors, and sensors with Python on a Raspberry Pi is common in home automation and robotics projects.
💼 Career
Understanding GPIO control with Python is useful for roles in embedded systems, IoT development, and hardware prototyping.
Progress0 / 4 steps
1
Set up the GPIO pin variable
Create a variable called led_pin and set it to 17.
Raspberry Pi
Hint
Use led_pin = 17 to store the GPIO pin number.
2
Set the delay time variable
Create a variable called delay_time and set it to 2.
Raspberry Pi
Hint
Use delay_time = 2 to set the wait time in seconds.
3
Set up GPIO and turn LED on
Import RPi.GPIO as GPIO, set the GPIO mode to GPIO.BCM, set up led_pin as output using GPIO.setup(led_pin, GPIO.OUT), then turn the LED on with GPIO.output(led_pin, GPIO.HIGH). Also, print "LED is ON".
Raspberry Pi
Hint
Remember to import the GPIO library and set the mode before setting up the pin.
4
Turn LED off after delay and clean up
Use time.sleep(delay_time) to wait, then turn the LED off with GPIO.output(led_pin, GPIO.LOW). Print "LED is OFF" and call GPIO.cleanup() to reset the pins. Finally, print the messages exactly as shown.
Raspberry Pi
Hint
Use time.sleep(delay_time) to wait, then turn off the LED and clean up.
Practice
(1/5)
1. What is the main purpose of using Python on a Raspberry Pi?
easy
A. To control hardware like LEDs and sensors easily
B. To create websites only
C. To run Windows applications
D. To edit videos
Solution
Step 1: Understand Raspberry Pi's use with Python
Python on Raspberry Pi is mainly used to control hardware components like LEDs and sensors.
Step 2: Compare options with this purpose
The options describing website creation only, running Windows applications, and video editing do not relate to hardware control, so they are incorrect.
Final Answer:
To control hardware like LEDs and sensors easily -> Option A
Quick Check:
Python on Raspberry Pi = hardware control [OK]
Hint: Think about what Raspberry Pi projects usually do [OK]
Common Mistakes:
Confusing Raspberry Pi with a general computer
Thinking Python on Pi is only for web development
Assuming it runs Windows apps natively
2. Which of the following is the correct way to import the GPIO library in Python on Raspberry Pi?
easy
A. import GPIO.RPi
B. import RPi.GPIO as GPIO
C. from GPIO import RPi
D. import raspberry.GPIO
Solution
Step 1: Recall the standard import syntax for GPIO
The correct import statement is import RPi.GPIO as GPIO to use GPIO functions easily.
Step 2: Check other options for syntax errors
The other options ('import GPIO.RPi', 'from GPIO import RPi', 'import raspberry.GPIO') do not follow Python import syntax or correct module names.
Final Answer:
import RPi.GPIO as GPIO -> Option B
Quick Check:
Standard GPIO import = import RPi.GPIO as GPIO [OK]
Hint: Remember the exact module name: RPi.GPIO [OK]
Common Mistakes:
Swapping module and package names incorrectly
Using wrong import syntax
Assuming 'GPIO' alone is a module
3. What will be the output of this Python code on Raspberry Pi?
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.OUT)
GPIO.output(18, GPIO.HIGH)
print(GPIO.input(18))
medium
A. 0
B. HIGH
C. 1
D. Error
Solution
Step 1: Understand GPIO setup and output
Pin 18 is set as output and then set to HIGH, which means voltage is ON (1).
Step 2: Check GPIO.input(18) after setting HIGH
Reading the pin returns 1 because it was set HIGH.
Final Answer:
1 -> Option C
Quick Check:
GPIO.output HIGH means input reads 1 [OK]
Hint: HIGH means 1, LOW means 0 in GPIO [OK]
Common Mistakes:
Confusing HIGH with string 'HIGH'
Expecting 0 instead of 1 after setting HIGH
Thinking GPIO.input returns string values
4. Identify the error in this Raspberry Pi Python code snippet:
import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BOARD)
GPIO.setup(12, GPIO.OUT)
GPIO.output(12, HIGH)
medium
A. Missing GPIO prefix before HIGH
B. Wrong pin number for BOARD mode
C. GPIO.setmode should be BCM
D. GPIO.setup should be input, not output
Solution
Step 1: Check usage of HIGH constant
HIGH must be referenced as GPIO.HIGH, not just HIGH alone.
Step 2: Verify other code parts
Pin 12 is valid in BOARD mode, setmode can be BOARD, and output pin can be output.
Final Answer:
Missing GPIO prefix before HIGH -> Option A
Quick Check:
Use GPIO.HIGH, not just HIGH [OK]
Hint: Always prefix constants with GPIO. [OK]
Common Mistakes:
Using HIGH without GPIO prefix
Confusing BOARD and BCM pin numbering
Setting output pin as input by mistake
5. You want to blink an LED connected to pin 17 on Raspberry Pi using Python. Which code snippet correctly blinks the LED once with a 1-second delay?
hard
A. import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)
GPIO.output(17, GPIO.HIGH)
time.sleep(1)
GPIO.output(17, GPIO.LOW)
B. import RPi.GPIO as GPIO
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, 1)
time.sleep(1)
GPIO.output(17, 0)
C. import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BOARD)
GPIO.setup(17, GPIO.OUT)
GPIO.output(17, GPIO.HIGH)
time.sleep(1)
GPIO.output(17, GPIO.LOW)
D. 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)
Solution
Step 1: Check pin numbering and setup
BCM mode, import time, GPIO.setup(17, GPIO.OUT) are all correct.
Step 2: Verify output and delay
GPIO.output(17, GPIO.HIGH), time.sleep(1), GPIO.output(17, GPIO.LOW) blinks once (on for 1s then off).
Step 3: Identify errors in other options
One lacks import time (NameError on sleep); another uses BOARD mode (pin 17 ≠ BCM pin 17 physically); another sets GPIO.IN (should be OUT).
Final Answer:
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) -> Option D
Quick Check:
Use BCM mode, output pin, and time.sleep for blinking [OK]
Hint: Use GPIO.HIGH/LOW with time.sleep for blinking [OK]
Common Mistakes:
Mixing BOARD and BCM pin numbers
Setting pin as input instead of output
Forgetting to import time module
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.