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.
But there are boxes on top row of LCD. No text. (Attachment 1)
My sketch is:
#include
#include
void setup()
{
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.print(“Hello, world!”);
}
void loop()
{
}
How can I solve this problem? I tried many libraries but no luck. When I connect LCD direct to Arduino without I2C module, result is same. But I am sure that LCD is not corrupted because I have 2 more LCD’s, result is same with them, too.
Most I2C libraries are written to support a specific I2C adapter since they have to account for the actual layout of the connections on that adapter.
Another choice is to use a library, such as the one by Francisco Malpartida (fm), that has provisions to deal with many I2C adapters since the layout of the connections can be specified in the ‘constructor’.
Here is a blog that seems to use your I2C adapter with fm’s library. I suggest that you modify his example program to just display a simple message in setup() and keep loop() blank as you did in the program that you posted above.
Make sure that you follow the instructions for installing that library – specifically you must remove all traces of the I2C libraries that you have previously tried and you must also remove or rename the default LiquidCrystal library.
#include
#include
#include
void setup()
{
lcd.setBacklightPin(3,POSITIVE);
lcd.setBacklight(HIGH); // NOTE: You can turn the backlight off by setting it to LOW instead of HIGH
lcd.begin(16, 2);
lcd.clear();
lcd.print(“Hello”);
}
void loop()
{
}
I use lastest version now. And i followed intallation instructions carefully.
When I use this sample sketch (HelloWorld_I2C)
#include
#include
LiquidCrystal_I2C lcd(0x27); // Set the LCD I2C address
//LiquidCrystal_I2C lcd(0x38, BACKLIGHT_PIN, POSITIVE); // Set the LCD I2C address
void setup()
{
// Switch on the backlight
pinMode ( BACKLIGHT_PIN, OUTPUT );
digitalWrite ( BACKLIGHT_PIN, HIGH );
lcd.begin(16,2); // initialize the lcd
lcd.home (); // go home
lcd.print(“Hello, ARDUINO “);
lcd.setCursor ( 0, 1 ); // go to the next line
lcd.print (” FORUM – fm “);
delay ( 1000 );
}
void loop()
{
}
Backlight gone and no text, too.
I cant understand what is #define BACKLIGHT_PIN 13
I’m also having issues with my qapass 16×2 LCD that came with serial module presolidered on to the LCD. I noticed 3 sets of pads A0 to A2. Could these be the same links I see on other boards that have pads marked bs0 -bs2. The picture of this is here Setting up wide.hk OLED I2C display – Displays – Arduino Forum
Step 3: Code
We must require to include two libraries ,in order to work the code attached .
Download the libraries from the attachment LCD library .
Basic functions we use in code
lcd.begin(16,2); //Defining 16 columns and 2 rows of lcd display
lcd.backlight(); //To Power ON /OFF the back light
lcd.setCursor(0,0); //Defining positon to write from first row,first column .
lcd.setCursor(0,1); //Defining positon to write from second row,first column .
lcd.print(” write here to print”); //You can write 16 Characters per line within quotations.
lcd.clear(); //Clean the screen
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.
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.
#includeLiquidCrystal_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");
Introduction: How to Connect I2C Lcd Display to Arduino Uno
Hello Guys , In this Instructable you are going to see how to connect i2c lcd display to arduino and how to print on lcd display .
Before going to start this tutorial you must know a brief about i2c communication .
Each I2C bus consists of two signals: SCL and SDA. SCL is the clock signal, and SDA is the data signal. The clock signal is always generated by the current bus master; some slave devices may force the clock low at times to delay the master sending more data (or to require more time to prepare data before the master attempts to clock it out). This is called “clock stretching” and is described on the protocol page.
For more information visit Electronics Projects Hub
Now lets start this Instructable ..
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 |
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.
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
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.
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:
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 |
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.
This article gives you a step-by-step guide to becoming a pro in using Liquid Crystal Display. We will use a free Arduino Simulator to try all the examples without leaving your PC. No hardware is needed.
Important Links
Wokwi Arduino Simulator – https://wokwi.com
Documentation on LCD – https://docs.wokwi.com/parts/wokwi-lcd1602
Facebook group to seek support on the Arduino Simulator – https://docs.wokwi.com/parts/wokwi-lcd1602
What is an LCD?
LCD stands for Liquid Crystal Display. The very commonly used LCD type is 16×2 LCD, and it has two rows and 16 columns.
Here is the table with the LCD pin description. This will help you understand and debug the project later in an intelligent way.
You will also find LCDs with an I2C interface. Fewer Pins will lead to easier connection—less messy wires.
So, based on the LCD, you will have to tell the Arduino UNO which type of LCD you are using. You will see this in the sections later.
The LCD1602 uses the Hitachi HD44780 LCD controller chip. The chip comes with a built-in font and the ability to define up to 8 custom characters.
There are two versions of the chip’s ROM with two different fonts: HD44780UA00, Japanese katakana characters, and HD44780UA02, which includes Western European characters.
Wokwi simulates the HD44780UA00 variant. It has a total of 256 characters:
You can see that the first eight characters are user-defined. It allows you to create custom shapes and store them. You will see how to create custom characters and load them in your following Arduino projects. Let us start with a basic example.
Example 1 – Basic example with LCD “Hello World” Example on I2C interface
We will print a simple text on the LCD using Arduino UNO in this example. In this case, you control what is displayed on the Arduino readily. You only need four cables. Power, Ground, I2C data, and I2C clock.
Here is the code
/* Hello Wokwi! */
#includeLiquidCrystal_I2C lcd(0x27, 20, 4);void setup() {lcd.init();lcd.backlight();lcd.setCursor(1, 0);lcd.print(“Hello, Wokwi!”);}void loop() {}
Connection diagram: LCD and Arduino UNO
Project link: https://wokwi.com/projects/325996799788581459
Use the link above to run the code. You can tinker with the code to change the text displayed or the position. The best thing about the link is that it will save the project as your version. It will be automatically saved under my projects tab on the wokwi site if you are logged in.
Let us take a minute and see the code line by line. Let me try to explain the purpose of each code one by one.
The below line code adds the LCD library to your project. This consists of all the LCD-related functions. Since we are using the I2C version, we have included the standard LCD library made for the I2C version.
#include
Below, we are creating an object called LCD. We pass the I2C address, the number of columns, and the number of rows.
Can you let me know the parameters in the comments if we want to use an LCD2004 instead of LCD1602?
LiquidCrystal_I2C lcd(0x27, 16, 2);
The following line of the code resets and initializes all the LCD registers and prepares them for project usage. This function will be called only once in the
setup()
function.
lcd.init();
To turn on the backlight, you can use the below code. You will be able to see the contents of the display without a backlight, too, if it is a green LCD. Backlight, nevertheless, makes the project more beautiful and reading crisper.
lcd.backlight();
You can mention where the characters should be displayed. You can always use the below function to set/reset the cursor position. This function will be beneficial when you have to display time or a counter that demands the cursor to always be in the same position.
The first parameter tells the position column-wise ( indicated first place, indicates the second place, and so on). The second parameter tells the row number. We have only two rows ( and ).
lcd.setCursor(1, 0);
The last thing to be seen is the below line, where we actually send a message. In this case a string
Hello, Wokwi
is sent!
lcd.print("Hello, Wokwi!");
This completes a basic introduction to the LCD as well as an example project to start the LCD exploration. In the coming sections, we will see different projects as soon as possible 🚧
Example 2 – Basic example with LCD “Hello World” Example on Parallel interface
You will often see the LCD1602 interface connected with Arduino UNO. Here you will use a few control lines, and 4 data lines for communication.
Here is the example connection diagram
-
Arduino
PIN.7
-> D7 -
Arduino
PIN.8
-> D6 -
Arduino
PIN.9
-> D5 -
Arduino
PIN.10
-> D4 -
Arduino
PIN.11
-> E -
Arduino
PIN.GND
-> RW -
Arduino
PIN.5V
-> RS
Code
// LCD1602 to Arduino Uno connection example
#includeLiquidCrystal lcd(12, 11, 10, 9, 8, 7);void setup() {lcd.begin(16, 2);// you can now interact with the LCD, e.g.:lcd.print(“Hello World!”);}void loop() {// …}
I am sure you have noticed the differences. The LCD library file you include is now different since we are no more using the I2C interface.
another difference is the constructor parameters you send. This time, you have to send 6 numbers, representing control and data lines as depicted in the pin list above.
Simulation link: https://wokwi.com/projects/294342288335700490
Example 3 – Creating user-defined characters in LCD1602, easy way
Creating custom characters on the LCD is easy. In this example, you will see how to create a character in the shape of a heart.
Project link: https://wokwi.com/projects/294395602645549578
Code:
// LCD1602 custom characters example
#includeLiquidCrystal lcd(12, 11, 10, 9, 8, 7);uint8_t heart[8] = {0b00000,0b01010,0b11111,0b11111,0b11111,0b01110,0b00100,0b00000,};void setup() {lcd.createChar(3, heart);lcd.begin(16, 2);lcd.print(” I \x03 Arduino”);}void loop() { }
To create a symbol, you have to define an array with 8 numbers. Basically, you will define which pixels will be lit and which pixels will not be in the array.
Let us see some examples.
Heart shape example
The first line is empty.. so it can be defined as
00000
in binary. The second line will be built using
01010
. The third line will be all ones
11111
I hope you got the idea now. You can compare again the binary values above in the code.
Box shape
uint8_t heart[8] = {
0b11111,0b10001,0b10001,0b10001,0b10001,0b10001,0b10001,0b11111,};
an animation for any future projects (think some games!)
In the following sections, we will see more exciting LCD projects
Example 4 – Working with LCD2004 and Arduino UNO (with some simple animation)
In this project, you will see how to use an LCD2004 module. LCD2004 has 4 rows and 20 columns. The basic function names and constructor pattern remains the same.
You might have even come across the blue color LCD2004 module. It might look something similar to this.
Here is one link: https://wokwi.com/projects/294590769009787402
You can get access to the code and the connection diagram in the link above.
here is how the animation looks!
Simulation link: https://wokwi.com/projects/326034190442168916
Simulation output:
To be continued 😉
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.
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.
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.
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.
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.
#includeLiquidCrystal_I2C lcd(0x3F, 16, 2); // set the LCD address to 0x3F for a 16 chars and 2 line display // make some custom characters: byte Heart[8] = { 0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000 }; byte Bell[8] = { 0b00100, 0b01110, 0b01110, 0b01110, 0b11111, 0b00000, 0b00100, 0b00000 }; byte Alien[8] = { 0b11111, 0b10101, 0b11111, 0b11111, 0b01110, 0b01010, 0b11011, 0b00000 }; byte Check[8] = { 0b00000, 0b00001, 0b00011, 0b10110, 0b11100, 0b01000, 0b00000, 0b00000 }; byte Speaker[8] = { 0b00001, 0b00011, 0b01111, 0b01111, 0b01111, 0b00011, 0b00001, 0b00000 }; byte Sound[8] = { 0b00001, 0b00011, 0b00101, 0b01001, 0b01001, 0b01011, 0b11011, 0b11000 }; byte Skull[8] = { 0b00000, 0b01110, 0b10101, 0b11011, 0b01110, 0b01110, 0b00000, 0b00000 }; byte Lock[8] = { 0b01110, 0b10001, 0b10001, 0b11111, 0b11011, 0b11011, 0b11111, 0b00000 }; void setup() { lcd.init(); // Make sure backlight is on lcd.backlight(); // create a new characters lcd.createChar(0, Heart); lcd.createChar(1, Bell); lcd.createChar(2, Alien); lcd.createChar(3, Check); lcd.createChar(4, Speaker); lcd.createChar(5, Sound); lcd.createChar(6, Skull); lcd.createChar(7, Lock); // Clears the LCD screen lcd.clear(); // Print a message to the lcd. lcd.print("Custom Character"); } // Print All the custom characters void loop() { lcd.setCursor(0, 1); lcd.write(0); lcd.setCursor(2, 1); lcd.write(1); lcd.setCursor(4, 1); lcd.write(2); lcd.setCursor(6, 1); lcd.write(3); lcd.setCursor(8, 1); lcd.write(4); lcd.setCursor(10, 1); lcd.write(5); lcd.setCursor(12, 1); lcd.write(6); lcd.setCursor(14, 1); lcd.write(7); }
The output appears as shown.
Code Explanation:
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.
byte Heart[8] = { 0b00000, 0b01010, 0b11111, 0b11111, 0b01110, 0b00100, 0b00000, 0b00000 };
In the setup, we use the
createChar()
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);
Display
A library for I2C LCD displays.
The library allows to control I2C displays with functions extremely similar to LiquidCrystal library. THIS LIBRARY MIGHT NOT BE COMPATIBLE WITH EXISTING SKETCHES.
Maintainer: Marco Schwartz
This library is compatible with the avr architecture so you should be able to use it on the following Arduino boards:
Note: while the library is supposed to compile correctly on these architectures, it might require specific hardware features that may be available only on some boards.
To use this library, open the Library Manager in the Arduino IDE and install it from there.
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.
Code mẫu
#include
#includeLiquidCrystal_I2C lcd(0x3F,16,2); void setup() { lcd.init(); lcd.backlight(); lcd.setCursor(2,0); lcd.print(“Arduinokit.vn”); lcd.setCursor(0,1); lcd.print(“Xin chao cac ban”); } void loop() { }
Giải thích code
LiquidCrystal_I2C lcd(0x3F,16,2);
- Đặt địa chỉ LCD là 0x3F cho màn hình LCD 16×2.
- 16 là số cột của màn hình (nếu dùng loại màn hình 20×4) thì thay bằng 20.
- 2 là số dòng của màn hình (nếu dùng loại màn hình 20×4) thì thay bằng 4.
lcd.init();
Khởi động màn hình LCD, bắt đầu cho phép Arduino sử dụng màn hình.
lcd.backlight();
Bật đèn nền LCD 16×2.
lcd.setCursor(2,0);
Đưa con trỏ tới hàng 1, cột 3.
Lưu ý: giá trị hàng và cột bắt đầu từ số 0 có nghĩa 0 là hàng(cột) 1.
lcd.print(“Arduinokit.vn”);
Xuất ra dòng chữ Arduinokit.vn tại vị trí con trỏ ở hàng 1, cột 3.
lcd.setCursor(0,1); lcd.print(“Xin chao cac ban”);
Đ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é.
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.
Ưu điểm
- Tiết kiệm chân cho vi điều khiển.
- Dễ dàng kết nối với LCD.
Thông số kĩ thuật
- Điện áp hoạt động: 2.5-6V DC.
- Hỗ trợ màn hình: LCD1602,1604,2004 (driver HD44780).
- Giao tiếp: I2C.
- Đị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
About LCD I2C 16×2
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.
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
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
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’.
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ế
Keywords searched by users: qapass lcd arduino i2c
Categories: Cập nhật 53 Qapass Lcd Arduino I2C
See more here: kientrucannam.vn
See more: https://kientrucannam.vn/vn/