5003浏览
查看: 5003|回复: 10

【转载】用树莓派驱动一个16×2的LCD

[复制链接]
【转载】用树莓派驱动一个16×2的LCD图1

不管什么项目,如果加上一个液晶显示屏的话肯定都会看起来更棒。这篇文章将详解如何用树莓派的六个通用端口(GPIO)来连接一个廉价的HDD44780的小型LCD。当然也有用I2C或是UART来连接LCD的, 但是使用GPIO是最直接的方法。

这种方法的几个优势:

  • 使得廉价的LCD得以应用
  • 不需要I2C的驱动器
  • 不会占用树莓派仅有的USB口

以下是用Python代码控制显示的时间日期以及IP地址。如果你的树莓派运行在Headless模式下(Headless模式是系统的一种配置模式。在该模式下系统缺少了显示设备、键盘或鼠标),能有个小的显示屏显示IP地址可是很有吸引力的。

【转载】用树莓派驱动一个16×2的LCD图2

以下是完成本次教程的必要硬件
  • 一个标准的16×2的LCD
  • Adafruit Pi Cobbler (树莓派GPIO的扩展设备,这里是以Cobbler为例,当然也可以用树莓派的breakout)
  • 面包板
  • 连接线
  • 一个树莓派

本教程只适用于16×1, 16×2, 20×2, 20×4 的LCD

连接Cobbler到LCD上LCD

【转载】用树莓派驱动一个16×2的LCD图3

任何一个拥有16个引脚的LCD基本上都是用HD44780控制器来控制的。 这种类型的LCD的引脚都拥有相同的输入输出功能,所以比较容易使用。LCD采用的是并行接口,这就意味着树莓派需要提供多个引脚来控制它。本篇教程中我们会用到树莓派的4个数据引脚(4位模式)和两个控制引脚。

数据引脚可以直接传输数据到LCD上, 这里我们只让LCD处于写模式,不读取任何数据。

寄存器的选择引脚有两种用途。当设置为低位时,它可以发送指令到LCD(比如显示的位置或是清空屏幕),可理解为命令寄存器。 当设置为高位的时候,它使得LCD转为数据模式并且将数据传输到屏幕上。

读/写引脚在这里会被设置成低位(写模式),因为我们只是想让LCD作为一个输出设备。

LCD 各个引脚的定义:

  • Ground
  • VCC - 5v not 3.3v
  • Contrast adjustment (VO) from potentiometer
  • Register Select (RS). RS=0: Command, RS=1: Data
  • Read/Write (R/W). R/W=0: Write, R/W=1: Read (we won’t use this pin)
  • Clock (Enable). Falling edge triggered
  • Bit 0 (Not used in 4-bit operation)
  • Bit 1 (Not used in 4-bit operation)
  • Bit 2 (Not used in 4-bit operation)
  • Bit 3 (Not used in 4-bit operation)
  • Bit 4
  • Bit 5
  • Bit 6
  • Bit 7
  • Backlight LED Anode (+)
  • Backlight LED Cathode (-)
在连接这些引脚之前,先确认你的LCD的背光是否可以正常工作,背光应为LED的背光因为这只需要10-40mA的功率,但是若为EL的背光就需要200mA以上的功率了。EL背光的LCD往往会便宜些但是用起来比较难操作,确保你的LCD不是EL背光,否则会将整个树莓派的功率拖下来。还有一些LCD的LED背光没有自带的稳压电阻,所以在连接前要去确定好你的LCD是否需要加载额外的电阻来保证背光LED正常工作。线路图

