LED Blinking with Python
This presentation demonstrates how to develop a simple Python program to control an LED connected to GPIO pin
21. The LED will blink by staying ON for 500 milliseconds and OFF for 1500 milliseconds in a continuous loop,
providing a basic introduction to GPIO programming with Python.
Presented by:
Pandula sravanthi
23B81A6254
CSC-A
Setting Up Your Raspberry Pi with LED
1 2 3
Gather Hardware Install Software Make Connections
Collect Raspberry Pi, LED, Make sure to have Python 3 and Connect LED to GPIO 21 with a
resistor, and jumper wires. [Link] library installed. resistor; ensure proper GND
connection.
Writing the Code
import [Link] as GPIO
import time
# Use BCM numbering
[Link]([Link])
# Set GPIO 21 as output pin
led_pin = 21
[Link](led_pin, [Link])
print("LED blinking... Press Ctrl+C to stop")
try:
while True:
[Link](led_pin, [Link]) # Turn ON LED
[Link](0.5) # Keep ON for 500 ms
[Link](led_pin, [Link]) # Turn OFF LED
[Link](1.5) # Keep OFF for 1500 ms
except KeyboardInterrupt:
print("\nProgram stopped by user")
finally:
[Link]() # Reset all GPIOs
Complete Code Example for Blinking LED
This Python code effectively blinks an LED connected to GPIO 21. The try-except block is utilized to manage exceptions while ensuring the GPIO settings
are cleaned up upon exit.
Import Libraries Setup GPIO
Using [Link] and time for GPIO handling Configure GPIO mode and pin output
LED Blinking Logic Exception Handling
Code to blink LED connected to GPIO 21 Ensure proper cleanup on exit
Testing the Circuit
1 2 3
Check Connections Run the Script Troubleshooting
Double-check all connections Execute the script using python3 Ensure GPIO 21 is configured
including LED orientation. led_blink.py. correctly and the circuit is
properly connected.
Project Conclusion
Further Learning
2
This project can be a stepping
stone to more complex
applications like controlling
multiple LEDs or integrating
Applications sensors.
Blinking LEDs are often
used in various projects, 1
including indicators,
alerts, and sequential Extensions
logic circuits.
Explore integrating user input
to control the blink rates or
patterns for a more dynamic
project. Experiment with
3 different GPIO pins and LED
configurations.