• HOME
  • DOCS
  • WTF
  • TECH
  • LIFE
  • PAGES
    • ARCHIVE
    • TAGS
    • ABOUT
    • FRIENDS
    • RSS
  • TOOLS
    • GEO
    • RANDOM()
    • GOO.GL
    • CSS HEART
Aj's Blog

记录时间溜走的瞬间和折腾过的那些事

树莓派+LCD1602实现系统监控: IP/时钟/温度/内存

2014-06-24  TECH  CPU  GPU  IP  LCD1602  时钟  树莓派  监控  9  

一、GPIO引脚说明

不得不说树莓派的各种命名真心乱
GPIO

二、LCD1602引脚说明

LCD1602有3.3V、5.0V、黄屏、蓝屏等分类方式,但引脚定义都是一样的
lcd1602-datasheet

三、接线

LCD1602提供4位数据与8位数据两种工作模式
因为raspi的GPIO口数量很有限,所有我们使用4位数据模式

VSS,接地,RPi PIN 6
VDD,接5V电源,PRi PIN 2
VO,液晶对比度调节,接电位器中间的引脚
RS,寄存器选择,接GPIO 14,RPi PIN 8
RW,读写选择,接地,表示写模式,PRi PIN 6
EN,使能信号,接GPIO 15,RPi PIN 10
D0,数据位0,4位模式,不接
D1,数据位1,4位模式,不接
D2,数据位2,4位模式,不接
D3,数据位3,4位模式,不接
D4,数据位4,接GPIO 17,RPi PIN 11
D5,数据位5,接GPIO 18,RPi PIN 12
D6,数据位6,接GPIO 27,RPi PIN 13
D7,数据位7,接GPIO 22,RPi PIN 15
A,液晶屏背光+,接5V,RPi PIN 2
K,液晶屏背光-,接地,RPi PIN 6

四、python库

Adafruit有个现成的LCD1602库: https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_CharLCD
不是过针对Raspberry R1的,我用的R2,查了一些资料后发现只是几个引脚定义的变化,所以就自己修改了:

### line #57
def __init__(self, pin_rs=14, pin_e=15, pins_db=[17, 18, 27, 22], GPIO = None):
### line #68: 插入
self.GPIO.setwarnings(False)

修改后的库: lcd1602_R2

五、监控代码

#!/usr/bin/python

from lcd1602 import *
from datetime import *
import commands

def get_cpu_temp():
	tmp = open('/sys/class/thermal/thermal_zone0/temp')
	cpu = tmp.read()
	tmp.close()
	return '{:.2f}'.format( float(cpu)/1000 ) + ' C'

def get_gpu_temp():
	tmp = commands.getoutput('vcgencmd measure_temp|awk -F= \'{print $2}\'').replace('\'C','')
	gpu = float(tmp)
	return '{:.2f}'.format( gpu ) + ' C'

def get_time_now():
	return datetime.now().strftime('    %H:%M:%S\n   %Y-%m-%d')

def get_ip_info():
	return commands.getoutput('ifconfig wlan0|grep inet|awk -Faddr: \'{print $2}\'|awk \'{print $1}\'')

def get_mem_info():
	total= commands.getoutput('free -m|grep Mem:|awk \'{print $2}\'')	
	free = commands.getoutput('free -m|grep cache:|awk \'{print $4}\'')	
	return 'MEM:\n    ' + free +' / '+ total +' M'

lcd = lcd1602()
lcd.clear()

if __name__ == '__main__':

	while(1):
		lcd.clear()
		lcd.message( get_ip_info() )
		sleep(5)
		
		lcd.clear()
		lcd.message( get_time_now() )
		sleep(5)
		
		lcd.clear()
		lcd.message( get_mem_info() )
		sleep(5)

		lcd.clear()
		lcd.message( 'CPU: ' + get_cpu_temp()+'\n' )
		lcd.message( 'GPU: ' + get_gpu_temp() )
		sleep(5)

参考资料:
http://hugozhu.myalert.info/2013/03/16/19-raspberry-pi-drive-1602-lcd.html
https://learn.adafruit.com/drive-a-16×2-lcd-directly-with-a-raspberry-pi/wiring

运行效果:

面包版调试
用面包版+GPIO扩展版调试 飞线太麻烦 也不美观
运行效果-1
转接板连接,启动时

运行效果-2
转接板连接,运行时
转接板-1
转接板:双面板x1、26p超长排母x1、90度排母x1、电位器x1、导线若干

转接板-2
LCD1602和转接板连接效果
下一篇:   清华同方X46H刷BIOS开启VT-x
上一篇:   goagent安全隐患与解决方法
  • HeroX says:
    September 28, 2016 at 00:17

    v0 必须接电位器么? 没有电位器的不就别催了么?

    Reply
    • Aj says:
      September 29, 2016 at 04:49

      没有电位器,随便找个电阻也能将就用,用来调LCD背景和前景对比度的。

      Reply
  • 科技爱好者博客 says:
    June 17, 2016 at 21:32

    我的树莓派2B,按照教程弄,为什么没有显示?LCD1602上面全是方块,下面空白,这是什么情况?

    Reply
  • 树莓派连接LCD1602 | 素人派 says:
    March 24, 2015 at 20:15

    […] http://www.6zou.net/works/raspberry-pi-lcd1602-system-monitor.html […]

    Reply
  • 素人派 says:
    March 16, 2015 at 21:38

    你好,如果5V的连接.3V可以吗?

    Reply
    • Aj says:
      March 17, 2015 at 09:07

      可以,亮度变低而已

      Reply
      • 素人派 says:
        March 17, 2015 at 16:25

        您亲自做过实验,是吗?

        Reply
        • Aj says:
          March 17, 2015 at 22:35

          没有,看别人讨论过5V的接3V的是可以的,反过来不行

          Reply
          • 素人派 says:
            March 17, 2015 at 22:37

            谢谢!
            欢迎回访http://surenpi.com

  • Cancel reply