Chapter 10 Photoresistor & LED
126
29
30
31
32
delay
(
100
);
}
return
0
;
}
Python Code 10.1.1 Nightlamp
First observe the project result, and then analyze the code.
1.
Use cd command to enter 09.1.1_Nightlamp directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/10.1.1_Nightlamp
2.
Use the python command to execute the Python code “Nightlamp.py”.
python Nightlamp.py
After the program is executed, when you cover the photosensitive resistance or make a flashlight toward the
photoresistor, the brightness of LED will be enhanced or weakened. And the terminal window will print out
the current input voltage value of PCF8591 AIN0 pin and the converted digital quantity.
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
smbus
i m port
time
address
=
0x48
bus
=
smbus
.
SMBus
(
1
)
cmd
=
0x40
ledPin
=
11
d e f
analogRead
(
chn
):
value
=
bus
.
read_byte_data
(
address
,
cmd
+
chn
)
r e turn
value
d e f
analogWrite
(
value
):
bus
.
write_byte_data
(
address
,
cmd
,
value
)
d e f
setup
( ):
g l obal
p
GPIO
.
setmode
(
GPIO
.
BOARD
)
GPIO
.
setup
(
ledPin
,
GPIO
.
OUT
)
GPIO
.
output
(
ledPin
,
GPIO
.
LOW
)
p
=
GPIO
.
PWM
(
ledPin
,
1000
)
p
.
start
(
0
)
d e f
loop
( ):
w h ile
T r ue
:
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 ...