0
0
Raspberry Piprogramming~30 mins

Python on Raspberry Pi - Mini Project: Build & Apply

Choose your learning style9 modes available
Python on Raspberry Pi: Controlling an LED
📖 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
Need a 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
Need a 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
Need a 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
Need a hint?

Use time.sleep(delay_time) to wait, then turn off the LED and clean up.