57
Chapter 1 LED
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
i m port
RPi
.
GPIO
a s
GPIO
i m port
time
ledPin
=
11
# RPI Board pin11
d e f
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 r int
(
'using pin%d'
%
ledPin
)
d e f
loop
( ):
w h ile
T r ue
:
GPIO
.
output
(
ledPin
,
GPIO
.
HIGH
)
# led on
p r int
(
'...led on'
)
time
.
sleep
(
1
)
# delay 1 second
GPIO
.
output
(
ledPin
,
GPIO
.
LOW
)
# led off
p r int
(
'led off...'
)
time
.
sleep
(
1
)
d e f
destroy
():
GPIO
.
output
(
ledPin
,
GPIO
.
LOW
)
# led off
GPIO
.
cleanup
()
# Release resource
i f
__name__
= =
'__main__'
:
# Program start from here
setup
()
t r y
:
loop
( )
e x cept
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
Summary of Contents for Ultimate Starter Kit
Page 1: ...Free your innovation Freenove is an open source electronics platform www freenove com ...
Page 117: ...117 Chapter 9 Potentiometer RGBLED www freenove com support freenove com Hardware connection ...
Page 155: ...155 Chapter 14 Relay Motor www freenove com support freenove com Hardware connection OFF 3 3V ...
Page 173: ...173 Chapter 16 Stepping Motor www freenove com support freenove com Hardware connection ...
Page 239: ...239 Chapter 22 Matrix Keypad www freenove com support freenove com Circuit Schematic diagram ...
Page 240: ...Chapter 22 Matrix Keypad 240 www freenove com support freenove com Hardware connection ...