Chuyển tới nội dung
Home » I2C Lcd Display Arduino Code | Library Installation

I2C Lcd Display Arduino Code | Library Installation

How to Use I2C LCD with Arduino | Very Easy Arduino LCD I2C Tutorial | Arduino 16x2 LCD I2C Tutorial

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.

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
How to Use I2C LCD with Arduino | Very Easy Arduino LCD I2C Tutorial | Arduino 16x2 LCD I2C Tutorial
How to Use I2C LCD with Arduino | Very Easy Arduino LCD I2C Tutorial | Arduino 16×2 LCD I2C 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.

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.
Arduino LCD I2C - Tutorial with Arduino Uno
Arduino LCD I2C – Tutorial with Arduino Uno

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

Useful Functions from the Library

In this section, I have picked some useful functions from the library and discussed them. I have written some example code where needed. Try to run those codes and see the output. You will understand the function very easily.


clear()

– Clears the display and places the cursor at the top left corner. You can use this function to display different text/strings at the same place at a time. The below code shows the use of this function.


#include LiquidCrystal_I2C lcd(0x27, 16, 2); void setup() { lcd.init(); lcd.backlight(); } void loop() { lcd.clear(); lcd.print("Hello !"); delay(1000); lcd.clear(); lcd.print("Welcome to"); delay(1000); lcd.clear(); lcd.print("Circuit Geeks"); delay(1000); }


home()

– Places the cursor at the top left corner of the display without clearing the display.


cursor()

– Displays the LCD cursor. The default cursor is an underscore line.


noCursor()

– Hides the LCD cursor.


blink()

– Creates a blinking block-type LCD cursor.


noBlink()

– Disable the blinking-block type LCD cursor.


display()

– Turns on the LCD screen and displays the texts/characters that were previously printed on the display.


noDisplay()

– Turns off the LCD screen but does not clear data from the LCD memory. The below code shows the use of display() and noDisplay() functions.


#include LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); void setup() { lcd.init(); lcd.backlight(); lcd.print("Blinking Display"); } void loop() { lcd.display(); delay(1000); lcd.noDisplay(); delay(1000); }


write()

– This function is used to write a character to the display. You can see the use of this function in the I2C LCD custom character section below.


scrollDisplayLeft()

– Moves the display content one step to the left. You can use this function in a loop to create a scrolling text effect.


scrollDisplayRight()

– Moves the display content one step to the right. The below code shows the use of these functions.


#include LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); void setup() { lcd.init(); lcd.backlight(); lcd.setCursor(2, 0); lcd.print("Hello world!"); } void loop() { for (int i = 0; i < 8; i++) { lcd.scrollDisplayLeft(); delay(250); } for (int i = 0; i < 16; i++) { lcd.scrollDisplayRight(); delay(250); } for (int i = 0; i < 8; i++) { lcd.scrollDisplayLeft(); delay(250); } }


autoscroll()

– Scrolls the content of the display automatically.


noAutoscroll()

– Stops the auto-scrolling display. See the below code to understand these functions.


#include LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); void setup() { lcd.init(); lcd.backlight(); } char text_1[12] = "Hello world!"; void loop() { lcd.setCursor(14, 0); lcd.autoscroll(); for (int i = 0; i < 12; i++) { lcd.print(text_1[i]); delay(300); } lcd.noAutoscroll(); delay(2000); lcd.clear(); }


leftToRight()

– sets the display orientation from left to right. That means the text/strings will flow from left to right.


rightToLeft()

– sets the display orientation from right to left. That means the text/strings will flow from right to left. The below code shows the use of this function.


#include LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); void setup() { lcd.init(); lcd.backlight(); lcd.setCursor(15, 0); lcd.rightToLeft(); lcd.print("ABCDEFG"); lcd.setCursor(0, 1); lcd.leftToRight(); lcd.print("ABCDEFG"); } void loop() {}

How to use a 1602 i2c Serial LCD Display with Arduino
How to use a 1602 i2c Serial LCD Display with Arduino

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’.

