lou 发表于 2020-4-27 14:19:29

基于Arduino使用LCD1602显示屏

基于Arduino使用LCD1602显示屏



引脚说明
GND         <------>       地线VCC 电源    <------>      (5V or3.3v 电源不同显示效果有点差别)SDA I2C      <------>       数据线SCL I2C       <------>       时钟线
接线方法
LCD1602 i2c模块      <------>      Ardunio UnoGND                        <------>      GND接地线VCC                         <------>      5V 接电源SDA                         <------>       A4SCL                        <------>       A5
程序实现
需要用到LCD1602I2C的库,下载地址是 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 如果2个都不对,就需要读取地址。
寻找设备地址的代码
#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上的设备地址
页: [1]
查看完整版本: 基于Arduino使用LCD1602显示屏