简介
LCD1602是一种工业字符型液晶,能够同时显示16x02即32个字符。LCD1602液晶显示的原理是利用液晶的物理特性,通过电压对其显示区域进行控制,即可以显示出图形。
引脚说明
引脚 符号 说明
1 GND 接地
2 VCC 5V正极
3 V0 对比度调整,接正极时对比度最弱
4 RS 寄存器选择,1数据寄存器(DR),0指令寄存器(IR)
5 R/W 读写选择,1度,0写
6 EN 使能(enable)端,高电平读取信息,负跳变时执行指令
7~14 D0~D7 8位双向数据
15 BLA 背光正极
16 BLK 背光负极
3脚电位器
一个滑动变阻器,中间接负极(输出),两边分别接电源正极和接地(或不接)
材料
大面包板 x1
3脚电位器 x1
LCD 1602 x1
Arduino UNO x1
接线示意图
LCD1602 —> Arduino UNO 说明
GND —> GND 接地
VCC —> 5V 5V电源
V0 —> 连接3脚继电器中间,用于调节对比度
RS —> 3 随便接一个输出口,方便接线、画图
R/W —> GND 接地,写模式
EN —> 5 随便接一个输出口,方便接线、画图
D0~D3 —> 4位工作模式,不使用
D4~D7 —> 10~13 其它口也行,方便接线、画图
BLA —> 背光,电源正极,可选
BLK —> 背光,接地,可选
代码
加载库文件
打开Arduino IDE,选项目 -> 加载库 -> 管理库中搜索LiquidCrystal,然后安装即可,笔者的IDE版本为1.6.12,自带该库。
示例代码, hello word
//引入依赖
#include <LiquidCrystal.h>
// 初始化针脚
const int rs = 3, en = 5, d4 = 10, d5 = 11, d6 = 12 d7 = 13;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
//设置LCD要显示的列数、行数,即2行16列
lcd.begin(16, 2);
//输出Hello World
lcd.print("hello, world!");
}
void loop() {
//设置光标定位到第0列,第1行(从0开始)
lcd.setCursor(0, 1);
//打印从重置后的秒数
lcd.print( millis() / 1000);
}
示例代码,自动滚屏
//引入依赖
#include <LiquidCrystal.h>
// 初始化针脚
const int rs = 3, en = 5, d4 = 10, d5 = 11, d6 = 12, d7 = 13;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
char arr [9]= {'a', 'b', 'c', 'd' ,'e' ,'f' ,'g', 'h', 'i'};
void setup() {
//设置LCD要显示的列数、行数,即2行16列
lcd.begin(16, 2);
}
void loop() {
//输出1-9,a1-9,b,1-9,c
lcd.setCursor(0, 0);
lcd.autoscroll();
for(int i = 0; i < 9; i++){
for(int j = 1; j < 10; j++){
lcd.print(j);
delay(500);
}
lcd.print(arr[i]);
delay(500);
}
//关闭自动滚屏
lcd.noAutoscroll();
//为下重循环清屏
lcd.clear();
}
————————————————
2.通过PCF8574T转接板与Arduino相连
通过此种方式,可以大大节省Arduino的IO口,前提是你还得购买一块PCF8574T转接板。
LCD显示屏与转接板
所需要材料
- 1x Arduino UNO
- 1x LCD 16x2
- 1x PCF8574T转接板
- 电烙铁、焊锡、松香等
接线
首先,把转接板焊接到LCD显示屏上(方向如上图)
PCF8574T | Arduino | |
---|---|---|
GND | -> | GND |
VCC | -> | 5V |
SDA | -> | A4 |
SCL | -> | A5 |
如果你的A4,A5口已经被占用,那么你还可以接到Arduino最上面的两个没有标文字的IO口,即D0-D13那一排最上面的那两个口
- SCL -> 最上面的口
- SDA -> 第二个口
扫描I2C地址
将以下代码拷贝到Arduino IDE,并执行。然后选择工具
->串口监视器
,把右下角的波特率改为115200
,即可读出I2C地址,如下图。
// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011
#include <Wire.h>
void setup() {
Serial.begin (115200); // Leonardo: wait for serial port to connect
while (!Serial) { }
Serial.println ();
Serial.println ("I2C scanner. Scanning ...");
byte count = 0;
Wire.begin();
for (byte i = 8; i < 120; i++) {
Wire.beginTransmission (i);
if (Wire.endTransmission () == 0) {
Serial.print ("Found address: ");
Serial.print (i, DEC);
Serial.print (" (0x");
Serial.print (i, HEX);
Serial.println (")");
count++;
delay (1); // maybe unneeded?
} // end of good response
} // end of for loop
Serial.println ("Done.");
Serial.print ("Found ");
Serial.print (count, DEC);
Serial.println (" device(s).");
} // end of setup
void loop() {}
加载库文件
到这里下载最新的New LiquidCrystal,手动添加到你的 Arduino IDE中。(ps:记得修改你的I2C地址,把LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7);
的0x3F
改为你的真实地址)
示例代码
/* Demonstration sketch for PCF8574T I2C LCD Backpack Uses library from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads GNU General Public License, version 3 (GPL-3.0) */
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified backpack
void setup() { // activate LCD module
lcd.begin (16,2); // for 16 x 2 LCD module
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
}
void loop() {
lcd.home (); // set cursor to 0,0
lcd.print(" tronixlabs.com");
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print(millis());
delay(1000);
lcd.setBacklight(LOW); // Backlight off delay(250);
lcd.setBacklight(HIGH); // Backlight on delay(1000);
}