Chapter 5 RGBLED
86
The related function of Software PWM can be described as follws:
int softPwmCreate (int pin, int initialValue, int pwmRange) ;
This creates a software controlled PWM pin.
void softPwmWrite (int pin, int value) ;
This updates the PWM value on the given pin.
long random();
This function will return a random number.
For more details about Software PWM, please refer to:
http://wiringpi.com/reference/software-pwm-library/
Python Code 5.1.1 ColorfulLED
First observe the project result, and then analyze the code.
1.
Use cd command to enter 05.1.1_ColorfulLED directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/05.1.1_ColorfulLED
2.
Use python command to execute python code “ColorfulLED.py”.
python ColorfulLED.py
After the program is executed, you will see that the RGBLED shows light of different color randomly.
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
i m port
RPi
.
GPIO
a s
GPIO
i m port
time
i m port
random
pins
=
{
'pin_R'
:
11
,
'pin_G'
:
12
,
'pin_B'
:
13
}
# pins is a dict
d e f
setup
( ):
g l obal
p_R
,
p_G
,
p_B
p r int
(
'Program is starting ... '
)
GPIO
.
setmode
(
GPIO
.
BOARD
)
# Numbers GPIOs by physical location
f o r
i
i n
pins
:
GPIO
.
setup
(
pins
[
i
],
GPIO
.
OUT
)
# Set pins' mode is output
GPIO
.
output
(
pins
[
i
],
GPIO
.
HIGH
)
# Set pins to high(+3.3V) to off led
p_R
=
GPIO
.
PWM
(
pins
[
'pin_R'
],
2000
)
# set Frequece to 2KHz
p_G
=
GPIO
.
PWM
(
pins
[
'pin_G'
],
2000
)
p_B
=
GPIO
.
PWM
(
pins
[
'pin_B'
],
2000
)
p_R
.
start
(
0
)
# Initial duty Cycle = 0
p_G
.
start
(
0
)
p_B
.
start
(
0
)
d e f
setColor
(
r_val
,
g_val
,
b_val
):
p_R
.
ChangeDutyCycle
(
r_val
)
# Change duty cycle
p_G
.
ChangeDutyCycle
(
g_val
)
p_B
.
ChangeDutyCycle
(
b_val
)
d e f
loop
( ):
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 ...