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
fr om PCF8574 i m port PCF8574_GPIO
fr om Adafruit_LCD1602 import Adafruit_CharLCD
fr om time i m port sleep, strftime
fr om datetime i m port datetime
def 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 eturn '{:.2f}'.format( float(cpu)/1000 ) + ' C'
def get_time_now(): # get system time
r eturn datetime.now().strftime(' %H:%M:%S')