Arduino实践(二)lcd1602使用说明,源码,寻找IIC(I2c)设备地址

871 阅读3分钟

持续创作,加速成长!这是我参与「掘金日新计划 · 6 月更文挑战」的第16天,点击查看活动详情

介绍

1602液晶显示器是一种常见的字符液晶显示器,因其能显示16*2个字符而得名。

在这里插入图片描述

使用说明

  1. 使用IIC LCD1602模块集成了IIC I/O扩展芯片,通过两线制的IIC总线(串行时钟线SCL,串行数据线SDA),可使Arduino实现控制LCD 1602显示的目的。这样做的好处是接线少,不用IIC LCD1602模块arduino接完1602后接口就很少了

  2. 通过设置跳线可以设置地址: 0x20-0x27。

  3. 模块背面可以看到一块蓝色的旋钮,旋转它可以调节1602液晶显示器的对比度(在显示模糊时调节)。

  4. 背后的接线引脚分别为GND;VCC;SDA;SCL(SDA和SCL分别为iic通讯的数据线和时钟线)

  5. 需要 添加库函数:项目-加载库-管理库,在搜索框内搜索liquidCrystal可找到若干关于驱动液晶的库文件,在下面就有LiquidCrystal_I2C相关的库文件,点击安装即可。也可以去GitHub下载LCD1602_I2C的库,下载地址是 github.com/marcoschwar…

接线说明

引脚说明及接线方法

引脚说明对应arduino引脚
GND地线接地线
VCC电源(5V or 3.3v 电源不同显示效果有点差别)接电源
SDAI2C 数据线A4
SCLI2C 时钟线A5

常见问题

  1. 模块是通过LCD1602屏 和 LCD1602 I2C 模块 焊接结合的,可以直接买焊接好的,也可以分开买。
  2. 无法正常显示? 刚上电的时候,老是显示一个个方块, (1)倾斜一定角度时候可以看到模糊的显示,是对比度的问题调节背面蓝色的旋钮 (2)无上面这种情况一般是iic地址错误,地址一般是0x3F,0x20,或者0x27比较多 (我的另一篇博客中有查找设备iic地址的方法)

源代码及代码解释

/*1602*/


#include <LiquidCrystal_I2C.h> //引用I2C库
#include <Wire.h>
//设置LCD1602设备地址,这里的地址是0x3F,一般是0x20,或者0x27,具体看模块手册,,,,,,
//SDA <------> A4     SCL <------> A5  
LiquidCrystal_I2C lcd(0x27,16,2);//将16个字符和2行显示的LCD地址设置为0x27
//                名  地址   
void setup() {
lcd.init(); // 初始化LCD
lcd.backlight(); //设置LCD背景等亮
lcd.setCursor(0,0); //设置显示指针
lcd.print("Welcome to "); //输出字符到LCD1602上
lcd.setCursor(0,1);
lcd.print("It's my system");
delay(2000);
lcd.clear();
}

void loop() {
// put your main code here, to run repeatedly:
lcd.setCursor(0,0);
lcd.print("hello");
lcd.setCursor(0,1);
lcd.print("world");
}

arduino寻找IIC(I2c)设备地址

寻找IIC设备地址 官方地址 官方地址:i2c_scanner(playground.arduino.cc/Main/I2cSca…

==示例==: 可参考我的另外一篇博客Arduino实践(二)lcd1602 把模块按接线方法接好,上传这段代码后,打开端口监视器,就能找到在I2C上的设备地址.

#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
}```