首先将Cobber的电源引脚连接到面包板的供电轨上。+5V的用红线连接到红线轨上(译者认为这里连接3.3V的就够了), GND用黑线连接到蓝线轨上, 为了能使数据传到LCD上,我们将进行以下的连接。
  • LCD的Pin 1脚接地(黑线)
  • LCD的Pin 2脚接 +5V(红线)
  • LCD的Pin 3脚接到分压器的中间位置(橙线)
  • LCD的Pin 4脚接到Cobber的 #25位 (黄线)
  • LCD的Pin 5脚接地(黑线)
  • LCD的Pin 6脚接到Cobber的#24位
  • LCD的Pin 7,8,9,10什么都不接
  • LCD的Pin 11脚接 Cobber的 #23位(蓝线)
  • LCD的Pin 12脚接 Cobber的 #17位 (紫线)
  • LCD的Pin 13脚接 Cobber的 #21位 (灰线)(译者推荐这里连接#18位)
  • LCD的Pin 14脚接 Cobber的 #22位 (白线)
  • LCD的Pin 15脚接 +5V(红线)
  • LCD的Pin 16脚接地 (黑线)

分压器左边的引脚接地(黑线),右边的引脚接+5V(红线)。

【转载】用树莓派驱动一个16×2的LCD图4【转载】用树莓派驱动一个16×2的LCD图5

原理图5V LCD vs 3.3V Pi

树莓派配置的通用接口(GPIO)为3.3V,但是我们的LCD是需要5V配电的设备。如果我们仅仅是用LCD做树莓派的输出设备的话,连接5V的引脚当然没有问题。所以我们这里不使用Cobbler上3.3V的Pin口,并且我们将LCD上的RW(读写)脚接地,这样就避免了LCD向树莓派发送+5V的信号。

准备LCD



在你开始前,确认你有一组 0.1“规格的引脚和一个阻值为10K的分压器。
【转载】用树莓派驱动一个16×2的LCD图10
大部分LCD显示屏是需要16个引脚的,如果头部太长,可以适当剪短到合适的长度即可。
【转载】用树莓派驱动一个16×2的LCD图11
接着你需要将引脚和LCD焊接到一起。你必须这么做,不能只是扣上去就完事了。
【转载】用树莓派驱动一个16×2的LCD图12
首先将Cobbler上的+5V引脚跟GND引脚连接到面包板上。接着如图连接LCD的Pin1脚、Pin2脚、Pin15脚和Pin16脚连接到面包板的供电轨上。这个时候LCD的背光应该就亮了,如果没有亮请检查你的线路是否连接正常。
【转载】用树莓派驱动一个16×2的LCD图13
接着,将分压器中间的引脚按图中所示连接到LCD的Pin脚3上,其他两个引脚分别连接5V电源和地线。
【转载】用树莓派驱动一个16×2的LCD图14
【转载】用树莓派驱动一个16×2的LCD图15
扭动分压器直到LCD的第一行显示出方块来。如果看不到,检查一下线路是否连接正确。

按照电路图所示完成LCD最后RS(Pin 4脚),RW(Pin 5脚), EN(Pin 6脚), D4(Pin 11脚), D5(Pin 12脚), D6(Pin 13脚)和D7(Pin 14脚)的连接。

到这里,就可以用Python脚本来驱动LCD显示些东西了。

必要的Python包

本教程是基于Debian的Wheezy系统写成的。必须要安装以下组件才能使用树莓派的GPIO口。

安装python(2.x)的最新开发套件:

[mw_shl_code=applescript,true]$ sudo apt-get install update -y
$ sudo apt-get install upgrade -y[/mw_shl_code]

[mw_shl_code=applescript,true]$ sudo apt-get install python3-pip[/mw_shl_code]


[mw_shl_code=applescript,true]$ sudo pip3 install adafruit-blinka[/mw_shl_code]

[mw_shl_code=applescript,true]$ sudo pip3 install adafruit-circuitpython-charlcd[/mw_shl_code]

安装如下组件:

安装 RPi.GPIO 0.3.1a

  1. sudo pip install rpi.gpio
复制代码
Python脚本代码

可以在Github获得控制LCD的Python脚本。其中包括两个文件:

  • Adafruit_CharLCD.py —该文件中包含用来控制LCD的Python类
  • Adafruit_CharLCD_IPclock_example.py — 样例程序,用来显示IP地址、日期时间。

第一个文件Adafruit_CharLCD.py将两个LCD的控制代码混合在了一起。感谢Github上的用户lrvick,他用一个Python类将它们漂亮的封装在一起。

