搜尋程式碼

LCD2004顯示器配合I2C嘅函式庫同Arduino代碼

LCD2004顯示器配合I2C嘅函式庫同Arduino代碼

喺呢個教學入面,我哋會探討點樣用 I2C 通訊嚟使用 LCD2004 顯示屏。呢個設定可以畀你喺四行上面顯示文字,令到佢好適合各種應用。我哋會涵蓋所需嘅接線、程式碼片段,以及點樣有效咁實現呢個功能。

我哋會用 LiquidCrystal_I2C 函式庫嚟管理 LCD 顯示屏。呢個函式庫透過幫你處理 I2C 通訊,簡化咗同 LCD 嘅互動。為咗全面理解,記得睇吓條片嚟獲取視覺指引(片入面 00:00)。

硬件解說

呢個項目嘅主要元件係 LCD2004 顯示屏,佢有 20 欄同 4 行嚟輸出文字。佢用 I2C 嚟通訊,比起傳統嘅並行連接,需要嘅接線少好多。咁令到佢好適合 Arduino 上面針腳有限嘅項目。

I2C 介面用兩條線,SDA(數據線)同 SCL(時鐘線),佢哋連接去 Arduino 上面相應嘅針腳。LCD 亦都需要電源同接地連接。喺呢個設定入面,我哋會將顯示屏連接去 5V 電源,可以直接由 Arduino 提供。

數據手冊詳情

製造商通用
零件編號LCD2004
邏輯/IO 電壓5 V
供電電壓5 V
輸出電流(每通道)通常 20 mA
峰值電流(每通道)通常 40 mA
PWM 頻率指引不適用
輸入邏輯閾值0.3 Vcc(低),0.7 Vcc(高)
電壓降 / RDS(on) / 飽和通常 0.2 V
熱限制0°C 至 50°C
封裝標準 4x20 LCD
備註 / 變體有唔同背光選項可供選擇

  • 確保電源供應正確,避免損壞。
  • 如有需要,喺 SDA 同 SCL 線上加拉高電阻。
  • 檢查 I2C 地址;呢個顯示屏通常係 0x27。
  • 保持接線短,以減少干擾。
  • 上載程式碼之前,確認函式庫已經正確安裝。

接線說明

要將 LCD2004 顯示屏經 I2C 連接去 Arduino,跟住以下接線:

  • 將 LCD 嘅 VCC 針腳連接去 Arduino 嘅 5V 針腳。
  • 將 LCD 嘅 GND 針腳連接去 Arduino 嘅 GND 針腳。
  • 將 LCD 嘅 SDA 針腳連接去 Arduino 嘅 A4 針腳。
  • 將 LCD 嘅 SCL 針腳連接去 Arduino 嘅 A5 針腳。

如果你嘅 Arduino 板有唔同嘅 I2C 針腳,記得相應咁調整接線。條片入面都有示範另一個接線配置(片入面 01:30)。

程式碼示例同講解

等我哋睇吓一個簡單嘅示例,初始化 LCD 同顯示訊息。以下片段設定 LCD 並喺屏幕上印出「Hello, world!」。

lcd.begin(); // 初始化 LCD
lcd.backlight(); // 開啟背光
lcd.print("Hello, world!"); // 顯示訊息

呢段程式碼初始化 LCD 並啟動背光。然後,佢會喺 LCD 嘅第一行顯示訊息。喺光線不足嘅環境下,背光對可見性嚟講好重要。

跟住,我哋會睇一個更複雜嘅示例,顯示動態數據,例如電壓讀數。

lcd.setCursor(0,0); // 將游標設定去第一行
lcd.print("Voltage: "); // 顯示電壓標籤
float v = 8.254; // 示例電壓值
lcd.print(v); // 印出電壓值

喺呢段摘錄入面,我哋將游標位置設定去第一行,並印出一個電壓標籤,後面跟住實際嘅電壓值。咁可以令你以用戶友好嘅格式顯示實時測量數據。

為咗額外功能,你可以實現串列輸入,等用戶可以透過串列監視器輸入字符,並將佢哋顯示喺 LCD 上面。

if (Serial.available()) { // 檢查有冇串列輸入
  lastChar = Serial.read(); // 讀取最後一個字符
  lcd.write(lastChar); // 將字符顯示喺 LCD 上
}

呢段程式碼檢查串列埠有冇任何可用數據。如果有,佢會讀取輸入嘅最後一個字符,並將佢顯示喺 LCD 上。呢種互動透過容許根據用戶輸入嚟動態顯示,增強咗用戶體驗。

至於完整程式碼,請參考文章下面載入嘅完整程式。

示範 / 預期效果

當你執行程式碼時,你應該會見到初始訊息顯示喺 LCD 上,之後係電壓讀數,以及任何透過串列監視器輸入嘅字符。確保連接穩固,避免任何顯示問題。如果 LCD 冇顯示任何嘢,請再檢查接線同 I2C 地址(片入面 02:15)。

圖片