I2C LCD Custom Character

As you see in the above section the character in a Liquid crystal display is built using a grid of 5×8 small pixels. You can individually turn on and off any pixel and make your own custom character. The custom character data is stored in the CGRAM of the display.

CGROM and CGROM

Displays that use the Hitachi HD44780 controller have two types of memories – CGROM and CGRAM (Character Generator ROM & RAM). CGROM is non-volatile means it can’t be modified whereas CGRAM is volatile so it can be modified at any time.

CGROM stored all permanent fonts that can be displayed by using their ASCII code. For example, the character ‘A’ can be written using write(65) or write(0x41).

CGRAM is used for storing user-defined characters. The Hitachi HD44780 controller has a CGROM of 64 bytes. So for a 5×8 pixel-based LCD, up to 8 user-defined characters can be stored in the CGRAM. And for a 5×10 pixel-based LCD, only 4 user-defined characters can be stored.

To print a custom character, you need to make the byte code for that character first. Creating a byte code for a custom character is very simple. You need to go through row-wise and write “1” if you need to turn on a pixel and write “0” if you need to turn off a pixel. Then go to the next row and do the same. Do it for all eight rows.

So for example, If I want to create a custom character for a smiley like below –

When you go through row-wise and follow the above instructions you will get numbers like this –


00000, 10001, 00000, 00000, 10001, 01110, 00000, 00000,

Now simply place a “B” before every row and this would be the byte code for that character.


B00000, B10001, B00000, B00000, B10001, B01110, B00000, B00000,

You can store that data in an array like this –


byte smiley[] = {B00000, B10001, B00000, B00000, B10001, B01110, B00000, B00000}

You can convert the binary value to hexadecimal and use it like this –


byte smiley[] = {0x0, 0x11, 0x0 , 0x0, 0x11, 0xE, 0x0, 0x0}

There are some online and offline tools available to visually draw the custom character and it will automatically generate the byte code for you. I suggest you use the online LCD Character Creator tools.

Now in the below sketch, I will use that bite code to print the custom character on the LCD. Upload the below code to your Arduino board and see what the display looks like.

PS:- Don’t forget to change the I2C address and dimension of the display in the below sketch.

Arduino Code


#include // Set the LCD address and dimension LiquidCrystal_I2C lcd(0x27, 16, 2); // Create a custom character: byte smiley[] = { B00000, B10001, B00000, B00000, B10001, B01110, B00000, B00000, }; void setup() { lcd.init(); lcd.clear(); lcd.backlight(); // Turns on backlight // Create a new characters: lcd.createChar(0, smiley); } void loop() { // Print all the custom characters: lcd.setCursor(0, 0); lcd.write(0); }

Explaining the Code

Here you can see that I include the

LiquidCrystal_I2C

library first. Then I create a

LiquidCrystal_I2C

variable for my LCD using the I2C address and dimension of my LCD. I am using an array

smiley[]

to store the bit data for the custom character.

In the setup section, I create a custom character from that byte array using the

createChar()

function. It needs two parameters – a number between 0 – 7 to reserve a space in the CGRAM for the custom character and the name of the byte array.

Then in the loop section, I use the

write()

function to display custom characters. This function needs the character number as an argument.

In the below example, I will print eight different custom characters on the LCD.


