[Arduino] วงจรแสดงข้อความไปยัง LCD 16x2 + L2C Module

Teerapong Singthong 👨🏻‍💻
iamgoangle
Published in
2 min readMay 19, 2019

ทำไม LCD 16 x 2 + I2C ?

ถ้าเราใช้ LCD อย่างเดียว เราต้องจั้มสายไฟเข้ากับ LCD ถึง 16 เส้น ซึ่งอาจไม่สะดวกในการต่อวงจร จึงมีคนคิด I2C Module มาให้ใช้งาน แล้วบัคกรีเข้ากับ LCD 16x2 ให้ใช้ได้สะดวก ราคาก็ไม่แพง ประมาณ 60 บาทเท่านั้น ซึ่งข้อดีนอกจากราคาถูกแล้ว เราใช้สายจั้มเพียงแค่ 4 เส้นเท่านั้น

Install Library

http://www.mathertel.de/Arduino/LiquidCrystal_PCF8574.aspx

ใช้ Library ตัวนี้ https://github.com/mathertel/LiquidCrystal_PCF8574

Circuit

ตามสเปคของ I2C จะกำหนด port ไว้ที่ <SCL:SDA> หรือ <A4:A5> ในที่นี่เราจะใช้ SCL:SDA สำหรับ Output

Code

#include <Wire.h>
#include <LiquidCrystal_PCF8574.h>
// set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_PCF8574 lcd(0x27);
void clearScreen() {
lcd.home();
lcd.clear();
}
void checkLCD() {
int error;
while (! Serial); Serial.println("Dose: check for LCD"); // See http://playground.arduino.cc/Main/I2cScanner
Wire.begin();
Wire.beginTransmission(0x27);
error = Wire.endTransmission();
Serial.print("Error: ");
Serial.print(error);
if (error == 0) {
Serial.println(": LCD found.");
} else {
Serial.println(": LCD not found.");
}
}
void setup() {
Serial.begin(9600);
checkLCD();
lcd.begin(16, 2); // initialize 16x2 pixel
}
void printHello() {
clearScreen();
lcd.setCursor(0, 0);
lcd.setBacklight(255);
lcd.display();
lcd.blink();
String name = "Hello! Golf";
lcd.print(name);
delay(3000);
}
void printMedium() {
clearScreen();
lcd.setBacklight(255);
lcd.display();
lcd.noBlink();
// 1st line
lcd.setCursor(0, 0);
String blog = "medium.com";
lcd.print(blog);
// 2nd line
lcd.setCursor(0, 1);
lcd.cursor();
String user = "/iamgoangle";
lcd.print(user);
delay(3000);
}
void loop() {
printHello();
printMedium();
}

Demo

--

--

Teerapong Singthong 👨🏻‍💻
iamgoangle

Engineering Manager, ex-Solution Engineering Lead at LINE | Tech | Team Building | System Design | Architecture | SWE | Large Scaling System