首先下载库文件
https://pan.baidu.com/s/1zLYSlDuxTMvxjclyk94hNg
注意:如果是从淘宝店下载的库文件有可能出现只显示一个首字符的现象,此时需要修改 LiquidCrystal_I2C.cpp 文件中的一个语句就可以了,更改如下:
inline size_t LiquidCrystal_I2C::write(uint8_t value) {
send(value, Rs);
return 0; 改为 return 1;
}
按如下方式接线
下面是地址检测代码
#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
}
编译运行后,找出模块的起始地址。
下面是显示字符的代码:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,4); // 此处0x27改成刚才检测到的地址即可。
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight();
lcd.setCursor(0, 0);
lcd.setCursor(0, 0);
lcd.print("Success!");
lcd.setCursor(0, 1);
lcd.print("Hello, world!");
}
void loop()
{}