Chapter 2 Button & LED
64
In subfunction setup (), GPIO.setmode (GPIO.BOARD) is used to set the serial number of the GPIO, which is
based on physical location of the pin. So, GPIO17 and GPIO18 correspond to pin11 and pin12 respectively in
the circuit. Then set ledPin to output mode, buttonPin to input mode with a pull resistor.
ledPin
=
11
# define the ledPin
buttonPin
=
12
# define the buttonPin
d e f
setup
( ):
p r int
(
'Program is starting...'
)
GPIO
.
setmode
(
GPIO
.
BOARD
)
# Numbers GPIOs by physical location
GPIO
.
setup
(
ledPin
,
GPIO
.
OUT
)
# Set ledPin's mode is output
GPIO
.
setup
(
buttonPin
,
GPIO
.
IN
,
pull_up_down
=
GPIO
.
PUD_UP
)
# Set buttonPin's mode is
input, and pull up to high level(3.3V)
In the loop function while dead circulation, continue to judge whether the key is pressed. When the button is
pressed, the GPIO.input(buttonPin) will return low level, then the result of “if” is true, ledPin outputs high level,
LED is turned on. Or, LED will be turned off.
d e f
loop
( ):
w h ile
T r ue
:
i f
GPIO
.
input
(
buttonPin
)==
GPIO
.
LOW
:
GPIO
.
output
(
ledPin
,
GPIO
.
HIGH
)
p r int
(
'led on ...'
)
e l se
:
GPIO
.
output
(
ledPin
,
GPIO
.
LOW
)
p r int
(
'led off ...'
)
Execute the function destroy (), close the program and release the resource.
About function GPIO.input ():
G P IO.input()
This function returns the value read at the given pin. It will be “
HIGH
” or “
LOW
”(1 or 0) depending on the
logic level at the pin.
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 ...