Chapter 9 Potentiometer & RGBLED
120
Python Code 9.1.1 ColorfulSoftlight
First observe the project result, and then analyze the code.
1.
Use cd command to enter 09.1.1_ColorfulSoftlight directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/09.1.1_ ColorfulSoftlight
2.
Use python command to execute python code "ColorfulSoftlight.py".
python ColorfulSoftlight.py
After the program is executed, rotate one of potentiometers, then the color of RGBLED will change
consequently. And the terminal window will print out the ADC value of each potentiometer.
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
35
i m port
RPi
.
GPIO
a s
GPIO
i m port
smbus
i m port
time
address
=
0x48
bus
=
smbus
.
SMBus
(
1
)
cmd
=
0x40
ledRedPin
=
15
#define 3 pins of RGBLED
ledGreenPin
=
13
ledBluePin
=
11
d e f
analogRead
(
chn
):
#read ADC value
bus
.
write_byte
(
address
,
cmd
+
chn
)
value
=
bus
.
read_byte
(
address
)
value
=
bus
.
read_byte
(
address
)
#This function always return the last data read.
#If you want return the current data, you need execute it twice.
r e turn
value
d e f
analogWrite
(
value
):
bus
.
write_byte_data
(
address
,
cmd
,
value
)
d e f
setup
( ):
g l obal
p_Red
,
p_Green
,
p_Blue
GPIO
.
setmode
(
GPIO
.
BOARD
)
GPIO
.
setup
(
ledRedPin
,
GPIO
.
OUT
)
#set 3 pins of RGBLED to output mode
GPIO
.
setup
(
ledGreenPin
,
GPIO
.
OUT
)
GPIO
.
setup
(
ledBluePin
,
GPIO
.
OUT
)
p_Red
=
GPIO
.
PWM
(
ledRedPin
,
1000
)
#configure PWM to 3 pins of RGBLED
p_Red
.
start
(
0
)
p_Green
=
GPIO
.
PWM
(
ledGreenPin
,
1000
)
p_Green
.
start
(
0
)
p_Blue
=
GPIO
.
PWM
(
ledBluePin
,
1000
)
p_Blue
.
start
(
0
)
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 ...