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 #158 background imageLoading...
Page #158 background image
Chapter 14 Relay & Motor
158
www.freenove.com
support@freenove.com
Python Code 14.1.1 Relay
First observe the project result, and then analyze the code.
1. Use cd command to enter 14.1.1_Relay directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/14.1.1_Relay
2. Use python command to execute code "Relay.py".
python Relay.py
After the program is executed, press the button, then the relay is opened, the Motor starts to rotate and LED
is turned on. If you press the button again, the relay is closed, the Motor stops running, and the LED is turned
off.
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
33
34
im port RPi. GPIO as GPIO
im port time
relayPin = 11 # define the relayPin
buttonPin = 12 # define the buttonPin
debounceTime = 50
def setup():
p rint ('Program is starting...')
GPIO. setmode(GPIO.BOARD) # Numbers GPIOs by physical location
GPIO. setup(relayPin, GPIO. OUT) # Set relayPin's mode is output
GPIO. setup(buttonPin, GPIO. IN)
def loop():
relayState = False
lastChangeTime = round( time.time()*1000)
buttonState = GPIO.HIGH
lastButtonState = GPIO. HIGH
reading = GPIO. HIGH
w hile Tr ue:
reading = GPIO.input(buttonPin)
if reading != lastButtonState :
lastChangeTime = round( time.time()*1000)
if ((round(time.time()*1000) - lastChangeTime) > debounceTime):
if reading != buttonState :
buttonState = reading;
if buttonState == GPIO. LOW:
print("Button is pressed!")
relayState = not relayState
if relayState:
print("Turn on relay ...")
else :
print("Turn off relay ... ")
e l se :

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