将代码加载到树莓派上的最简单的方法就是将树莓派连上网络,然后直接通过git的clone命令来下载。只要在合适的目录下(比如说/home/pi/)键入以下命令即可:

apt[color=rgb(0, 111, 224) !important]-[color=teal !important]get [color=teal !important]install [color=teal !important]git
[color=teal !important]git clone[color=rgb(0, 111, 224) !important] http[color=rgb(0, 111, 224) !important]:[color=rgb(153, 153, 153) !important]//github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
[color=teal !important]cd Adafruit[color=rgb(0, 111, 224) !important]-Raspberry[color=rgb(0, 111, 224) !important]-Pi[color=rgb(0, 111, 224) !important]-Python[color=rgb(0, 111, 224) !important]-[color=teal !important]Code
[color=teal !important]cd Adafruit_CharLCD

测试

现在你就可以测试之前连接好的线路了,只要简单运行Python代码Adafruit_CharLCD.py即可。因为这里的代码很少,它只会简单的显示出一段测试消息。

无论你使用的是什么型号的树莓派,译者在这里建议大家将引脚21替换换为引脚18, 所以这里要对 Adafruit_CharLCD.py做一个小小的改动,将

def[color=rgb(0, 111, 224) !important] [color=teal !important]__init__[color=rgb(51, 51, 51) !important](self[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 45, 122) !important]pin_rs[color=rgb(0, 111, 224) !important]=[color=rgb(0, 153, 153) !important]25[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 45, 122) !important]pin_e[color=rgb(0, 111, 224) !important]=[color=rgb(0, 153, 153) !important]24[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 45, 122) !important]pins_db[color=rgb(0, 111, 224) !important]=[color=rgb(51, 51, 51) !important][[color=rgb(0, 153, 153) !important]23[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 153, 153) !important]17[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 153, 153) !important]21[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 153, 153) !important]22[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 45, 122) !important]GPIO[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]=[color=rgb(0, 111, 224) !important] [color=rgb(128, 0, 128) !important]None[color=rgb(51, 51, 51) !important])[color=rgb(0, 111, 224) !important]:

修改为:

def[color=rgb(0, 111, 224) !important] [color=teal !important]__init__[color=rgb(51, 51, 51) !important](self[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 45, 122) !important]pin_rs[color=rgb(0, 111, 224) !important]=[color=rgb(0, 153, 153) !important]25[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 45, 122) !important]pin_e[color=rgb(0, 111, 224) !important]=[color=rgb(0, 153, 153) !important]24[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 45, 122) !important]pins_db[color=rgb(0, 111, 224) !important]=[color=rgb(51, 51, 51) !important][[color=rgb(0, 153, 153) !important]23[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 153, 153) !important]17[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 153, 153) !important]18[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 153, 153) !important]22[color=rgb(51, 51, 51) !important],[color=rgb(0, 111, 224) !important] [color=rgb(0, 45, 122) !important]GPIO[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]=[color=rgb(0, 111, 224) !important] [color=rgb(128, 0, 128) !important]None[color=rgb(51, 51, 51) !important])[color=rgb(0, 111, 224) !important]:

可以使用nano编辑器来修改代码。


下图为译者按照参考进行的试验,整体进行很顺利,提醒一下译者连接的时候就是用的树莓派的Pin #18口 而不是原文作者使用的#21或者#27。(顺便让译者的小黄人stuart也上一下镜,希望大家喜欢


IP和时钟的显示

