In the previous tutorial, we had learned how to use the normal LCD. However, wiring between Arduino and the normal LCD is complicated. Therefore, LCD I2C has been created to simplify the wiring. Actually, LCD I2C is composed of a normal LCD, an I2C module and a potentiometer.
Pinout
LCD I2C uses I2C interface, so it has 4 pins:
GND pin: needs to be connected to GND (0V).
VCC pin: the power supply for the LCD, needs to be connected to VCC (5V).
SDA pin: I2C data signal
SCL pin: I2C clock signal
LCD Coordinate
LCD I2C 16×2 includes 16 columns and 2 rows. the conlums and rows are indexed from 0.
Arduino Code
Quick Steps
Navigate to the Libraries icon on the left bar of the Arduino IDE.
Search “LiquidCrystal I2C”, then find the LiquidCrystal_I2C library by Frank de Brabander
Click Install button to install LiquidCrystal_I2C library.
Copy the above code and open with Arduino IDE
Click Upload button on Arduino IDE to upload code to Arduino
See the result on LCD
Try modifying text and position
C++
#include
LiquidCrystal_I2C lcd(0x20, 16, 2);
void
setup()
lcd.init();
lcd.backlight();
void
loop()
lcd.clear();
lcd.setCursor(0, 0);
lcd.print(
"Hello"
);
lcd.setCursor(0,1);
lcd.print(
"Geek"
);
delay(100);
Working of code:
Initially, the library, address, height and width are defined in the code.
#include
LiquidCrystal_I2C lcd(0x20, 16, 2);
In the setup() function, initialization and backlight are enabled.
lcd.init();lcd.backlight();
In the loop() function,
Initially clearing the existing content in the display buffer.
lcd.clear();
Cursor is set to (0,0) in the format of (row, column). whereas, indexing starts from 0.
// Set cursor (Column, Row)lcd.setCursor(0, 0);
print() function in the LCD is used to print the text where the cursor is set.
// print “Hello” at (0, 0) lcd.print(“Hello”);
Output:
Printing characters using LCD display
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
Looking for a place to share your ideas, learn, and connect? Our Community portal is just the spot! Come join us and see what all the buzz is about!
Last Updated :
20 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment…
Arduino – LCD I2C
In this Arduino LCD I2C tutorial, we will learn how to connect an LCD I2C (Liquid Crystal Display) to the Arduino board. LCDs are very popular and widely used in electronics projects for displaying information. There are many types of LCD. This tutorial takes LCD 16×2 (16 columns and 2 rows) as an example. The other LCDs are similar.
Hardware Overview
A typical I2C LCD display consists of an HD44780-based character LCD display and an I2C LCD adapter. Let’s learn more about them.
Character LCD Display
As the name suggests, these LCDs are ideal for displaying only characters. A 16×2 character LCD, for example, can display 32 ASCII characters across two rows.
If you look closely, you can see tiny rectangles for each character on the screen as well as the pixels that make up a character. Each of these rectangles is a grid of 5×8 pixels.
Please refer to our in-depth guide for more information about character LCD displays.
I2C LCD Adapter
At the heart of the adapter is an 8-bit I/O expander chip – PCF8574. This chip converts the I2C data from an Arduino into the parallel data required for an LCD display.
The board also includes a tiny trimpot for making precise adjustments to the display’s contrast.
There is a jumper on the board that provides power to the backlight. To control the intensity of the backlight, you can remove the jumper and apply external voltage to the header pin labeled ‘LED’.
C++
#include
LiquidCrystal_I2C lcd(0x20, 16, 2);
void
setup()
lcd.init();
lcd.backlight();
Step 6: Print characters in LCD.
Here is the example code to display characters in LCD display.
Arduino code:
Library Installation
Before you can proceed, you must install the LiquidCrystal_I2C library. This library allows you to control I2C displays using functions that are very similar to the LiquidCrystal library.
To install the library, navigate to Sketch > Include Library > Manage Libraries… Wait for the Library Manager to download the library index and update the list of installed libraries.
Filter your search by entering ‘liquidcrystal‘. Look for the LiquidCrystal I2C library by Marco Schwartz. Click on that entry and then choose Install.
Step 2: Connections
Refer the pictures and make the connections. It’s quite simple, you just have to plug in the I2C in the ports of the LCD and solder it into place. Then connect the SCL pin to A4 pin on the Arduino and the SDA pin to the A5 pin on the Arduino.
I’m not soldering the I2C as I have already soldered header pins on the LCD. But I would suggest soldering the I2C on the LCD.
Module I2C Arduino
LCD có quá nhiều nhiều chân gây khó khăn trong quá trình đấu nối và chiếm dụng nhiều chân trên vi điều khiển.
Module I2C LCD ra đời và giải quyết vấn để này cho bạn.
Thay vì phải mất 6 chân vi điều khiển để kết nối với LCD 16×2 (RS, EN, D7, D6, D5 và D4) thì module IC2 bạn chỉ cần tốn 2 chân (SCL, SDA) để kết nối.
Module I2C hỗ trợ các loại LCD sử dụng driver HD44780(LCD 16×2, LCD 20×4, …) và tương thích với hầu hết các vi điều khiển hiện nay.
Địa chỉ mặc định: 0X27 (có thể điều chỉnh bằng ngắn mạch chân A0/A1/A2).
Tích hợp Jump chốt để cung cấp đèn cho LCD hoặc ngắt.
Tích hợp biến trở xoay điều chỉnh độ tương phản cho LCD.
Để sử dụng màn hình LCD giao tiếp I2C sử dụng Arduino thì ta cần cài đặt thư viện Liquidcrystal_I2C. Tại đây
Lời kết
Qua bài hôm nay các bạn biết cách làm thế nào để hiển thị các ký tự và chuỗi ký tự lên LCD 16×2 và biết cách giao tiếp I2C.
Để nhận được nhiều kiến thức mới các bạn Đăng ký để nhận được thông báo sớm nhất.
Tham gia Cộng đồng Arduino KIT để cùng nhau thảo luận và chia sẽ kiến thức về lập trình Arduino.
Nếu các bạn thấy bài viết bổ ích nhớ Like và Share cho mọi người cùng đọc nhé.
Chúc các bạn thành công.
Trân trọng.
Mạch Chuyển Đổi Giao Tiếp I2C Cho LCD
LCD I2C Interface Adapter
Mã sản phẩm: BZOZ
Sản phẩm hiện đang hết hàng.
Xem chi nhánh còn hàng
Mạch chuyển đổi giao tiếp I2C cho LCD. Điện áp hoạt động: 2.5V-6V. Biến trở xoay độ tương phản cho LCD. Kích thước: 41.5mm(L)X19mm(W)X15.3MM(H)
Cộng thêm 1 điểm tích lũy
TP.HCM: Miễn phí vận chuyển đơn hàng từ 300k
Tỉnh thành khác: Miễn phí vận chuyển đơn hàng từ 500k
Xem thêm các khuyến mãi vận chuyển khác.
Giới thiệu LCD 16×2
Thông số kỹ thuật LCD 16×2
LCD 16×2 được sử dụng để hiển thị trạng thái hoặc các thông số.
LCD 16×2 có 16 chân trong đó 8 chân dữ liệu (D0 – D7) và 3 chân điều khiển (RS, RW, EN).
5 chân còn lại dùng để cấp nguồn và đèn nền cho LCD 16×2.
Các chân điều khiển giúp ta dễ dàng cấu hình LCD ở chế độ lệnh hoặc chế độ dữ liệu.
Chúng còn giúp ta cấu hình ở chế độ đọc hoặc ghi.
LCD 16×2 có thể sử dụng ở chế độ 4 bit hoặc 8 bit tùy theo ứng dụng ta đang làm.
Step 3: Code
There is an LCD I2C master library included in the Arduino IDE. But there’s a slight problem with the code in it. All the examples in this library assumes the default address of the I2C as 0x27. So first we have to find out what the address of our I2C is. We can do this by using the I2C scanner code. Once we have the I2C address we can replace this value in the example code and start using it.
I2C Scanner Code:
#include
void setup() { Wire.begin(); Serial.begin(9600); while (!Serial); // wait for serial monitor Serial.println(“\nI2C Scanner”); } void loop() { byte error, address; int nDevices; Serial.println(“Scanning…”); nDevices = 0; for(address = 1; address < 127; address++ ) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the address. Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print(“I2C device found at address 0x”); if (address<16) Serial.print(“0″); Serial.print(address,HEX); Serial.println(” !”); nDevices++; } else if (error==4) { Serial.print(“Unknown error at address 0x”); if (address<16) Serial.print(“0”); Serial.println(address,HEX); } } if (nDevices == 0) Serial.println(“No I2C devices found\n”); else Serial.println(“done\n”); delay(5000); // wait 5 seconds for next scan }
Example Code(To display characters entered in Serial Monitor):
#include
#include
LiquidCrystal_I2C lcd(0x3F,20,4); // set the LCD address to 0x27 for a 16 chars and 2 line display
void loop() { // when characters arrive over the serial port… if (Serial.available()) { // wait a bit for the entire message to arrive delay(100); // clear the screen lcd.clear(); // read all the available characters while (Serial.available() > 0) { // display each character to the LCD lcd.write(Serial.read()); } } }
Do More with LCD
Custom Character
lcd.print() function supports only ASCII characters. If you want to display a special character or symbol (e.g. heart, angry bird), you need to use the below character generator.
LCD 16×2 can display 32 characters (2 rows and 16 columns). Each character is composed of 40 pixels (8 rows and 5 columns).
The character generator represents a character (40 pixels). You just need to do the following steps:
Result on LCD:
Multiple custom characters
We can create up to 8 custom characters (indexed 0 to 7). The below example creates and displays three characters.
Result on LCD:
Summary: how to use custom character on LCD
Use the above character generator to create binary code for the custom character.
Declare the binary code for the custom character (copy from above step)
Create custom character and assign to an index value (from 0 to 7) in setup() function
Print the custom character in LCD anytime, anywhere (in setup() or loop() function)
Other functions
Add the below functions into loop() function one by one. And add delay(5000) after each function
Clear LCD screen
Move the cursor to the upper-left of the LCD
Move the cursor to the a position (column, row)
Display the LCD cursor
Hides the LCD cursor.
Display the blinking LCD cursor
Turns off the blinking LCD cursor.
And more at LiquidCrystal Library Reference
Create and Display Custom Characters
If you find the default font uninteresting, you can create your own custom characters (glyphs) and symbols. They come in handy when you need to display a character that isn’t in the standard ASCII character set.
As previously discussed in this tutorial, a character is made up of a 5×8 pixel matrix; therefore, you must define your custom character within this matrix. You can define a character by using the
createChar()
function.
To use
createChar()
, you must first create an 8-byte array. Each byte in the array corresponds to a row in a 5×8 matrix. In a byte, the digits 0 and 1 indicate which pixels in a row should be OFF and which should be ON.
All of these user-defined characters are stored in the LCD’s CGRAM.
CGROM and CGRAM
All Hitachi HD44780 driver-based LCDs have two types of memory: CGROM and CGRAM (Character Generator ROM and RAM).
CGROM is non-volatile memory that retains data even when the power is removed, whereas CGRAM is volatile memory that loses data when the power is removed.
The CGROM stores the font that appears on a character LCD. When you instruct a character LCD to display the letter ‘A’, it needs to know which pixels to turn on so that we see an ‘A’. This data is stored in the CGROM.
CGRAM is an additional memory for storing user-defined characters. This RAM is limited to 64 bytes. Therefore, for a 5×8 pixel LCD, only 8 user-defined characters can be stored in CGRAM, whereas for a 5×10 pixel LCD, only 4 can be stored.
Custom Character Generator
Creating custom characters has never been easier! We’ve developed a small application called Custom Character Generator. Can you see the blue grid below? You can click on any pixel to set or clear that pixel. And as you click, the code for the character is generated next to the grid. This code can be used directly in your Arduino sketch.
There’s no limit to what you can create. The only limitation is that the LiquidCrystal_I2C library only supports eight custom characters. But don’t be sad, look at the bright side; at least we have eight characters.
Arduino Example Code
The sketch below demonstrates how to display custom characters on the LCD.
After including the library and creating the LCD object, custom character arrays are defined. The array consists of 8 bytes, with each byte representing a row in a 5×8 matrix.
This sketch contains eight custom-characters. Take, for example, the
Heart[8]
array. You can see that the bits (0s and 1s) are forming the shape of a heart. 0 turns the pixel off, and 1 turns it on.
function to create a custom character. This function accepts two parameters: a number between 0 and 7 to reserve one of the eight supported custom characters, and the name of the array.
lcd.createChar(0, Heart);
In the loop, to display the custom character, we simply call the
write()
function and pass it the number of the character we reserved earlier.
lcd.setCursor(0, 1); lcd.write(0);
Tổng tiền thanh toán:
Code test cơ bản
#include
void loop()
Code Đếm số 0 đến 9 LCD 16×2 I2C
#include
void loop()
Code test cơ bản
#include
void loop()
Code Đếm số 0 đến 9 LCD 16×2 I2C
#include
void loop()
Hardware components
Software apps and online services
Hello friends welcome back to Techno-E-solution, In previous video we see how to interface LCD 16×2 to Arduino Uno, but there are very complicated circuits, so in this tutorial, I’ll show you how to reduce circuitry by using I2C module which is very compact & easy to connection. Simply connect I2C module with LCD parallel & connect I2C modules 4 pins to Arduino. I2C module has 4 output pins which contains VCC, GND, SDA, SCL where 5V supply gives to I2C module through VCC & GND to GND of Arduino. SDA is a data pin & SCL is clock pin of I2C module. To interface LCD and I2C with Arduino we need Liquid Crystal I2C Library in Arduino IDE software.
In my upcoming projects we need LCD in project & to avoid complicated circuit I’ll use I2C module. So let’s get started…..
If you like this project subscribe us on Youtube, So without wasting time Let’s get started…………..
INSTALL ARDUINO LIQUIDCRYSTAL I2C LIBRARY IN ARDUINO IDE
To make this project we need Arduino Liquidcrystal library in Arduino IDE. Follow following steps to add this library in Arduino IDE software.
Open Arduino IDE Software.
Select Sketch > Include Library > Add. Zip library.
New dialog box will open.
Select Zip file. (Download Zip File)
Click on Open Button.
Now Arduino Liquid Crystal Library is successfully Installed in your Arduino IDE.
————————————————————————————————–
NextDFM Software From NextPCB
2 / 4
A PCB Design Problems Detector, An Engineering Solution ProviderImport the Gerber file with one click. No need for complicated file reading steps to review easily and improve efficiency.
Help you quickly familiarize DFM design specifications and production needs to determine whether there are any manufacturing constraints
1 / 3
Features
Make PCB design more standard Prevent the quality flaw
Impedance calculation function and lamination automation
Automatically generate the best puzzle
CAM350 free alternative version
Check Gerber files anytime, anywhere and parse it with one click
Instate Quote and evaluate delivery time Reduce cost and improve benefit
these are advantage comparing to Eagle and Altium
Hi!Please, someone can explain exactly to each of these pins of the display I2C (GND, VCC, SDA, SCL) which corresponds to the pin of old LCD (DB4, DB5, DB6, DB7, E, RW, RS, V0, VDD, VSS )?
THANK YOU!
Hi!Please, someone can explain exactly to each of these pins of the display I2C (GND, VCC, SDA, SCL) which corresponds to the pin of old LCD (DB4, DB5, DB6, DB7, E, RW, RS, V0, VDD, VSS )?
THANK YOU!
There is a big difference. Simply, no one of signals is corresponding (except of VCC, GND and VDD, VSS – power supply, but it is no signal in fact). Standard LCD has an parallel data bus and I2C is serial data bus. You cannot interconnect these directly.
LCD:DB4-DB7 ~ four high bidirectional data linesE ~ enable to read/writeRW ~ read/write modeRS ~ register selectionVO ~ LCD operating voltage
I2C:SCL – clock signalSDA – serial data
Additional module is needed for non i2C LCD to I2C adaptation e.g. based on old good PCF8574 chip.
thanks for the reply! I’ll explain my problem: I would like to do this project:http://www.instructables.com/file/FAZQGW7HOW0HQ7S (or http://www.instructables.com/file/FVBC6TYHOW0HQ7H)but I have a display with I2C … how can I do?Which PIN I have to connect and which are not? Where?Can you help me?
You’ve post wrong URL. Couldn’t be found.
If you need to connect standard LCD via I2C you have to use additional module which converts signals as I wrote already.Try to google and you will obtain lots of results for such module.
… but I have a display with I2C … how can I do?
Which PIN I have to connect and which are not? Where? …
Most likely your I2C display is actually a standard parallel device that has a separate I2C adapter board. You could unsolder the adapter board (not a trivial task) and just use the display board by itself but it would be far easier to just buy a new display.
Don
this is working URL
I have a display with I2C … how can I do?
You have to modify the instructables code to use the i2c display instead of the parallel display with a change of library and constructor. All the lcd.print() and cursor instructions in the code should work with either display.
If currently don’t know how to print to the i2c display then you need to begin the learning process of how to do so.
There is a Forum Search box at the top right of this webpage. Some key terms will be “i2c display” “fmalpartida i2c library” and “Bill Perry i2cLCD guesser”.
Here’s a useful link [Tutorial] How to use an unknow I2C LCD library – Programming Questions – Arduino Forum
You can use also I2C display for this project. Arduino has I2C (SCL, SDA). In this case you can connect display to these pins and in the program you have to modify LCD to I2C version. It uses LiquidCrystal library. So simple modification should work.
EDIT: cattledog was little bit faster. I think, fmalpartida’s library for LCD is pretty good and complex. I am recommending this.https://bitbucket.org/fmalpartida/new-liquidcrystal/downloads
Thanks! The Library had already seen something and how to changeBut specifically I would know exactly which pins connect to bradbord and which to discard (like the link that I post) and where connect them.
Discard DB4-7, RW, E, RS and you have to connect SCL from LCD to SCL on Arduino and SDA to SDA.
Thank you all! It seems to work!
I2C LCD
I2C_LCD is an easy-to-use display module, It can make display easier. Using it can reduce the difficulty of make, so that makers can focus on the core of the work.
We developed the Arduino library for I2C_LCD, user just need a few lines of the code can achieve complex graphics and text display features. It can replace the serial monitor of Arduino in some place, you can get running informations without a computer.
More than that, we also develop the dedicated picture data convert software (bitmap converter)now is available to support PC platform of windows, Linux, Mac OS. Through the bitmap convert software you can get your favorite picture displayed on I2C_LCD, without the need for complex programming.
I2C_LCD can provide you with a very convenient way of make. Enjoy yourself!
###Product Version
Version
How to buy
I2C_LCD (With universal Grove cable)
I2C_LCD (With conversion Grove cable)
##Features
Only 2 Arduino pins are occupied (Use I2C interface).
Supports standard I2C mode (100Kbit/s) and fast I2C mode (400Kbit/s).
Compatible with multiple communication logic levels: 2.8~5VDC.
Arduino library supported, use a line of code to complete the display.
Integrate 7 sizes of ASCll fonts, 5 graphics functions.
Provide dedicated picture data convert software (Bitmap Converter).
Most of the complex operation is processed by I2C_LCD independent controller, saving user controller resources.
Supports cursor function, can set up 16 cursor flicker frequency.
Step 1: Install the latest version of Arduino IDE to your computer.
Step 2: Download and install the I2C_LCD library to Arduino IDE:
Open Arduino IDE, click Sketch -> Include library -> Add .ZIP library.
Find and select the I2C_LCD.zip file you just downloaded.
Restart the Arduino IDE.
Step 3: Chose the example project which you like. (Take “HelloWorld” project for example here.)
Click File > Examples > I2C_LCD > HelloWorld
Step 4: Connect I2C_LCD to your Seeeduino Vx board with Grove cable. Then connect Seeeduino Vx board to your computer.
Step 5: Select your board and serial port.
Select the board: Click Tools > Board > “Arduino Duemilanove or Diecimila”(Seeeduino V3.0 Or early version), “Arduino Uno”(Seeeduino Lotus or Seeeduino V4.0).
Select the COM: Click Tools -> Serial Port -> COMX(which connected with your board.)
Step 6: Upload the program and enjoy yourself!
PS:
For more details about library install please refer to https://arduino.cc/en/Guide/Liaries.
If you encounter other problems during the use, please refer to the User Manual for help. If you can’t solve it, please contact us.
Technical support: [email protected]
##Version Tracker I2C_LCD Hardware:
Revision
Release Note
Release Date
I2C_LCD_v1.2
1. Add production test point.
Sep 18, 2015
I2C_LCD_v1.1
1. Modify the logo position. 2. Modify the board shape. 3. Modify the button position.
May 8, 2014
I2C_LCD_v1.0
1. Modify the power circuit wiring.
Mar 1, 2014
I2C_LCD_v0.9b
1. Initial public release.
Feb 15, 2014
I2C_LCD Library:
Revision
Release Note
Release Date
I2C_LCD_v1.21
Support LinkIt boards.
Aug 21, 2016
I2C_LCD_v1.20
Reorganize I2C_LCD driver interface, make it easy to transplant to any other boards.
Apr 16, 2016
I2C_LCD_v1.12
Modify the bug that can’t display bitmap when y coordinate is more than 16.
The repository of Arduino library hosted here, if you have any good idea of the code, you can pull to us.
I2C_LCD Library
I2C_LCD User Manual EN
I2C_LCD User Manual 中文
BitmapConverter(ForWindows)
BitmapConverter(ForMacOS)
BitmapConverter(ForLinux)
I2C_LCD_SourceFile
Adjusting The LCD Contrast
After wiring the LCD, you will need to adjust the contrast of the LCD. On the I2C module, there is a potentiometer that can be rotated with a small screwdriver.
Now, turn on the Arduino. You will see the backlight light up. As you turn the potentiometer knob, the first row of rectangles will appear. If you have made it this far, Congratulations! Your LCD is functioning properly.
Step 4: The Output
Once you have uploaded the code, you are ready to go. For this Instructable I have taken the example of the Serial Print Code. So now after uploading the code, open the Serial Monitor and type a word and click “send”. Now you should see this value getting displayed on the LCD.
That’s All Folks !! Stay Tuned For More !!
Participated in the Makerspace Contest 2017
In this article, we will learn how to interface LCD displays with Arduino Uno R3.
Arduino is an open-source electronics platform. It consists ATmega328P 8-bit Microcontroller. It can be able to read inputs from different sensors & we can send instructions to the microcontroller in the Arduino. It provides Arduino IDE to write code & connect the hardware devices like Arduino boards & sensors.
LCD Display:
LCD stands for Liquid Crystal Display. LCD is a flat-paneled display. It uses liquid crystals combined with polarized to display the content. LCD uses the light modulation property of LCD. LCD is available both in Monochrome and Multicolor. It cannot emit light directly without a backlight. In some LCDs, It displays the content only with the help of a backlight in a dark place.
I2C communication:
I2C or IIC stands for Inter-Integrated Communication. I2C is a serial communication interface to communicate with other I2C devices. I2C uses multi-master / multi slave method. I2C uses 2 lines named SCL and SDA for transmission/reception and another 2 lines for power supply and ground. Each and every I2C device has I2C address to identify. I2C addresses of multiple devices may have the same address. The address is in the format of “0x20” (Example address). Steps to find out I2C address device is discussed in the following (step 4).
The serial Clock (SCL) pin is to synchronize the transmitter and receiver.
Serial Data (SDA) pin is to transfer data.
I2C LCD:
I2C LCD uses I2C communication interface to transfer the information required to display the content. I2C LCD requires only 2 lines (SDA and SCL) for transferring the data. So, the complexity of the circuit is reduced.
Interfacing I2C LCD to the Arduino:
I2C LCD can be connected to the Arduino directly with SDA pin to SDA pin and SCL pin to SCL pin as per the below circuit diagram. I2C LCD requires additional library to be installed. The next step is to connect the LCD to the address of the device using the following code. Those steps are explained in detail below.
Components required:
Arduino Uno R3
I2C LCD display
Jumper Wires
Steps to interface LCD display with Arduino:
Step 1: Install the library for LCD display in Arduino IDE.
Open Arduino IDE and navigate to Tools>Library Manager.
Search for “LiquidCrystal I2C” and install the “LiquidCrystal I2C” library in the Arduino IDE.
Library Manager
Step 2: Import “LiquidCrystal_I2C.h” header file in the code.
Define header file in the code ” #include
“.
Step 3: Connect display device to Arduino.
Connect the SDA pin of an LCD display to the SDA pin of the Arduino.
Connect the SCL pin of an LCD display to the SCL of the Arduino.
Connect VCC to 5V pin
Connect GND to GND pin.
LCD display interfacing circuit
Step 4: Find the I2C Address of the display device.
Compile and run the below code to find the I2C Address.
Before running this try step 5 using most commonly used addresses “0x27” or “0x3F“. If those are not working, then continue with step 4.
Online simulation of I2C address finding using Tinkercad: https://www.tinkercad.com/things/0siOxvpmVNJ
Arduino code for I2C Address finding:
Introduction: Use 16×2 LCD With I2C
In one of my previous Instructable, I showed you how to connect and interface an LCD to Arduino Uno and display values on it. But as observed, there were a lot of connections and if the project started getting a lot messy due to too many wires.
In this Instructable, I’ll show you how to connect an LCD to an I2C, which will have only 4 pins to control and use the LCD. So let’s get started.
Wiring an I2C LCD Display to an Arduino
Connecting an I2C LCD is much simpler than connecting a standard LCD. You only need to connect four pins.
Begin by connecting the VCC pin to the Arduino’s 5V output and the GND pin to ground.
Now we are left with the pins that are used for I2C communication. Note that each Arduino board has different I2C pins that must be connected correctly. On Arduino boards with the R3 layout, the SDA (data line) and SCL (clock line) are on the pin headers close to the AREF pin. They are also referred to as A5 (SCL) and A4 (SDA).
The following table lists the pin connections:
I2C LCD
Arduino
VCC
5V
GND
GND
SCL
SCL or A5
SDA
SDA or A4
The diagram below shows how to connect everything.
Hardware Required
Arduino UNO or Genuino UNO
USB 2.0 cable type A/B
LCD I2C
Jumper Wires
(Optional) 9V Power Adapter for Arduino
(Recommended) Screw Terminal Block Shield for Arduino Uno
(Optional) Transparent Acrylic Enclosure For Arduino Uno
Or you can buy the following sensor kit:
DIYables Sensor Kit 30 types, 69 units
Determining the I2C Address
As previously stated, the I2C address of your LCD depends on the manufacturer. If your LCD has a PCF8574 chip from Texas Instruments, its I2C address is 0x27; if it has a PCF8574 chip from NXP Semiconductors, its I2C address is 0x3F.
If you’re not sure what your LCD’s I2C address is, you can run a simple I2C scanner sketch that scans your I2C bus and returns the address of each I2C device it finds.
You can find this sketch under File > Examples > Wire > i2c_scanner.
Load the i2c_scanner sketch into your Arduino IDE.
#include
void setup() { Wire.begin(); Serial.begin(9600); while (!Serial); // Leonardo: wait for serial monitor Serial.println("\nI2C Scanner"); } void loop() { int nDevices = 0; Serial.println("Scanning..."); for (byte address = 1; address < 127; ++address) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the address. Wire.beginTransmission(address); byte error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address < 16) { Serial.print("0"); } Serial.print(address, HEX); Serial.println(" !"); ++nDevices; } else if (error == 4) { Serial.print("Unknown error at address 0x"); if (address < 16) { Serial.print("0"); } Serial.println(address, HEX); } } if (nDevices == 0) { Serial.println("No I2C devices found\n"); } else { Serial.println("done\n"); } delay(5000); // Wait 5 seconds for next scan }
After you’ve uploaded the sketch, launch the serial monitor at 9600 baud. You should see the I2C address of your I2C LCD display.
Please make a note of this address. You’ll need it in later examples.
Chi tiết sản phẩm
Để sử dụng các loại LCD có driver là HD44780 (LCD 1602, LCD 2004,… ) cần có ít nhất 6 chân của MCU kết nối với các chân RS, EN, D7, D6, D5 và D4 để có thể giao tiếp với LCD.
Nhưng với mạch chuyển đổi giao tiếp I2C cho LCD, các bạn chỉ cần 2 chân (SDA và SCL) của MCU kết nối với 2 chân (SDA và SCL) của module là đã có thể hiển thị thông tin lên LCD. Ngoài ra có thể điều chỉnh được độ tương phản bởi biến trở gắn trên module.
THÔNG SỐ MẠCH CHUYỂN ĐỔI GIAO TIẾP I2C
Kích thước: 41.5mm(L)X19mm(W)X15.3MM(H)
Trọng lượng: 5g
Điện áp hoạt động: 2.5v-6v
Jump chốt: Cung cấp đèn cho LCD hoặc ngắt
Biến trở xoay độ tương phản cho LCD
Lưu ý : Các phiên bản cũ địa chỉ của bus i2c là 0X27, loại mới là 0x3F
Module được thiết kế dễ dàng cắm vào màn hình LCD theo các chân định sẵn, xem LCD 1602 để biết cách kết nối với Arduino
Sơ đồ chân:
Sơ đồ kết nối:
————————CODE THAM KHẢO———————
/*************************************************** Kết nối: I2C UNO R3 MEGA VIN 5V 5V GND GND GND SCL A5 SCL SDA A4 SDA Nạp code mở Serial Monitor chọn No line ending, baud 9600. ****************************************************/ #include
//————————————————————————— void setup() { Wire.begin(); Serial.begin(9600); while (!Serial); // Leonardo: wait for serial monitor Serial.println(” I2C Scanner”); } //————————————————————————– void loop() { byte error, address; int nDevices; Serial.println(“Scanning…”); nDevices = 0; for(address = 1; address < 127; address++ ) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the address. Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print(“I2C device found at address 0x”); if (address<16) Serial.print(“0″); Serial.print(address,HEX); Serial.println(” !”); nDevices++; } else if (error==4) { Serial.print(“Unknown error at address 0x”); if (address<16) Serial.print(“0”); Serial.println(address,HEX); } } if (nDevices == 0) Serial.println(“No I2C devices found “); else Serial.println(“done “); delay(5000); // wait 5 seconds for n }
Kết quả
Hình ảnh sản phẩm
Nshopvn.com · 07/03/2019 10:43 AM
Mạch Chuyển Đổi Giao Tiếp I2C Cho LCD giá chỉ 18.000₫
If you’ve ever attempted to connect an LCD display to an Arduino, you’ve probably noticed that it uses a lot of Arduino pins. Even in 4-bit mode, the Arduino requires seven connections – half of the Arduino’s available digital I/O pins.
The solution is to use an I2C LCD display. It only uses two I/O pins that are not even part of the digital I/O pin set and can be shared with other I2C devices.
Wiring Diagram
This image is created using Fritzing. Click to enlarge image
LCD I2C
Arduino Uno, Nano
Arduino Mega
Vin
5V
5V
GND
GND
GND
SDA
A4
20
SCL
A5
21
C++
#include
void
setup()
Wire.begin();
Serial.begin(9600);
void
loop()
byte err, addr;
int
devices = 0;
for
(addr = 1; addr < 127; addr++)
Wire.beginTransmission(addr);
err = Wire.endTransmission();
if
(!err)
Serial.print(
"Address 0x"
);
if
(addr < 16)
Serial.print(
"0"
);
Serial.println(addr, HEX);
devices++;
else
if
(err == 4)
Serial.print(
"Error at address 0x"
);
if
(addr < 16)
Serial.print(
"0"
);
Serial.println(addr, HEX);
if
(!devices)
Serial.println(
"Please connect your I2C device"
);
delay(2000);
Note: Make sure that you have connected the device properly.
Output:
I2C Address finding
Step 5: Define display device.
Define the display device in the code as the following example code.
Replace the address with your device address.
How To Program For LCD I2C
Thanks to the LiquidCrystal_I2C library, the using LCD is a piece of cake.
Include the library:
Declare a LiquidCrystal_I2C object with I2C address, the number of columns, the number of rows:
Initialize the LCD.
Move cursor to the desired position (column_index, row_index)
Print a message to the LCD.
There are many things more that we can do with LCD (see Do More with LCD part)
※ NOTE THAT:
The I2C address of LCD can vary according to the manufacturers. In the code, we used 0x27 that is specified by DIYables manufacturer
Troubleshooting on LCD I2C
If the text is not displayed on LCD I2C, please check the following issues:
Adjust the brightness of LCD by rotating potentiometer in the backside of LCD
Depending on manufacturers, the I2C address of LCD may be different. Usually, the default I2C address of LCD is 0x27 or 0x3F. Try these values one by one. If you still failed, run the below code to find the I2C address.
The result on Serial Monitor:
I2C Address of LCD
If you have multiple devices on the same I2C bus, you may need to set a different I2C address for the LCD adapter to avoid conflicting with another I2C device.
For this purpose, the adapter comes with three solder jumpers/pads (A0, A1, and A2). The address is set when a jumper is shorted with a blob of solder.
An important point to note here is that several companies, including Texas Instruments and NXP Semiconductors, manufacture the same PCF8574 chip. And the I2C address of your LCD depends on the chip manufacturer.
If your LCD has Texas Instruments’ PCF8574 chip:
According to the Texas Instruments’ datasheet, the three address selection bits (A0, A1, and A2) are located at the end of the 7-bit I2C address register.
Because there are three address inputs that can take on two states, either HIGH or LOW, eight (2^3) different combinations (addresses) are possible.
All three address inputs are pulled HIGH using onboard pullups. This gives the PCF8574 a default I2C address of 0x27.
When you short a solder jumper, you pull that address input LOW. If you were to short all three jumpers, the address would be 0x20. So the range of all possible addresses spans from 0x20 to 0x27.
You can set a different I2C address, according to the table below.
If your LCD has NXP’s PCF8574 chip:
According to the NXP Semiconductors’ datasheet, the three address selection bits (A0, A1, and A2) are located at the end of the 7-bit I2C address register. However, the remaining bits in the address register are different.
Because there are three address inputs that can take on two states, either HIGH or LOW, eight (2^3) different combinations (addresses) are possible.
All three address inputs are pulled HIGH using onboard pullups. This gives the PCF8574 a default I2C address of 0x3F.
When you short a solder jumper, you pull that address input LOW. If you were to short all three jumpers, the address would be 0x38. So the range of all possible addresses spans from 0x38 to 0x3F.
You can set a different I2C address, according to the table below.
So the I2C address of your LCD is most likely 0x27 or 0x3F. If you’re not sure what your LCD’s I2C address is, there’s an easy way to figure it out. You’ll learn about that later in this tutorial.
Đoạn code này thì tương tự như trên, xuất ra dòng chữ “Xin chao cac ban” tại vị trí con trỏ ở hàng 2, cột 1.
Bây giờ thì các bạn upload chương trình và xem kết quả nhé.
Other useful functions of the LiquidCrystal_I2C Library
There are many useful functions you can use with LiquidCrystal_I2C Object. Some of them are listed below:
lcd.home()
function positions the cursor in the upper-left of the LCD without clearing the display.
lcd.blink()
function displays a blinking block of 5×8 pixels at the position to which the next character will be written.
lcd.noBlink()
function turns off the blinking LCD cursor.
lcd.cursor()
function displays an underscore (line) at the position to which the next character will be written.
lcd.noCursor()
function hides the LCD cursor.
lcd.scrollDisplayRight()
function scrolls the contents of the display one space to the right. If you want the text to scroll continuously, you have to use this function inside a
for
loop.
lcd.scrollDisplayLeft()
function scrolls the contents of the display one space to the left. Similar to the above function, use this inside a
for
loop for continuous scrolling.
lcd.noDisplay()
function turns off the LCD display, without losing the text currently shown on it.
lcd.display()
function turns on the LCD display, after it’s been turned off with
noDisplay()
. This will restore the text (and cursor) that was on the display.
Các lỗi thường gặp khi sử dụng I2C LCD
Hiển thị một dãy ô vuông.
Màn hình chỉ in ra một ký tự đầu.
Màn hình nhấp nháy.
Các lỗi này chủ yếu là do sai địa chỉ bus, để fix lỗi các bạn thay địa chỉ mặc định là “0x27” thành “0x3F.
Trong trường hợp vẫn không được các bạn fix lỗi bằng cách nạp code tìm địa chỉ bus của I2C.
Sau khi tìm xong các bạn thay địa chỉ vừa tìm được vào vị trí “0x27” là xong.
Các bạn có thể tải code tìm địa chỉ bus ở đây. Tải ngay.
Basic Arduino Sketch – Hello World
The test sketch below will print ‘Hello World!’ on the first line of the LCD and ‘LCD Tutorial’ on the second.
However, before you upload the sketch, you must make a minor change to make it work for you. You must pass the I2C address of your LCD as well as the display dimensions to the LiquidCrystal_I2C constructor. If you’re using a 16×2 character LCD, pass 16 and 2; if you’re using a 20×4 character LCD, pass 20 and 4.
// enter the I2C address and the dimensions of your LCD here LiquidCrystal_I2C lcd(0x3F, 16, 2);
Once you are done, go ahead and try the sketch.
#include
LiquidCrystal_I2C lcd(0x3F,16,2); // set the LCD address to 0x3F for a 16 chars and 2 line display void setup() { lcd.init(); lcd.clear(); lcd.backlight(); // Make sure backlight is on // Print a message on both lines of the LCD. lcd.setCursor(2,0); //Set cursor to character 2 on line 0 lcd.print("Hello world!"); lcd.setCursor(2,1); //Move cursor to character 2 on line 1 lcd.print("LCD Tutorial"); } void loop() { }
This is what you should see on the screen.
Code Explanation:
The sketch begins by including the LiquidCrystal_I2C library.
#include
The next step is to create an object of LiquidCrystal_I2C class. The LiquidCrystal_I2C constructor accepts three inputs: I2C address, number of columns, and number of rows of the display.
LiquidCrystal_I2C lcd(0x3F,16,2);
In the setup, three functions are called. The first function is
init()
. It initializes the interface to the LCD. The second function is
clear()
. This function clears the LCD screen and positions the cursor in the upper-left corner. The third function,
backlight()
, turns on the LCD backlight.
lcd.init(); lcd.clear(); lcd.backlight();
The function
setCursor(2, 0)
is then called to move the cursor to the third column of the first row. The cursor position specifies where you want the new text to appear on the LCD. It is assumed that the upper left corner is
col=0
and
row=0
.
lcd.setCursor(2,0);
Next, the
print()
function is used to print “Hello world!” to the LCD.
lcd.print("Hello world!");
Similarly, the next two lines of code move the cursor to the third column of the second row and print ‘LCD Tutorial’ to the LCD.
lcd.setCursor(2,1); lcd.print("LCD Tutorial");
Giao tiếp I2C LCD Arduino
Module I2C LCD 16×2
Arduino UNO
GND
GND
VCC
5V
SDA
A4/SDA
SCL
A5/SCL
Sơ đồ đấu nối
Các linh kiện cần thiết cho dự án:
Tên linh kiện
Số lượng
Shopee
Arduino UNO R3
Mua ngay
Dây cáp nạp
Mua ngay
Màn hình LCD 16×2
Mua ngay
Module I2C LCD 16×2
Mua ngay
Dây cắm (Đực – Cái)
Mua ngay
Bạn sẽ học được gì
Có kiến thức cơ bản về Robotics
Chế tạo Robot dò đường thông minh
Đánh thức nhà khoa học bên trong bạn
Tìm hiểu thêm về Robotics, các thuật toán Robot tự động
Kiến thức nền tảng để chế tạo các máy móc tự động phục vụ đời sống sinh hoạt, lao động sản xuất
Kiến thức để chế tạo sản phẩm, tham gia các cuộc thi khoa học công nghệ trong nước và quốc tế