EasyManuals Logo

Freenove Ultimate Starter Kit User Manual

Freenove Ultimate Starter Kit
286 pages
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Page #63 background imageLoading...
Page #63 background image
63
Chapter 2 Button & LED
www.freenove.com
support@freenove.com
Python Code 2.1.1 ButtonLED
First, observe the project result, then analyze the code.
1. Use cd command to enter 01.1.1_btnLED directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/02.1.1_ButtonLED
2. Use Python command to execute btnLED.py.
python ButtonLED.py
Later, the terminal window continue to print out the characters “led off…”, press the button, then LED is turned
on and then terminal window print out the "led on…". Release the button, then LED is turned off and then
terminal window print out the "led off…". You can press "Ctrl+C" to terminate 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
32
im port RPi. GPIO as GPIO
ledPin = 11 # define the ledPin
buttonPin = 12 # define the buttonPin
def setup():
p rint ('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)
def loop():
w hile Tr ue:
if GPIO.input(buttonPin)==GPIO.LOW:
GPIO.output(ledPin,GPIO.HIGH)
print ('led on ...')
else :
GPIO.output(ledPin,GPIO.LOW)
print ('led off ...')
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()

Table of Contents

Questions and Answers:

Question and Answer IconNeed help?

Do you have a question about the Freenove Ultimate Starter Kit and is the answer not in the manual?

Freenove Ultimate Starter Kit Specifications

General IconGeneral
BrandFreenove
ModelUltimate Starter Kit
CategorySingle board computers
LanguageEnglish