这个脚本的功能是显示你的IP地址,若想显示无线接口的IP地址,请将代码中的eth0替换为wlan0或者wlan1即可。


  1. #!/usr/bin/python
  2. from Adafruit_CharLCD import Adafruit_CharLCD
  3. from subprocess import *
  4. from time import sleep, strftime
  5. from datetime import datetime
  6. lcd = Adafruit_CharLCD()
  7. cmd = "ip addr show eth0 | grep inet | awk '{print $2}' | cut -d/ -f1"
  8. lcd.begin(16,1)
  9. def run_cmd(cmd):
  10.     p = Popen(cmd, shell=True, stdout=PIPE)
  11.     output = p.communicate()[0
  12.     return output
  13. while 1:
  14.     lcd.clear()
  15.     ipaddr = run_cmd(cmd)
  16.     lcd.message(datetime.now().strftime('%b %d  %H:%M:%S\n'))
  17.     lcd.message('IP %s' % ( ipaddr ) )
  18.     sleep(2)
复制代码

[mw_shl_code=applescript,true]from subprocess import Popen, PIPE
from time import sleep
from datetime import datetime
import board
import digitalio
import adafruit_character_lcd.character_lcd as characterlcd

# Modify this if you have a different sized character LCD
lcd_columns = 16
lcd_rows = 2

# compatible with all versions of RPI as of Jan. 2019
# v1 - v3B+
lcd_rs = digitalio.DigitalInOut(board.D22)
lcd_en = digitalio.DigitalInOut(board.D17)
lcd_d4 = digitalio.DigitalInOut(board.D25)
lcd_d5 = digitalio.DigitalInOut(board.D24)
lcd_d6 = digitalio.DigitalInOut(board.D23)
lcd_d7 = digitalio.DigitalInOut(board.D18)


# Initialise the lcd class
lcd = characterlcd.Character_LCD_Mono(lcd_rs, lcd_en, lcd_d4, lcd_d5, lcd_d6,
                                      lcd_d7, lcd_columns, lcd_rows)

# looking for an active Ethernet or WiFi device
def find_interface():
    find_device = "ip addr show"
    interface_parse = run_cmd(find_device)
    for line in interface_parse.splitlines():
        if "state UP" in line:
            dev_name = line.split(':')[1]
    return dev_name

# find an active IP on the first LIVE network device
def parse_ip():
    find_ip = "ip addr show %s" % interface
    find_ip = "ip addr show %s" % interface
    ip_parse = run_cmd(find_ip)
    for line in ip_parse.splitlines():
        if "inet " in line:
            ip = line.split(' ')[5]
            ip = ip.split('/')[0]
    return ip

# run unix shell command, return as ASCII
def run_cmd(cmd):
    p = Popen(cmd, shell=True, stdout=PIPE)
    output = p.communicate()[0]
    return output.decode('ascii')

# wipe LCD screen before we start
lcd.clear()

# before we start the main loop - detect active network device and ip address
sleep(2)
interface = find_interface()
ip_address = parse_ip()

while True:

    # date and time
    lcd_line_1 = datetime.now().strftime('%b %d  %H:%M:%S\n')

    # current ip address
    lcd_line_2 = "IP " + ip_address

    # combine both lines into one update to the display
    lcd.message = lcd_line_1 + lcd_line_2

    sleep(2)[/mw_shl_code]
运行代码

运行代码很简单,直接输入下列命令即可。注意脚本的权限问题,可用chmod +x命令修改为可执行。

显示结果如

初始化脚本

能成功显示出时间和IP地址固然很好,但是这需要我们手动去启动 Adafruit_CharLCD_IPclock_example.py若是能在每次树莓派启动时,都能运行这个Python程序的话就会方便很多。下面我们将设置 Adafruit_CharLCD_IPclock_example.py为开机自启动,而在关机时会自动关闭。

将下段代码粘贴到 /etc/init.d/lcd,注意,需要root权限才能在这个目录下执行写操作。


  1. ### BEGIN INIT INFO
  2. # Provides: LCD - date / time / ip address
  3. # Required-Start: $remote_fs $syslog
  4. # Required-Stop: $remote_fs $syslog
  5. # Default-Start: 2 3 4 5
  6. # Default-Stop: 0 1 6
  7. # Short-Description: Liquid Crystal Display
  8. # Description: date / time / ip address
  9. ### END INIT INFO
  10. #! /bin/sh
  11. # /etc/init.d/lcd
  12. export HOME
  13. case "$1" in
  14.     start)
  15.         echo "Starting LCD"
  16.         /home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCD/Adafruit_CharLCD_IPclock_example.py  2>&1 &
  17.     ;;
  18.     stop)
  19.         echo "Stopping LCD"
  20.     LCD_PID=`ps auxwww | grep Adafruit_CharLCD_IPclock_example.py | head -1 | awk '{print $2}'`
  21.     kill -9 $LCD_PID
  22.     ;;
  23.     *)
  24.         echo "Usage: /etc/init.d/lcd {start|stop}"
  25.         exit 1
  26.     ;;
  27. esac
  28. exit 0
