ค้นหาโค้ด

การใช้งาน LCD1602 กับอินเทอร์เฟซ I2C - บทเรียน Arduino

การใช้งาน LCD1602 กับอินเทอร์เฟซ I2C - บทเรียน Arduino

บทช่วยสอนนี้สาธิตวิธีการเชื่อมต่อ จอแสดงผล LCD1602 เข้ากับ Arduino โดยใช้ โมดูล I2C ซึ่งทำให้การเชื่อมต่อง่ายและสะอาดกว่าเมื่อเทียบกับการเดินสายแบบขนานแบบดั้งเดิม ด้วยการเชื่อมต่อเพียงสี่เส้น (VCC, GND, SDA, SCL) คุณสามารถควบคุมจอแสดงผลได้อย่างเต็มที่และแสดงข้อความหรือข้อมูลเซนเซอร์ในโปรเจกต์ Arduino ของคุณ

LCD1602-I2C display module with 4 wires

โค้ดที่จำเป็นทั้งหมด แผนผังการเดินสาย และลิงก์ดาวน์โหลดไลบรารีมีให้ด้านล่างบทความนี้

LCD1602 พร้อม I2C คืออะไร?

LCD1602 เป็นจอแสดงผลขนาด 16 ตัวอักษร 2 แถวที่นิยมใช้ในระบบฝังตัว โดยปกติแล้วต้องใช้ 6 ถึง 10 พิน ในการทำงาน แต่การเพิ่ม โมดูล I2C จะทำให้ต้องใช้ สายข้อมูลเพียงสองเส้น (SDA และ SCL) ในการสื่อสาร ซึ่งช่วยลดความยุ่งยากในการเดินสายลงอย่างมาก และทำให้พินบน Arduino ว่างมากขึ้นสำหรับอุปกรณ์อื่นๆ

การเดินสาย LCD1602 เข้ากับ Arduino

Arduino wirng for LCD1602 with I2C
Arduino wirng for LCD1602 with I2C

นี่คือวิธีการเดินสาย LCD1602 พร้อมโมดูล I2C เข้ากับ Arduino Uno:

  • VCC - 5V

  • GND - GND

  • SDA - A4

  • SCL - A5

คำอธิบาย: LCD1602 เชื่อมต่อกับ Arduino ผ่าน I2C โดยใช้สายเพียง 4 เส้น

- คำอธิบายโค้ดการแสดงข้อความบน LCD

โค้ดด้านล่างเริ่มต้นการทำงานของ LCD เปิดไฟแบ็คไลท์ และพิมพ์ข้อความในลูป

cppCopyEdit#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

// กำหนดที่อยู่ LCD เป็น 0x27 สำหรับจอแสดงผล 16 ตัวอักษร 2 บรรทัด
LiquidCrystal_I2C lcd(0x27, 16, 2);

  • Wire.h: จำเป็นสำหรับการสื่อสารแบบ I2C

  • LiquidCrystal_I2C.h: ไลบรารีสำหรับควบคุม LCD โดยใช้ I2C

  • lcd(0x27, 16, 2): เริ่มต้นการทำงานของ LCD ที่ที่อยู่ 0x27 ด้วย 16 คอลัมน์และ 2 แถว

cppCopyEditvoid setup()
{
  lcd.begin();       // เริ่มต้นการทำงานของ LCD
  lcd.backlight();   // เปิดไฟแบ็คไลท์
}

  • lcd.begin() เตรียมความพร้อมของ LCD สำหรับการใช้งาน

  • lcd.backlight() เปิดไฟแบ็คไลท์ของจอแสดงผล

cppCopyEditvoid loop()
{
  lcd.clear();                 // ล้างเนื้อหาก่อนหน้า
  lcd.print("Robojax");        // พิมพ์บนบรรทัดแรก
  lcd.setCursor(0,1);          // ย้ายเคอร์เซอร์ไปที่จุดเริ่มต้นของบรรทัดที่สอง
  lcd.print("Hello World!");   // พิมพ์บนบรรทัดที่สอง
  delay(500);                  // หน่วงเวลา 0.5 วินาที
}

  • หน้าจอจะถูกรีเฟรชทุกครึ่งวินาที

  • คุณยังสามารถแสดงข้อมูลอื่นๆ เช่น เวลาหรือค่าเซนเซอร์ได้

การติดตั้งไลบรารีที่จำเป็น

คุณต้องติดตั้งไลบรารี LiquidCrystal_I2C:

  1. เปิด Arduino IDE

  2. ไปที่ Sketch > Include Library > Manage Libraries

  3. ค้นหา LiquidCrystal_I2C

  4. คลิก Install

เมื่อติดตั้งเสร็จแล้ว คุณก็พร้อมที่จะคอมไพล์และอัปโหลดโค้ด

บทต่างๆ จากวิดีโอ

  • 00:00 - เริ่มต้น

  • 00:35 - อธิบาย LCD1602 และโมดูล I2C

  • 04:37 - อธิบายการเดินสาย

  • 05:35 - ดาวน์โหลดไลบรารี LCD1602-I2C

  • 07:13 - อธิบายโค้ดสำหรับ LCD1602

 

 

ภาพ

Arduino wirng for LCD1602 with I2C
Arduino wirng for LCD1602 with I2C
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
63-This is code for an LCD1602 display with an I2C module.
ภาษา: C++
/*
This is code for LCD1602 Display with I2C module
 * Watch the video for this code https://youtu.be/q9YC_GVHy5A
 
 * Permission granted to share this code given that this
 * note is kept with the code.
 * Disclaimer: this code is "AS IS" and for educational purposes only.
 * This library is based on work done by DFROBOT (www.dfrobot.com).
 */
/*
 *  This code has been modified from the Arduino library
 *  Updated by Ahmad Nejrabi on Jan 20, 2018 at 11:09
 *  in Ajax, Ontario, Canada
 *  for Robojax.com
 *  
 *  This is code for LCD1602 Display with I2C module
 *  which can display text on the screen.
 */
#include <Wire.h> 
#include <LiquidCrystal_I2C.h>

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

void setup()
{
  // Robojax code for LCD with I2C
	// initialize the LCD, 
	lcd.begin();
 
	// Turn on the backlight and print a message.
	lcd.backlight();
  // Robojax code for LCD with I2C


}

void loop()
{
  
  //start of loop Robojax code for LCD with I2C
  lcd.clear();
  lcd.print("Robojax");
  lcd.setCursor (0,1); // go to start of 2nd line
 lcd.print("Hello World!");
  //lcd.print(millis() / 1000);
  delay(500);
 //end of loop Robojax code for LCD with I2C
}

สิ่งที่คุณอาจต้องการ

แหล่งข้อมูลและเอกสารอ้างอิง

ไฟล์📁

ไลบรารี Arduino (zip)

ไฟล์ Fritzing