Python Code 1.1.1 Blink
Net, we will use Python language to make LED blink.
First, observe the project result, and then analyze the code.
1. Use cd command to enter 01.1.1_Blink directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/01.1.1_Blink
2. Use python command to execute python code blink.py.
python Blink.py
Now, LED start blinking.
You can press “Ctrl+C” to end the program. The following is the program code:
im port time
ledPin = 11 # RPI Board pin11
def setup():
GPIO. setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO. setup(ledPin, GPIO. OUT) # Set ledPin's mode is output
GPIO. output(ledPin, GPIO. LOW) # Set ledPin low to off led
p rint ('using pin%d'%ledPin)
def loop():
w hile Tr ue:
GPIO.output(ledPin, GPIO. HIGH) # led on
print ('...led on')
time.sleep(1) # delay 1 second
GPIO.output(ledPin, GPIO. LOW) # led off
print ('led off...')
time.sleep(1)
def destroy():
GPIO. output(ledPin, GPIO. LOW) # led off
GPIO. cleanup() # Release resource
if __name__ == '__main__': # Program start from here
setup()
try:
loop()
e xcept KeyboardInterrupt: # When 'Ctrl+C' is pressed, the subprogram destroy() will
be executed.
destroy()
In subfunction setup(), GPIO.setmode (GPIO.BOARD) is used to set the serial number for GPIO based on
physical location of the pin. GPIO17 use pin 11 of the board, so define ledPin as 11 and set ledPin to output