This is a complete guide on simple LED Blinking Project on Raspberry Pi using ChatGPT.
Links:
- Headless Raspberry Pi Setup Guide: https://youtu.be/GQQpMArxhx4
- Download Filezilla: https://filezilla-project.org/download.php?type=client
- Download Notepad++: https://notepad-plus-plus.org/downloads/
- Chat GPT: https://chat.openai.com/
Source Code used in the video:
import RPi.GPIO as GPIO
import time
# Set GPIO mode to BCM (Broadcom SOC channel) numbering
GPIO.setmode(GPIO.BCM)
# Define the GPIO pins for the LEDs
led_pins = [12, 13, 14]
# Set up GPIO pins as outputs
for pin in led_pins:
GPIO.setup(pin, GPIO.OUT)
try:
while True:
# Turn on each LED one by one
for pin in led_pins:
GPIO.output(pin, GPIO.HIGH)
time.sleep(0.5) # Wait for half a second
GPIO.output(pin, GPIO.LOW)
time.sleep(0.5) # Wait for another half a second
except KeyboardInterrupt:
# Clean up GPIO settings when the script is interrupted
GPIO.cleanup()
Explanation on the Source Code
Line1 & Line2:
These lines import the RPi.GPIO and time module. RPi.GPIO functions as an interface module for the script to read the input pins and write the output pins on Raspberry Pi.
Time module provides the function of delay for the script. We will need delay function to “pause” after we turn ON the LEDs, or else the blinking will be too fast and not visible to human eye.
Line5:
Line5 basically tell the system to use BCM numbering mode for the GPIO module. This is one of the requirement in using RPi.GPIO module.
For a complete beginner in coding, line4 is only a comment for the code. In fact, any line that begin with the ‘#’ symbol automatically ignored by the system, and treated as comment. This helps the programmer for addition some explanation to the code.
Line8:
Line8 defines the GPIO pins that we will use for LEDs. In this example, we are using pin 12, 13 and 14. Take not that it defines these 3 pins as an array. Arrays make it simpler later when we are using For loop to defines the pins or to control the pins output.
Line11 & Line12:
Line11 and line12 is the For loop that loop through the pins to configure these pins to be OUTPUT pins. Take note that on most of the Digital IO, they can be configured as either digital input or digital output.
Line14 to Line25:
Line14 to line25 is the Try … Except loop. The Try and Except functions as checking for Keyboard Interrupt that allow our script to be stopped by the Control C keyboard interrupt. After a Control C is triggered, the execution will jump to line 25, where it will cleanup the GPIO before ending the script.
Line15 to Line21:
Line15 to line21 is basically the while loop that will run continuously. This While pair with a True means that this while loop will run continuously until Control C is triggered.
Line17 to Line21:
Under the While loop within line15 to line21, there is this For loop. What this for loop does is that it will loop through all the pin under led_pins array defined in line8. In the other words, the For loop will run for 3 times, the first time the pin value is 12, the second time, the pin value is 13 and the third time, the pin value is 14.
Line18: set the output of the pin to GPIO.HIGH, which turn ON the LED.
Line19: perform a delay of 0.5s.
Line20: set the output of the pin to GPIO.LOW, which turn OFF the LED.
Line21: perform a delay of 0.5s
After executing line18 to line21 for the led on pin 12, it will be repeated for same operation but for pin 13, and then pin 14.