#include LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2); // Make custom characters: byte smiley[8] = {B00000, B10001, B00000, B00000, B10001, B01110, B00000, B00000}; byte man[8] = {B00000, B01110, B10001, B01110, B00100, B11111, B00100, B11011}; byte music[8] = {B00001, B00011, B00101, B01001, B01001, B01011, B11011, B11000}; byte heart[8] = {B00000, B01010, B11111, B11111, B01110, B00100, B00000, B00000}; byte speaker[8] = {B00001, B00011, B01111, B01111, B01111, B00011, B00001, B00000}; byte bell[8] = {0x00, 0x04, 0x0E, 0x0E, 0x0E, 0x1F, 0x04, 0x00}; byte pie[8] = {0x00, 0x1F, 0x0A, 0x0A, 0x0A, 0x13, 0x00, 0x00}; byte ohm[8] = {0x00, 0x0E, 0x11, 0x11, 0x0A, 0x1B, 0x00, 0x00}; void setup() { lcd.init(); lcd.backlight(); // Create a new characters: lcd.createChar(0, smiley); lcd.createChar(1, man); lcd.createChar(2, music); lcd.createChar(3, heart); lcd.createChar(4, speaker); lcd.createChar(5, bell); lcd.createChar(6, pie); lcd.createChar(7, ohm); // Clears the LCD screen: lcd.clear(); lcd.setCursor(0, 0); lcd.print("Custom Caracter"); } void loop() { // Print all the custom characters: lcd.setCursor(0, 1); lcd.write(byte(0)); lcd.setCursor(2, 1); lcd.write(byte(1)); lcd.setCursor(4, 1); lcd.write(byte(2)); lcd.setCursor(6, 1); lcd.write(byte(3)); lcd.setCursor(8, 1); lcd.write(byte(4)); lcd.setCursor(10, 1); lcd.write(byte(5)); lcd.setCursor(12, 1); lcd.write(byte(6)); lcd.setCursor(14, 1); lcd.write(byte(7)); }

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 Tutorial-Visuino
Arduino LCD I2C Tutorial-Visuino

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 ..

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.

{916} i2c LCD configuration with Arduino UNO in Tinkercad
{916} i2c LCD configuration with Arduino UNO in Tinkercad
    • 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()

Tổng quan LCD 16×2 và giao tiếp I2C LCD sử dụng Arduino

Arduino I2C LCD – Print a Simple Text

Printing text on the LCD is very simple. The below sketch will print some text on the display. But before uploading this sketch you need to make some minor changes according to your display size and address.

In the second line, I created a

LiquidCrystal_I2C

variable. It requires three variables – the I2C address of the LCD and the dimension of the LCD (columns and rows of the display). The I2C address of my display is

0x27

and it has 16 columns and 2 rows. So I will use –

LiquidCrystal_I2C lcd(0x27,16,2)

. If you have a different LCD display change the I2C address and dimension accordingly.

Now the sketch is ready for your display. Upload it to your Arduino board and see the output.

Code to Print a Simple Text


#include LiquidCrystal_I2C lcd(0x27,16,2); void setup() { lcd.init(); lcd.clear(); lcd.backlight(); lcd.setCursor(2,0); lcd.print("Hello world!"); lcd.setCursor(2,1); lcd.print("LCD Tutorial"); } void loop() {}

Explaining the Code

The sketch is very simple. It begins with including the

LiquidCrystal_I2C

header file –

#include LiquidCrystal_I2C.h>

Then you need to define a

LiquidCrystal_I2C

variable for your LCD. It requires three variables – the I2C address of the LCD and the dimension of the LCD (columns and rows of the display).

Then in the setup section, you have to initialize the display using the

init()

function. The

clear()

function will clear the display buffer. It is good to clear the display buffer before printing anything on the display so that it does not display anything that was previously stored in the display buffer.


backlight()

– this will turn on the display backlight.


setCursor(col, row)

– this will set the cursor to a position from where the text and characters will print. This function requires two variables – columns, and rows.One thing to note here is that columns and rows counting start from the top left corner and it starts with (0, 0).


print()

– this will print text and characters on the display.

Now you can understand that first I set the cursor to the third column of the first row and printed some text there.


lcd.setCursor(2,0); lcd.print("Hello world!");

Then I set the cursor to the third column of the second row and print some text there.


lcd.setCursor(2,1); lcd.print("LCD Tutorial");

For this example, we don’t have to use the loop section, so leave it blank.

How to Connect an I2C Lcd Display to an Arduino Uno Tutorial
How to Connect an I2C Lcd Display to an Arduino Uno Tutorial

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.

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.


