LiquidCrystal 1602 介紹
Arduino 是一個可程式的控制裝置,只要您熟悉基本的C語言程式邏輯,加上Arduino的I/O控制方法,即可進入產品開發實戰。
一般的互動式產品設計通常具備一個資訊顯示裝置,在Arduino的領域經常會使用便宜又大碗的LCD1602的液晶顯示器。LCD1602顧名思義就是一種小型的液晶顯示器,可以顯示單一列16個字元的長度,一次可以顯示兩列。這種裝置經常在DVD播放機、機上盒、電腦機殼與磁碟陣列控制器使用。
Arduino想要整合LCD1602是一件非常簡單的工作,已經有許多種程式庫(library)提供操作介面供您使用,最著名的程式庫就是LiquidCrystal.h。

LCD1602硬體
傳統的LCD1602液晶顯示器是具備 4bit / 8bit 資訊傳輸的方式,因此配置有16個針腳,這些針腳可分為以下類別:
| 接腳代號 | 功能說明 |
| GND | 常見符號如VSS,連接電源的負極。 |
| VCC | 常見符號如VDD,連接電源+5V。 |
| VO | 透過電位計分壓原理,調整字體的對比亮度。 |
| RS | 暫存器的功能設定, 高電位(HIGH)將D0~D7資料放入資料暫存器。 低電位(LOW)將D0~D7資料放入指令暫存器。 |
| RW | Read/Write 模式設定。 高電位(HIGH)從LCD讀取資料。 低電位(LOW)將資料寫入LCD。 |
| E | Enable/Disable read information. 高電位(HIGH)啟用功能 |
| D0~D7 | 資料位元組傳送/接收 |
| LED+ | 液晶背光照明 LED+ |
| LED- | 液晶背光照明 LED- |

點亮LCD1602背光
Tinkercad線上模擬提供LCD1602裝置,從最基本的供電連接背光開始練習。LED+、LED-這兩個腳位是背光的供電腳位,若在+5V供電下,記得串聯220或330歐姆的電阻作為限流電阻,避免長時間處在過高電流而將背光LED燒毀。

LCD1602控制器供電、電位計調整LCD字體對比
將GND、VCC加入+5V電壓,讓LCD控制器開始工作。使用電位計分壓原理控制VO電壓,調整字體對比效果。

LCD的供電、背光供電以及使用分壓方式控制字體對比的簡單電路可以透過電池供電方式模擬示範,文字的顯示操作則需使用Arduino進行控制,後續的說明則以Arduino進行介紹。
使用Arduino LCD1602 範例程式模擬練習
Arduino IDE軟體提供 LCD1602 HelloWorld範例程式,可從選單內直接使用,操作如下圖。

此時Arduino IDE會將LCD1602範例程式碼載入,其中第48行就是宣告Arduino 與 LCD1602接線的方式,後續的LCD1602訊息顯示直接呼叫程式庫操作即可。
/*
LiquidCrystal Library - Hello World
Demonstrates the use a 16x2 LCD display. The LiquidCrystal
library works with all LCD displays that are compatible with the
Hitachi HD44780 driver. There are many of them out there, and you
can usually tell them by the 16-pin interface.
This sketch prints "Hello World!" to the LCD
and shows the time.
The circuit:
* LCD RS pin to digital pin 12
* LCD Enable pin to digital pin 11
* LCD D4 pin to digital pin 5
* LCD D5 pin to digital pin 4
* LCD D6 pin to digital pin 3
* LCD D7 pin to digital pin 2
* LCD R/W pin to ground
* LCD VSS pin to ground
* LCD VCC pin to 5V
* 10K resistor:
* ends to +5V and ground
* wiper to LCD VO pin (pin 3)
Library originally added 18 Apr 2008
by David A. Mellis
library modified 5 Jul 2009
by Limor Fried (http://www.ladyada.net)
example added 9 Jul 2009
by Tom Igoe
modified 22 Nov 2010
by Tom Igoe
modified 7 Nov 2016
by Arturo Guadalupi
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/LiquidCrystalHelloWorld
*/
// include the library code:
#include <LiquidCrystal.h>
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
}
透過Tinkercad進行電路實作與模擬
使用Tinkercad 完成 Arduino 與LCD1602的接線(務必參考程式碼48行所定義的方式連接),程式碼使用上方的範例程式,貼上Tinkercad的Arduino程式碼之後,即可進行線上模擬,完成後如下圖所示。

使用程式庫控制LCD顯示文字方法
透過範例程式可以了解輸出文字至LCD是一件非常簡單的事情,只要學會下方的兩個方法,基本控制就搞定了。
- 程式碼第五行 lcd.print(); 輸出文字至LCD螢幕
- 程式碼11行:lcd.setCursor(x, y); 將游標移動至x,y 的位置上,下一次輸出文字的定位座標。
void setup() {
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
// Print a message to the LCD.
lcd.print("hello, world!");
}
void loop() {
// set the cursor to column 0, line 1
// (note: line 1 is the second row, since counting begins with 0):
lcd.setCursor(0, 1);
// print the number of seconds since reset:
lcd.print(millis() / 1000);
}
小結
本章節提供LCD1602的硬體電路實作原理與軟體控制方式,下一篇可以開始開發一個簡易型的互動裝置。