0
0
Raspberry Piprogramming~15 mins

Single LED control in Raspberry Pi - Mini Project: Build & Apply

Choose your learning style9 modes available
Single LED control
📖 Scenario: You have a Raspberry Pi and a single LED connected to GPIO pin 17. You want to control this LED by turning it on and off using a simple Python program.
🎯 Goal: Build a Python program that sets up the GPIO pin, turns the LED on, waits for 2 seconds, then turns the LED off.
📋 What You'll Learn
Use the RPi.GPIO library to control the GPIO pins
Set GPIO mode to BCM
Set up GPIO pin 17 as an output
Turn the LED on by setting pin 17 to HIGH
Wait for 2 seconds
Turn the LED off by setting pin 17 to LOW
Clean up the GPIO settings at the end
💡 Why This Matters
🌍 Real World
Controlling LEDs is a basic skill for Raspberry Pi projects like indicators, alerts, or simple light displays.
💼 Career
Understanding GPIO control is essential for hardware interfacing roles, embedded systems, and IoT development.
Progress0 / 4 steps
1
Setup GPIO and LED pin
Import the RPi.GPIO library as GPIO and the time module. Set the GPIO mode to GPIO.BCM. Then create a variable called led_pin and set it to 17. Finally, set up led_pin as an output pin using GPIO.setup().
Raspberry Pi
Need a hint?

Remember to import both RPi.GPIO and time. Use GPIO.setmode(GPIO.BCM) to set the pin numbering mode. Then set up pin 17 as output.

2
Turn the LED on
Use GPIO.output() to set led_pin to GPIO.HIGH to turn the LED on.
Raspberry Pi
Need a hint?

Use GPIO.output(led_pin, GPIO.HIGH) to turn the LED on.

3
Wait for 2 seconds
Use time.sleep() to pause the program for 2 seconds.
Raspberry Pi
Need a hint?

Use time.sleep(2) to wait for 2 seconds.

4
Turn the LED off and clean up
Use GPIO.output() to set led_pin to GPIO.LOW to turn the LED off. Then call GPIO.cleanup() to reset the GPIO pins. Finally, print "LED turned off" to show the program finished.
Raspberry Pi
Need a hint?

Turn off the LED with GPIO.output(led_pin, GPIO.LOW), then clean up with GPIO.cleanup(). Use print("LED turned off") to show the result.