天天看點

Arduino Due 與 I2C LCD1602 的正确配置(完美運作)

由于很多"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、連接配接方式

Arduino Due 與 I2C LCD1602 的正确配置(完美運作)

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
}
           

繼續閱讀