227
Chapter 20 LCD1602
time
(&
rawtime
);// get system time
timeinfo
=
localtime
(&
rawtime
);// convert to local time
printf
(
"%s \n"
,
asctime
(
timeinfo
));
lcdPosition
(
lcdhd
,
0
,
1
);// set the LCD cursor position to (0,1)
lcdPrintf
(
lcdhd
,
"Time:%d:%d:%d"
,
timeinfo
->
tm_hour
,
timeinfo
->
tm_min
,
timeinfo
->
tm_sec
);
//Display system time on LCD
}
Python Code 20.1.1 I2CLCD1602
First observe the project result, and then analyze the code.
1.
Use cd command to enter 20.1.1_ I2CLCD1602 directory of Python code.
cd ~/Freenove_Ultimate_Starter_Kit_for_Raspberry_Pi/Code/Python_Code/20.1.1_I2CLCD1602
2.
Use python command to execute python code “I2CLCD1602.py”.
python I2CLCD1602.py
After the program is executed, LCD1602 screen will display current CPU temperature and system time.
If there is no display or the display is not clear, rotate white knob in back of LCD to adjust the contrast
of LCD1602 until the screen can display clearly.
The following is the program code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
f r om
PCF8574
i m port
PCF8574_GPIO
f r om
Adafruit_LCD1602
i m port
Adafruit_CharLCD
f r om
time
i m port
sleep
,
strftime
f r om
datetime
i m port
datetime
d e f
get_cpu_temp
():
# get CPU temperature and store it into file
"/sys/class/thermal/thermal_zone0/temp"
tmp
=
open
(
'/sys/class/thermal/thermal_zone0/temp'
)
cpu
=
tmp
.
read
()
tmp
.
close
()
r e turn
'{:.2f}'
.
format
(
float
(
cpu
)/
1000
)
+
' C'
d e f
get_time_now
():
# get system time
r e turn
datetime
.
now
().
strftime
(
' %H:%M:%S'
)
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 ...