#include LiquidCrystal_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);

In the previous Arduino LCD tutorial, you have noticed that the classic parallel LCD consumes a lot of pins on the Arduino. Even in the 4-bit mode, it requires at least 6 digital I/O pins on the Arduino. So in many projects where you use the classic parallel LCD, you will run out of pins very easily.

The solution to this problem is to use an I2C LCD display. It requires only two digital I/O pins and you can even share those two pins with other I2C devices.

In this tutorial, I will show you how to interface an I2C LCD with Arduino Uno and print some text, numbers, and custom characters.

Table of Contents

  • 1 I2C LCD Overview
  • 2 I2C LCD Pinout
  • 3 Parts required for this Tutorial
  • 4 Connect an I2C LCD with Arduino
  • 5 Arduino I2C LCD Code
  • 6 Find the I2C address of your LCD
  • 7 Arduino I2C LCD – Print a Simple Text
  • 8 Useful Functions from the Library
  • 9 I2C LCD Custom Character
How to use  LCD LCD1602  with I2C module for Arduino - Robojax
How to use LCD LCD1602 with I2C module for Arduino – Robojax

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.

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");

Using LCD Displays with Arduino
Using LCD Displays with Arduino

Code mẫu

#include

#include LiquidCrystal_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é.

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
ARDUINO 20x4 LCD i2c Tutorial | How to Print Text On LCD Display
ARDUINO 20×4 LCD i2c Tutorial | How to Print Text On LCD Display

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.

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.

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.

I2C LCD not showing Text | I2C LCD Errors Fixing || 16x2 LCD not displaying Text || 1602 LCD Error
I2C LCD not showing Text | I2C LCD Errors Fixing || 16×2 LCD not displaying Text || 1602 LCD Error

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

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

I2C LCD not showing Text | I2C LCD Errors Fixing || 16x2 LCD not displaying Text || Lcd.begin();
I2C LCD not showing Text | I2C LCD Errors Fixing || 16×2 LCD not displaying Text || Lcd.begin();

I2C LCD Overview

An I2C LCD display consists of a classic parallel LCD and an I2C LCD adapter. The I2C LCD adapter is soldered from the back of the LCD.

If you already have an LCD you can only purchase the I2C LCD adapter and solder it by yourself.

Now let’s have a look at the LCD display and the I2C LCD adapter in detail.

Liquid Crystal Display (LCD)

Commonly available LCD displays with I2C LCD adapters are 16×2 and 20×4 character LCD displays. They both have a total of 16 pins including 8 parallel data pins. So if you don’t use an I2C LCD you will need at least 6 digital I/O pins on the Arduino to display something.

If you look closely you will find that the characters of the LCD are built using a grid of 5×8 pixels. Later in this tutorial, you will learn how to turn on and off any individual pixels to make custom characters.

You can read more about the LCD display in my Arduino LCD tutorial.

I2C LCD Adapter Overview

At the center of this adapter, there is an 8-bit I/O expander chip – PCF8574. It takes the I2C data from the MCU (Arduino) and converts it into serial data required for an LCD display. On one side the I2C LCD adapter has four pins that can be connected to Arduino or any microcontroller that supports the I2C communication protocol. On another side, it has 16 pins that are connected to the LCD display.

There are two header pins to control the backlight of the LCD display. One pin supplies a 5v power and another pin is for the backlight LED. These two pins are connected together by default. So the backlight will be always on. You can remove the jumper to turn off the backlight LED or you can use a potentiometer in between these two pins to control the intensity of the backlight LED.

The I2C LCD adapter also has a small trim pot to adjust the contrast of the display.

How to Change the Default I2C Address

Some I2C LCD adapters come with PCF8574, while others use PCF8574A chips. Each of these chips has its own I2C address. The PCF8574 chip from NXP Semiconductor uses 0x27 while the PCF8574A chip uses 0x3F.

