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 #80 background imageLoading...
Page #80 background image
Chapter 4 Analog & PWM
80
www.freenove.com
support@freenove.com
delay(300);
}
You can also adjust the rate of the state change of LED by changing the parameters of the delay() function in
the for cycle.
vo id p wmWrite ( int pin, i nt v alue) ;
Writes the value to the PWM register for the given pin. The Raspberry Pi has one on-board PWM pin, pin
1 (BCM_GPIO 18, Phys 12) and the range is 0-1024. .
Python Code 4.1.1 BreathingLED
First observe the project result, and then analyze the code.
1. Use cd command to enter 04.1.1_BreathingLED directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/04.1.1_BreathingLED
2. Use python command to execute python code “BreathingLED.py”.
python BreathingLED.py
After the program is executed, you'll see that LED is turned from on to off and then from off to on gradually
like breathing.
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
im port RPi. GPIO as GPIO
im port time
LedPin = 12
def setup():
g lobal p
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 to low
p = GPIO. PWM(LedPin, 1000) # Set Frequency to 1KHz
p. start(0) # Duty Cycle = 0
def loop():
w hile Tr ue:
for dc in range( 0, 101, 1): # Increase duty cycle: 0~100
p.ChangeDutyCycle(dc) # Change duty cycle
time.sleep(0.01)
time.sleep(1)
for dc in range( 100, - 1, -1): # Decrease duty cycle: 100~0
p.ChangeDutyCycle(dc)
time.sleep(0.01)
time.sleep(1)
def destroy():
p. stop()
GPIO. output(LedPin, GPIO. LOW) # turn off led
GPIO. cleanup()
if __name__ == '__main__': # Program start from here
setup()
try:

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