复制代码
你需要相应的将路径修改为你实际保存该脚本的路径才行。

修改初始化脚本的执行权限:

[color=teal !important]sudo chmod[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]+x[color=rgb(0, 111, 224) !important] [color=rgb(0, 111, 224) !important]/etc[color=rgb(0, 111, 224) !important]/[color=rgb(0, 45, 122) !important]init[color=rgb(51, 51, 51) !important].d[color=rgb(0, 111, 224) !important]/lcd

用update-rc.d命令使系统感知lcd初始化脚本:

[color=teal !important]sudo update[color=rgb(0, 111, 224) !important]-[color=rgb(0, 45, 122) !important]rc[color=rgb(51, 51, 51) !important].d[color=rgb(0, 111, 224) !important] [color=teal !important]lcd defaults

现在每次启动树莓派的时候lcd也会自动启动并显示出系统的时间和IP地址到屏幕上。这样你就可以在不用屏幕显示器的情况下知道树莓派的IP地址以及何时可以连接上它。

时区

最后但也是最重要的是:我的树莓派是按世界统一时间(UTC)配置的,但是我想让它显示出我所在的本地时间。以下命令可将树莓派设定为任意时区的本地时间,这个命令是一次性的,一旦完成设定,重启之后也不会失效。

sudo dpkg-reconfigure tzdata

【转载】用树莓派驱动一个16×2的LCD图6
【转载】用树莓派驱动一个16×2的LCD图7
【转载】用树莓派驱动一个16×2的LCD图8
【转载】用树莓派驱动一个16×2的LCD图9指令输入之后会转到一个选择时间域的程序,下移光标选择你所在的时区就可以了。原文链接: Mikey Sklar



dsweiliang  初级技神

发表于 2016-4-22 00:15:44

用arduino驱动反而简单很多
回复

使用道具 举报

hnyzcj  版主

发表于 2016-4-22 06:36:21

dsweiliang 发表于 2016-4-22 00:15
用arduino驱动反而简单很多

ARDUINO是简单,但是后面你玩起来你会觉得受限制的,不如树莓的
回复

使用道具 举报

dsweiliang  初级技神

发表于 2016-4-22 08:35:17

hnyzcj 发表于 2016-4-22 06:36
ARDUINO是简单,但是后面你玩起来你会觉得受限制的,不如树莓的

如果只是驱动一块LCD,用树莓派有点浪费了
回复

使用道具 举报

dsweiliang  初级技神

发表于 2016-4-22 08:35:50

楼主有驱动OLED的教程吗?
回复

使用道具 举报

dsweiliang  初级技神

发表于 2016-4-22 08:36:27

dsweiliang 发表于 2016-4-22 08:35
楼主有驱动OLED的教程吗?

例如这一款https://www.dfrobot.com.cn/goods-804.html
回复

使用道具 举报

hnyzcj  版主

发表于 2016-4-22 09:59:32

dsweiliang 发表于 2016-4-22 08:36
例如这一款https://www.dfrobot.com.cn/goods-804.html

我有个这个不过貌似坏了
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-4-22 10:44:46

dsweiliang 发表于 2016-4-22 08:35
楼主有驱动OLED的教程吗?

OLED教程有的,不过是1306芯片的SPI的OLED,这种淘宝很容易买到的
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-4-22 10:45:26

dsweiliang 发表于 2016-4-22 08:36
例如这一款https://www.dfrobot.com.cn/goods-804.html

https://mc.dfrobot.com.cn/thread-13396-1-1.html
回复

使用道具 举报

大连林海  初级技神

发表于 2016-4-22 19:37:45

学习中
回复

使用道具 举报

凌风清羽  中级技匠
 楼主|

发表于 2016-4-22 23:05:17


哈哈,共同学习~
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail