由于很多"LiquidCrystal_I2C.h" 库不匹配 Arduino Due,导致I2C LCD1602 无法在 Arduino Due 上正常工作,我在外网上找了很久,发现了一个匹配的库。
简单粗暴,不说废话。
需要:
1、Arduino Due 板子
2、I2C LCD1602
3、Arduino IDE 开发环境
4、最重要的 "LiquidCrystal_I2C.h" 库(https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library)
5、连接方式
6、代码编写,参考下面的代码,替换自己的内容。
//Will Chen lab
#include <Wire.h>
#include <LiquidCrystal_I2C.h> //引用I2C库
//设置LCD1602设备地址,这里的地址是0x27,具体看模块手册
LiquidCrystal_I2C lcd(0x27,16,2);
void setup()
{
lcd.begin(); //初始化LCD
lcd.backlight(); //设置LCD背景等亮
}
void loop()
{
lcd.setCursor(0,0); //设置显示指针
lcd.print("LCD1602 iic Test"); //输出字符到LCD1602上
lcd.setCursor(0,1);
lcd.print(" by Will.C.");
delay(1000);
}
7、如果不知道自己的设备地址可以用下面代码查找(来源):
//Find Address
#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
}