Sometimes you need to change this default I2C address for a project where you use multiple I2C devices on the same I2C bus So that it does not conflict with other I2C devices.

If you use multiple I2C devices on the same I2C bus, sometimes you need to change this default I2C address to avoid conflict with other I2C devices.

To change the default address it has address selection pads – A0, A1, and A2.

Each of these pads has a ground connection above it. By default, each of these pins is connected to VDD or a positive power supply. You can change the address by connecting any pin to the ground pin above it. You can make any possible combination and you will get a different address. From three address pins, you can get 23 different combinations means 8 different addresses.

If you go through the datasheet of PCF8574 by NXP, you will find that PCF8574 and PCF8574A use a 7-bit I2C address. The first four-bit is fixed and the last three-bit is hardware selectable.

As you read above A0, A2, and A3 are connected to the positive power supply by default. So the default address of PCF8574 is 0100111 in binary or 0x27 in hex. If you connect the A0 pin to the ground pin it will become logic LOW. The address will change to 0100110 in binary or 0x26 in hex.

See the illustration below to find all the possible connections and the hex addresses.

In the same way, you can change the address of a PCF8575A chip. But keep in mind that the first four bits of the PCF8575 are 0100 and the first four bits of the PCF8575A are 0111. Calculate the address according to it.

Parts required for this Tutorial

SL. Preview Name Link
Arduino Uno R3 Amazon | Aliexpress
I2C LCD Amazon | Aliexpress
Jumper Wires Amazon | Aliexpress
How To Connect LCD display with Arduino Nano kit
How To Connect LCD display with Arduino Nano kit

Troubleshooting on LCD I2C

If the text is not displayed on LCD I2C, please check the following issues:

  1. Adjust the brightness of LCD by rotating potentiometer in the backside of LCD
  2. 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:

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.

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

How to Use ESP8266 NodeMCU with 16x2 LCD i2C Display
How to Use ESP8266 NodeMCU with 16×2 LCD i2C 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.

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ế
Arduino Tutorial: 20x4 I2C Character LCD display with Arduino Uno from Banggood.com
Arduino Tutorial: 20×4 I2C Character LCD display with Arduino Uno from Banggood.com

Arduino I2C LCD Code

In this tutorial, I am using the LiquidCrystal-I2C library to control the display. It has many pre-built functions to control an I2C LCD display in a much simpler way.

To install the library first you need to download it from the LiquidCrystal-I2C GitHub repository. Then open your Arduino IDE and navigate to Sketch > Include Library > Add .ZIP Library.

Select the .zip file you just downloaded and click the open button. The installation is done, now you can include the library in your code.

Connect an I2C LCD with Arduino

Connecting an I2C LCD display with Arduino is very simple compared to a normal LCD display. You only need to connect four wires to the Arduino.

Connect the LCD’s VCC pin to the Arduino 5v pin and the Ground pin to the Arduino Ground pin. The remaining two pins are SCL and SDA. You need to connect the SCL pin to the Arduino SCL pin and SDA to the Arduino SDA pin.

On the Arduino Uno, the SCL pin is A5 and the SDA pin is A4 pin, connect it accordingly.

If you are using a different Arduino board, you will find the SCL and SDA pin for the respective board in the below table.

Arduino Board SCL Pin SDA Pin
Arduino Uno A5 A4
Arduino Nano A5 A4
Arduino Micro
Arduino Leonardo
Arduino Mega 2560 21 20
Arduino Due 21 20
How to Connect an I2C Lcd Display to an Arduino Uno
How to Connect an I2C Lcd Display to an Arduino Uno

Find the I2C address of your LCD

You need to know the I2C address of the LCD before starting the communication. To know the I2C address of your LCD run the below sketch.


#include

