According to the circuit connection, port 3 of PCF8574 is connected to positive pole of LCD1602 backlight.
Then in the loop () function, use of mcp.output(3,1) to turn on LCD1602 backlight, and set number of LCD
lines and columns.
def loop():
mcp. output(3,1) # turn on the LCD backlight
lcd. begin(16,2) # set number of LCD lines and columns
In the next while cycle, set the cursor position, and display the CPU temperature and time.
w hile(True):
#lcd.clear()
lcd.setCursor(0,0) # set cursor position
lcd.message( 'CPU: ' + get_cpu_temp()+'\n' )# display CPU temperature
lcd.message( get_time_now() ) # display the time
sleep(1)
CPU temperature is stored in file “/sys/class/thermal/thermal_zone0/temp”. Open the file and
read content of the file, and then convert it to Celsius degrees and return. Subfunction used to
get CPU temperature is shown below:
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'
Subfunction used to get time:
def get_time_now(): # get the time
r eturn datetime.now().strftime(' %H:%M:%S')
Details about PCF8574.py and Adafruit_LCD1602.py:
This module provides two classes PCF8574_I2C and PCF8574_GPIO.
Class PCF8574_I2C:provides reading and writing method for PCF8574.
Class PCF8574_GPIO:provides a standardized set of GPIO functions.
More information can be viewed through opening PCF8574.py.
Adafruit_LCD1602 Module
This module provides the basic operation method of LCD1602, including class Adafruit_CharLCD. Some
member functions are described as follows:
def begin(self, cols, lines): set the number of lines and columns of the screen.
def clear(self): clear the screen
def setCursor(self, col, row): set the cursor position
def message(self, text): display contents
More information can be viewed through opening Adafruit_CharLCD.py.