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的库里
文件夹说明: LiquidCrystal_I2C-master ——LCD1602 I2C库(库需要复制在arduino的库目录里)
//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上的设备地址,大家可以试试哦~~~
转载地址:https://blog.csdn.net/ling3ye/article/details/51542424