void setup() { Serial.begin(9600); Serial.println("\nI2C Scanner"); Serial.println("Scanning..."); byte device_count = 0; Wire.begin(); for (byte address = 1; address < 127; address++ ) { Wire.beginTransmission(address); if (Wire.endTransmission() == 0) { // Serial.print ("Found address: "); Serial.print("I2C device found at address 0x"); Serial.print(address, HEX); Serial.println(" !"); device_count++; delay(1); } } Serial.println ("Done."); Serial.print ("Found "); Serial.print (device_count); Serial.println (" device(s)."); } void loop() {}

Open the serial monitor on the Arduino IDE and you will get a result like this –

Note down the I2C address of your LCD somewhere. You will need it later in the following sketches.

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.

Arduino LCD Display Menu System Tutorial
Arduino LCD Display Menu System Tutorial

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

Keywords searched by users: i2c lcd display arduino code

I2C Liquid Crystal Displays | Arduino Project Hub
I2C Liquid Crystal Displays | Arduino Project Hub
In-Depth: Interfacing An I2C Lcd With Arduino
In-Depth: Interfacing An I2C Lcd With Arduino
Giao Tiếp Lcd 16X2 I2C Với Arduino Uno | Điện Tử Dat
Giao Tiếp Lcd 16X2 I2C Với Arduino Uno | Điện Tử Dat
How To Interface I2C Lcd Display With Arduino ? - Geeksforgeeks
How To Interface I2C Lcd Display With Arduino ? – Geeksforgeeks
How To Interface I2C Lcd With Arduino Nano
How To Interface I2C Lcd With Arduino Nano
How To Connect I2C Lcd Display To Arduino Uno : 5 Steps (With Pictures) -  Instructables
How To Connect I2C Lcd Display To Arduino Uno : 5 Steps (With Pictures) – Instructables
Interfacing An I2C Lcd With Arduino Uno
Interfacing An I2C Lcd With Arduino Uno
How To Interface I2C Lcd Display With Arduino ? - Geeksforgeeks
How To Interface I2C Lcd Display With Arduino ? – Geeksforgeeks
Arduino I2C Lcd 16X2 Interfacing
Arduino I2C Lcd 16X2 Interfacing
Arduino 16X2 Lcd Display With I2C - Hello World - Hackster.Io
Arduino 16X2 Lcd Display With I2C – Hello World – Hackster.Io
How To Connect I2C Lcd Display To Arduino Uno : 5 Steps (With Pictures) -  Instructables
How To Connect I2C Lcd Display To Arduino Uno : 5 Steps (With Pictures) – Instructables
How To Use I2C Lcd With Arduino | Very Easy Arduino Lcd I2C Tutorial |  Arduino 16X2 Lcd I2C Tutorial - Youtube
How To Use I2C Lcd With Arduino | Very Easy Arduino Lcd I2C Tutorial | Arduino 16X2 Lcd I2C Tutorial – Youtube
How To Interface I2C Lcd With Arduino Nano
How To Interface I2C Lcd With Arduino Nano
How To Use I2C Lcd Or Multiple I2C Lcd With Single Arduino
How To Use I2C Lcd Or Multiple I2C Lcd With Single Arduino
I2C Lcd With Arduino Display Scrolling Text And Custom Characters
I2C Lcd With Arduino Display Scrolling Text And Custom Characters
How To Set Up And Use A 1602 I2C Serial Lcd With Your Arduino. |  Z-Hut_Arduino
How To Set Up And Use A 1602 I2C Serial Lcd With Your Arduino. | Z-Hut_Arduino
I2C 1602 Serial Lcd For Arduino & Rpi
I2C 1602 Serial Lcd For Arduino & Rpi
How To Connect An I2C Lcd Display To An Arduino Uno Tutorial - Youtube
How To Connect An I2C Lcd Display To An Arduino Uno Tutorial – Youtube
I2C 16X2 Arduino Lcd Display Module | The Pi Hut
I2C 16X2 Arduino Lcd Display Module | The Pi Hut
Arduino - Lcd I2C | Arduino Tutorial
Arduino – Lcd I2C | Arduino Tutorial

See more here: kientrucannam.vn

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *