Arduino 使用 1602 IIC(I2C) LCD 點陣液晶模組

這裡介紹如何在 Arduino 上面使用 Hitachi HD44780U 1602 LCD 點陣液晶模組,顯示簡單的文字。


這是一片 Hitachi HD44780U 1602 LCD 點陣液晶模組,這個模組很便宜,拍賣網站上買的話,不用一百元就可以買到。


在我買的這塊模組的背面已經焊接好一片 LCM1602 IIC V1 控制板,他可以讓我們很方便的使用 IIC(I2C)來控制 LCD 顯示器,而有些網站上賣的並沒有加上這一片,購買的時候要注意看。


這是側面的樣子。



有了這片顯示模組之後,就可以直接讓沒有螢幕輸出的 Arduino 顯示一些簡單的文字訊息,以下是針腳的接法與控制的程式。

Step 1
在顯示器背面的 LCM1602 IIC V1 控制板有 I2C 的四個針腳,分別為 GND、VCC、SDA 與 SCL,用杜邦線全部接出來。


Step 2
然後參考一下 Arduino 的 pinout 參考圖,接上對應的腳位,這裡我以 Arduino UNO 為例。


連接的方式很簡單,VCC 接到 5V,GND 接到 GND,SDA 接到 SDA,SCL 接到 SCL。


Step 3
安裝 LiquidCrystal 函式庫,下載之後解壓縮到 Arduino 的 libraries 目錄中即可。

Step 4
接著開始編寫控制的程式:
#include <Wire.h>  // Arduino IDE 內建
// LCD I2C Library,從這裡可以下載:
// https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
#include <LiquidCrystal_I2C.h>

// Set the pins on the I2C chip used for LCD connections:
//                    addr, en,rw,rs,d4,d5,d6,d7,bl,blpol
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);  // 設定 LCD I2C 位址

void setup() {
  Serial.begin(115200);  // 用於手動輸入文字
  lcd.begin(16, 2);      // 初始化 LCD,一行 16 的字元,共 2 行,預設開啟背光

  // 閃爍三次
  for(int i = 0; i < 3; i++) {
    lcd.backlight(); // 開啟背光
    delay(250);
    lcd.noBacklight(); // 關閉背光
    delay(250);
  }
  lcd.backlight();

  // 輸出初始化文字
  lcd.setCursor(0, 0); // 設定游標位置在第一行行首
  lcd.print("Hello, world!");
  delay(1000);
  lcd.setCursor(0, 1); // 設定游標位置在第二行行首
  lcd.print("GTWang.org");
  delay(8000);

  // 告知使用者可以開始手動輸入訊息
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Use Serial Mon");
  lcd.setCursor(0, 1);
  lcd.print("Type to display");
}

void loop() {
  // 當使用者手動輸入訊息
  if (Serial.available()) {
    // 等待一小段時間,確認資料都接收下來了
    delay(100);
    // 清除舊訊息
    lcd.clear();
    // 讀取新訊息
    while (Serial.available() > 0) {
      // 將訊息顯示在 LCD 上
      lcd.write(Serial.read());
    }
  }
}

Step 5
將程式編譯上傳至 Arduino。


如果正常的話,一開始顯示器上面就會出現一些初始訊息。


等待幾秒鐘之後,顯示器會出現「Use Serial Mon Type to display」,這時候我們就可以開啟 Arduino IDE 中的 Serial Monitor 來輸入文字。


按下「Send」之後,這個訊息就會直接顯示在 LCD 上面了。


參考資料:Arduino-Info
本站已經搬家了,欲查看最新的文章,請至 G. T. Wang 新網站

2 則留言:

  1. This is a really very informative article, there is no doubt about it. Thanks for sharing this article with us. This is very nice of you 경마사이트

    回覆刪除
  2. Interesting to read more info abut arduino. Expecting more info about arduino. DUI Lawyer Fairfax VA

    回覆刪除