您的位置:首页 > 产品设计 > UI/UE

Arduino 和LCD1602液晶屏 I2C接口实验

2016-05-31 11:07 2131 查看

LCD1602液晶屏 带I2C接口



为什么使用I2C接口?因为省IO口啊,只需要4条线,可以看看之前发的实验,接线多复杂呀,对吧。

(传送门:Arduino携手LCD1602 向世界问好“hello World!”

还有背光灯,和可调节对比度(就是背面蓝色那块可以旋转的调)

具体怎么实现解决问题,请往下看

引脚说明

GND ------ 地线 

VCC ------ 电源(5V or 3.3v 电源不同显示效果有点差别)

SDA ------ I2C 数据线

SCL ------ I2C 时钟线

接线方法

LCD1602 i2c模块                         Ardunio Uno

GND                           <------>        GND接地线

VCC                           <------>         5V 接电源

SDA                            <------>         A4

SCL                            <------>         A5

程序实现

需要用到LCD1602 I2C的库,下载地址是 https://github.com/marcoschwartz/LiquidCrystal_I2C

把下载的库放到Arduino的库里(PS:不懂库是啥?请点击这里

//LingShun lab
#include <Wire.h>
#include <LiquidCrystal_I2C.h> //引用I2C库

//设置LCD1602设备地址,这里的地址是0x3F,一般是0x20,或者0x27,具体看模块手册
LiquidCrystal_I2C lcd(0x3F,16,2);

void setup()
{
lcd.init();                  // 初始化LCD
lcd.backlight();             //设置LCD背景等亮
}

void loop()
{
lcd.setCursor(0,0);                //设置显示指针
lcd.print("LCD1602 iic Test");     //输出字符到LCD1602上
lcd.setCursor(0,1);
lcd.print("         by L.L.");
delay(1000);
}


实例效果



问题解疑

是一个模块还是两个模块?

这模块是通过LCD1602屏 和 LCD1602 I2C 模块 焊接结合的,可以直接买焊接好的,也可以分开买,不过就需要点动手能力。

无法正常显示?

刚上电的时候,老是显示一个个方块,如图



这情况一般是地址错误,我根据说明上写的地址0x20,0x27 都试了个遍还是无法正常显示

后来通过一网友的帖子把这问题给解决了,

贴出了一个寻找设备地址的代码 (来源:Arduino驱动IIC/I2C LCD1602模块显示(4根线解决连接)

#include <Wire.h>

void setup(){
Wire.begin();
Serial.begin(9600);
Serial.println("\nI2C Scanner");
}
void loop(){
byte error, address;
int nDevices;
Serial.println("Scanning...");
nDevices = 0;
for (address = 1; address < 127; address++ ){
// The i2c_scanner uses the return value of
// the Write.endTransmisstion to see if
// a device did acknowledge to the address.
Wire.beginTransmission(address);
error = Wire.endTransmission();
if (error == 0){
Serial.print("I2C device found at address 0x");
if (address < 16)
Serial.print("0");
Serial.print(address, HEX);
Serial.println(" !");
nDevices++;
}else if (error == 4){
Serial.print("Unknow error at address 0x");
if (address < 16)
Serial.print("0");
Serial.println(address, HEX);
}
}
if (nDevices == 0)
Serial.println("No I2C devices found\n");
else
Serial.println("done\n");
delay(5000); // wait 5 seconds for next scan
}


把模块按接线方法接好,上传这段代码后,打开端口监视器,就能找到在I2C上的设备地址,大家可以试试哦
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: