安装LCD库

使用AdaFruit库来控制lcd库,这个库支持AdaFruit屏幕和使用HD44780的显示屏。

通过源码安装:

git clone https://github.com/adafruit/Adafruit_Python_CharLCD.git

cd ./Adafruit_Python_CharLCD
sudo python setup.py install

将树莓派和LCD1602连接

连接的图如下所示: 连线图

LCD电子钟程序

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import RPi.GPIO as gpio
#to add the LCD library
import Adafruit_CharLCD as LCD
import time
gpio.setmode(gpio.BCM)

#声明 LCD pins(对应BCM引脚)
lcd_rs = 17
lcd_en = 18
lcd_d4 = 27
lcd_d5 = 22
lcd_d6 = 23
lcd_d7 = 10
 
lcd_backlight = 2
  
lcd_columns = 16 #Lcd column
lcd_rows = 2 #number of LCD rows
   
lcd = LCD.Adafruit_CharLCD(lcd_rs, lcd_en, \
        lcd_d4, lcd_d5, lcd_d6, lcd_d7, lcd_columns, lcd_rows,\
        lcd_backlight)
lcd.set_cursor(0,0)
lcd.message('     CLOCK')
while True:
    lcd.set_cursor(0,1)
    localtime = time.asctime( time.localtime(time.time()) )[4:-5]
    print(localtime)
    lcd.message(localtime)
    time.sleep(1)

效果图

显示的效果图如下,可以显示日期和时间,每秒钟刷新屏幕一次: 效果图