LCD顯示屏模塊
LCD顯示屏與Arduino連接可以有兩種方式:
- 直接與Arduino相連
- 通過轉(zhuǎn)接板利用I2C的方式與Arduino相連
1. 直接與Arduino相連
直接與Arduino相連的好處是不用現(xiàn)另外購買轉(zhuǎn)接板,但這樣造成的后果就是要大量占用Arduino的IO口。如果你的項(xiàng)目外接的傳感器不多,那還好,但如果你需要外接很多個(gè)傳感器或者其他配件,那你的IO口就會(huì)告急了~
所需材料
- 1x Arduino UNO
- 1x LCD 16x2
- 1x 10KΩ旋轉(zhuǎn)變阻器
- 1x 面包板
接線示意圖

LCD直接與Arduino相連的接線圖
| LCD | Arduino | |
|---|---|---|
| RS | -> | 12 |
| E | -> | 11 |
| D4 | -> | 5 |
| D5 | -> | 4 |
| D6 | -> | 3 |
| D7 | -> | 2 |
| VCC | -> | 5V |
| R/W | -> | GND |
| GND | -> | GND |
| LCD | 旋轉(zhuǎn)變阻器 | |
|---|---|---|
| VCC | -> | 左邊引腳 |
| Vo | -> | 中間引腳 |
| R/W | -> | 右邊引腳 |
| GND | -> | 右邊引腳 |
加載庫文件
- 在Arduino IDE 1.6.2 或者以上版本中,
項(xiàng)目->加載庫->管理庫中搜索LiquidCrystal,然后安裝即可。
示例代碼
// include the library code:
#include <LiquidCrystal.h>
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
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);
}
參考文獻(xiàn)
https://www.arduino.cc/en/Reference/LiquidCrystal
2.通過PCF8574T轉(zhuǎn)接板與Arduino相連
通過此種方式,可以大大節(jié)省Arduino的IO口,前提是你還得購買一塊PCF8574T轉(zhuǎn)接板。

LCD顯示屏與轉(zhuǎn)接板
所需要材料
- 1x Arduino UNO
- 1x LCD 16x2
- 1x PCF8574T轉(zhuǎn)接板
- 電烙鐵、焊錫、松香等
接線
首先,把轉(zhuǎn)接板焊接到LCD顯示屏上(方向如上圖)
| PCF8574T | Arduino | |
|---|---|---|
| GND | -> | GND |
| VCC | -> | 5V |
| SDA | -> | A4 |
| SCL | -> | A5 |
如果你的A4,A5口已經(jīng)被占用,那么你還可以接到Arduino最上面的兩個(gè)沒有標(biāo)文字的IO口,即D0-D13那一排最上面的那兩個(gè)口
- SCL -> 最上面的口
- SDA -> 第二個(gè)口
掃描I2C地址
將以下代碼拷貝到Arduino IDE,并執(zhí)行。然后選擇工具->串口監(jiān)視器,把右下角的波特率改為115200,即可讀出I2C地址,如下圖。
// I2C Scanner
// Written by Nick Gammon
// Date: 20th April 2011
#include <Wire.h>
void setup() {
Serial.begin (115200); // Leonardo: wait for serial port to connect
while (!Serial) { }
Serial.println ();
Serial.println ("I2C scanner. Scanning ...");
byte count = 0;
Wire.begin();
for (byte i = 8; i < 120; i++) {
Wire.beginTransmission (i);
if (Wire.endTransmission () == 0) {
Serial.print ("Found address: ");
Serial.print (i, DEC);
Serial.print (" (0x");
Serial.print (i, HEX);
Serial.println (")");
count++;
delay (1); // maybe unneeded?
} // end of good response
} // end of for loop
Serial.println ("Done.");
Serial.print ("Found ");
Serial.print (count, DEC);
Serial.println (" device(s).");
} // end of setup
void loop() {}

串口監(jiān)視器中顯示的地址:0x3F
加載庫文件
到這里下載最新的New LiquidCrystal,手動(dòng)添加到你的 Arduino IDE中。(ps:記得修改你的I2C地址,把LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7);的0x3F改為你的真實(shí)地址)
示例代碼
/* Demonstration sketch for PCF8574T I2C LCD Backpack Uses library from https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads GNU General Public License, version 3 (GPL-3.0) */
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x3F,2,1,0,4,5,6,7); // 0x27 is the I2C bus address for an unmodified backpack
void setup() { // activate LCD module
lcd.begin (16,2); // for 16 x 2 LCD module
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH);
}
void loop() {
lcd.home (); // set cursor to 0,0
lcd.print(" tronixlabs.com");
lcd.setCursor (0,1); // go to start of 2nd line
lcd.print(millis());
delay(1000);
lcd.setBacklight(LOW); // Backlight off delay(250);
lcd.setBacklight(HIGH); // Backlight on delay(1000);
}
參考文獻(xiàn)
http://www.instructables.com/id/Using-PCF8574-backpacks-with-LCD-modules-and-Ardui/?ALLSTEPS
經(jīng)驗(yàn)教訓(xùn)
- 之前沒有查看I2C的地址,傻傻的復(fù)制別人的代碼過來直接用,結(jié)果人家是0x27,試了好久都不行,后來才看到一篇文章說要先確定I2C的地址,才得以解決。
- 有個(gè)困惑,之前確定I2C的地址后,用
NewliquidCrystal_1.3.4.zip這個(gè)庫死活還是不行,然后我把這個(gè)庫刪掉,然后換了個(gè)庫還是不行,最后又換回NewliquidCrystal_1.3.4.zip居然可以了。。。有大神知道是什么原因嗎。。。