LCD2004_display-3
LCD2004_display-3
LCD2004_display-1
LCD2004_display-1
LCD2004_display-2
LCD2004_display-2
121-Arduino code for LCD2004 with I2C: Hello World sketch
語言: C++
/*
 *  Download library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
 *  
 *  Updated by Ahmad Shamshiri on July 8, 2018 at 19:14 in Ajax, Ontario, Canada
 * for Robojax.com
 * Get this code from Robojax.com
 * Watch video instructions for this code at: https://youtu.be/DKmNSCMPDjE
 * 
 */
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup()
{
	// initialize the LCD
	lcd.begin();

	// Turn on the backlight and print a message.
	lcd.backlight();
	lcd.print("Hello, world!");
}

void loop()
{
	// Do nothing here...
}
122-Arduino code for LCD2004 with I2C RoboJax custom sketch
語言: C++
/*
 * 
 * *  Download library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
 * This code is basic usage of an LCD2004 display with I2C.
 * It will display text in 4 lines, each with 20 characters.
 * It displays multiple float values and text on the same line.
 * 
 * 
 * 
 * Updated by Ahmad Shamshiri on July 08, 2018 at 09:20 in Ajax, Ontario, Canada
 * for Robojax.com
 * Get this code from Robojax.com
 * Watch video instructions for this code at: https://youtu.be/DKmNSCMPDjE
 * 

 */

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup() {

  // initialize the LCD, 
  lcd.begin();
  // Turn on the backlight and print a message.
  lcd.backlight();
  lcd.clear();
   lcd.setCursor (0,0); //
   lcd.print("Robojax LCD2004 Test"); 
   lcd.setCursor (0,1); //   
 
   lcd.print("Please Wait - 3");  
   lcd.setCursor (0,1); // 
   delay(1000);        
   lcd.print("Please Wait - 2");  
   delay(1000); 
   lcd.setCursor (0,1); //      
   lcd.print("Please Wait - 1");  
   delay(1000);       
}

void loop() {

    // Robojax.com LCD2004 with I2C custom code
  lcd.clear();// clear previous values from screen
  lcd.setCursor (0,0); //character zero, line 1
  lcd.print("LCD2004 I2C Example"); // print text  

  lcd.setCursor (4,1); //character 4, line 2
  lcd.print("Robojax.com"); // print text   

  lcd.setCursor (0,2); //character 0, line 3
  lcd.print("Voltage: "); // print text 
  float v = 8.254;// define or get voltage
  char VoltageStr[5];
  dtostrf(v, 5, 3, VoltageStr );
  lcd.setCursor (9,2); //character 9, line 3  
  lcd.print(VoltageStr); // print voltage
  lcd.setCursor (14,2); //character 14, line 3
  lcd.print("V"); // print V at the end of voltage  

  lcd.setCursor (0,3); //character 0, line 4
  lcd.print("X: "); // print x
  float xdeg = -123.87;// define or get x degree (just example)
  lcd.setCursor (3,3); //character 8, line 3  
  lcd.print(xdeg); // print xdeg value

  
  lcd.setCursor (12,3); //character 12, line 4
  lcd.print("Y: "); // print Y
  float ydeg = 32.8;// define or get y degree (just example)
  lcd.setCursor (15,3); //character 15, line 4  
  lcd.print(ydeg); // print ydeg value
      
                     
   delay(100);

}// loop end
123-Arduino code for LCD2004 with I2C: blinking sketch
語言: C++
/*
 * 
 *  Download library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
 * This code is basic usage of an LCD2004 display with I2C 
 * It will display a blinking cursor on the screen 
 * 

 * Get this code from Robojax.com
 * Watch video instructions for this code at: https://youtu.be/DKmNSCMPDjE
 * 

 */
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);

void setup()
{
	// initialize the LCD
	lcd.begin();
}

void loop()
{
	bool blinking = true;
	lcd.cursor();

	while (1) {
		if (blinking) {
			lcd.clear();
			lcd.print("No cursor blink");
			lcd.noBlink();
			blinking = false;
		} else {
			lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Your name: ");
      lcd.blink();
			blinking = true;
		}
		delay(4000);
	}
}
124-Arduino code for LCD2004 with I2C serial monitor sketch
語言: C++
/*
 * 
 *  Download library: https://github.com/fdebrabander/Arduino-LiquidCrystal-I2C-library
 * This code is basic usage of an LCD2004 display with I2C.
 * This code will allow you to enter a character on the serial monitor and display it on the screen.
 * 

 * Updated by Ahmad Shamshiri on July 08, 2018 at 09:20 in Ajax, Ontario, Canada
 * for Robojax.com
 * Get this code from Robojax.com
 * Watch video instructions for this code at: https://youtu.be/DKmNSCMPDjE
 * 
 */
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// Set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);

char lastChar ='_';
void setup()
{
	lcd.begin();
	lcd.backlight();
  lcd.print("Robojax.com Test");
	// Initialize the serial port at a speed of 9600 baud
	Serial.begin(9600);
}

void loop()
{

      lcd.clear();
      lcd.setCursor(0,1);
      lcd.print("Enter Letter: ");

      lcd.setCursor(13,1);  
	// If characters arrived over the serial port...
	if (Serial.available()) {
		// Wait a bit for the entire message to arrive
		delay(100);


		// Write all characters received via the serial port to the LCD.
		while (Serial.available() > 0) {
      lastChar =Serial.read();
      Serial.print("Entered: ");
      Serial.println(lastChar);
		}//while end
	}// if end

  lcd.write(lastChar);// display last entered character
  delay(1000);


}// loop end

資源與參考資料

文件📁

Arduino 函式庫 (zip)