Chapter 4 Analog & PWM
80
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.
v o id p wmWrite ( int p in, 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
i m port
RPi
.
GPIO
a s
GPIO
i m port
time
LedPin
=
12
d e f
setup
( ):
g l obal
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
d e f
loop
( ):
w h ile
T r ue
:
f o r
dc
i n
range
(
0
,
101
,
1
) :
# Increase duty cycle: 0~100
p
.
ChangeDutyCycle
(
dc
)
# Change duty cycle
time
.
sleep
(
0.01
)
time
.
sleep
(
1
)
f o r
dc
i n
range
(
100
,
-
1
,
-
1
):
# Decrease duty cycle: 100~0
p
.
ChangeDutyCycle
(
dc
)
time
.
sleep
(
0.01
)
time
.
sleep
(
1
)
d e f
destroy
():
p
.
stop
()
GPIO
.
output
(
LedPin
,
GPIO
.
LOW
)
# turn off led
GPIO
.
cleanup
()
i f
__name__
= =
'__main__'
:
# Program start from here
setup
()
t r y
:
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 ...