Skip to content
Home » Lcd Coding In Arduino | Adjusting The Contrast Of The Lcd

Lcd Coding In Arduino | Adjusting The Contrast Of The Lcd

Arduino LCD I2C - Tutorial with Arduino Uno

Liquid Crystal Displays (LCD) with Arduino

Find out how to wire an LCD to an Arduino, and how to use the LiquidCrystal library through a set of useful examples.

This article was revised on 2021/11/18 by Karl Söderby.

The LiquidCrystal library allows you to control LCD displays that are compatible with the Hitachi HD44780 driver. There are many of them out there, and you can usually tell them by the 16-pin interface.

The LCDs have a parallel interface, meaning that the microcontroller has to manipulate several interface pins at once to control the display. The interface consists of the following pins:

  • A register select (RS) pin that controls where in the LCD’s memory you’re writing data to. You can select either the data register, which holds what goes on the screen, or an instruction register, which is where the LCD’s controller looks for instructions on what to do next.
  • A Read/Write (R/W) pin that selects reading mode or writing mode
  • An Enable pin that enables writing to the registers
  • 8 data pins (D0 -D7). The states of these pins (high or low) are the bits that you’re writing to a register when you write, or the values you’re reading when you read.

There’s also a display contrast pin (Vo), power supply pins (+5V and GND) and LED Backlight (Bklt+ and BKlt-) pins that you can use to power the LCD, control the display contrast, and turn on and off the LED backlight, respectively.

The process of controlling the display involves putting the data that form the image of what you want to display into the data registers, then putting instructions in the instruction register. The LiquidCrystal Library simplifies this for you so you don’t need to know the low-level instructions.

The Hitachi-compatible LCDs can be controlled in two modes: 4-bit or 8-bit. The 4-bit mode requires seven I/O pins from the Arduino, while the 8-bit mode requires 11 pins. For displaying text on the screen, you can do most everything in 4-bit mode, so example shows how to control a 16×2 LCD in 4-bit mode.

About LCD 16×2

Pinout

LCD has up to 16 pins. In the most common uses, we do NOT use all pins.

With the support of LiquidCrystal library, we even can use LCD WITHOUT knowing the meaning of these pins. However, if you are curious or want to know in-depth, let’s see these pins and their functionality:

  • GND pin: needs to be connected to GND (0V).
  • VCC pin: the power supply for the LCD, needs to be connected to VCC (5V).
  • Vo (LCD Contrast) pin: controls the contrast and brightness of the LCD, can be connected to 5V (the highest contrast and brightness), or connected to a potentiometer (to adjust to the contrast and brightness)
  • RS (Register Select) pin: There are two kinds of data that need to send to LCD: command (to control LCD) and data. These two are sent on the same data bus. RS pin tells the LCD whether the data on the data bus is the commands or the data.
  • If we want to send the command to control LCD, we need to set RS pin to LOW (like set the cursor to a specific location, clear the display …).
  • If we want to send the data to display on LCD, we need to set RS pin to HIGH.
  • R/W (Read/Write) pin: is to select READ mode or WRITE mode.
  • If we want to read data from LCD, this pin needs to be set to HIGH.
  • If we want to send data to LCD, this pin needs to be set to LOW. Since we’re just using this LCD as an OUTPUT device, we’re going to tie this pin LOW.
  • EN (Enable) pin: is used to enable the LCD. HIGH to enable the LCD, LOW to disable the LCD.
  • D0-D7 (Data Bus) pins: carries data and command between Arduino and LCD. There are two modes to send data: 4-bit mode and 8-bit mode.
  • A-K (Anode & Cathode) pins: are used to power the LCD backlight. A pin needs to be connected to VCC. K pin needs to be connected to GND.
4-bit mode and 8-bit mode
  • 8-bit mode: 8 bits of a byte are sent at the same time in pin D0 to D7.
  • 4-bit mode: 8 bits of a byte is sent two times, each time 4 bits in pin D4 to D7.

8-bit mode is faster than the 4-bit mode, but use more pins than 4-bit mode. The mode selection is performed at the initialization process by sending a command to LCD.

This tutorial uses 4-bit mode, which is the most common-used.

In this mode, LCD’s pins:

  • 6 pins (RS, EN, D4, D5, D6, and D7) are connected to Arduino’s pin.
  • 4 pins (D0, D1, D2, and D3) are NOT connected.
  • 6 remaining pins are connected to GND/VCC or potentiometer.

LCD pin table in 4-bit mode

LCD PIN CONNECTED TO
01 GND GND
02 VCC 5V
03 Vo 5V or potentiometer’s pin
04 RS An Arduino’s pin
05 R/W GND
06 EN An Arduino’s pin
07 D0 NOT connected
08 D1 NOT connected
09 D2 NOT connected
10 D3 NOT connected
11 D4 An Arduino’s pin
12 D5 An Arduino’s pin
13 D6 An Arduino’s pin
14 D7 An Arduino’s pin
15 5V
16 GND

LCD Coordinate

LCD 16×2 includes 16 columns and 2 rows. the conlums and rows are indexed from 0.

How It Works

The process of sending data (to be displayed) to LCD:

  1. Arduino sets RS pin to HIGH (to select data register)
  2. Arduino writes data to D4 → D7 pins (data bus).
  3. LCD receives data on the data bus.
  4. LCD stores the received data in the data resistor since the RS pin is HIGH. Then, LCD displays the data on the screen

The process of sending command (to control) to LCD (e.g, blink LCD, set the cursor to a specific location, clear the display …):

  1. Arduino sets RS pin to LOW (to select command register)
  2. Arduino writes command to D4 → D7 pins (data bus).
  3. LCD receives data on the data bus.
  4. LCD stores the received data in the command resistor since the RS pin is LOW. Then, LCD takes action based on the value of the command.

Arduino – LCD

Controlling LCD is a quite complicated task. Fortunately, thanks to the LiquidCrystal library, this library simplifies the process of controlling LCD for you so you don’t need to know the low-level instructions. You just need to connect Arduino to LCD and use the functions of the library. The using LCD is a piece of cake.

Arduino LCD I2C - Tutorial with Arduino Uno
Arduino LCD I2C – Tutorial with Arduino Uno

How To Program For LCD

  • Include the library:
  • Define which Arduino’s pin connected to six LCD’s pins: RS, EN, D4, D4, D6, D7

One of the advantages of the library is that Arduino’s pin connected to LCD is settable. This makes it flexible when you connect Arduino with LCD and other sensors/actuators.

  • Declare a LiquidCrystal object:
  • Set up the LCD’s number of columns and rows.
  • 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:

You can choose any six pins of Arduino to connect to LCD, as long as you specify the connected pin in the Arduino code.

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.

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

16×2 LCD Pinout

It has 16 pins and the first one from left to right is the Ground pin. The second pin is the VCC which we connect the 5 volts pin on the Arduino Board. Next is the Vo pin on which we can attach a potentiometer for controlling the contrast of the display.

Next, The RS pin or register select pin is used for selecting whether we will send commands or data to the LCD. For example if the RS pin is set on low state or zero volts, then we are sending commands to the LCD like: set the cursor to a specific location, clear the display, turn off the display and so on. And when RS pin is set on High state or 5 volts we are sending data or characters to the LCD.

Next comes the R/W pin which selects the mode whether we will read or write to the LCD. Here the write mode is obvious and it is used for writing or sending commands and data to the LCD. The read mode is used by the LCD itself when executing the program which we don’t have a need to discuss about it in this tutorial.

Next is the E pin which enables the writing to the registers, or the next 8 data pins from D0 to D7. So through this pins we are sending the 8 bits data when we are writing to the registers or for example if we want to see the latter uppercase A on the display we will send 0100 0001 to the registers according to the ASCII table. The last two pins A and K, or anode and cathode are for the LED back light.

After all we don’t have to worry much about how the LCD works, as the Liquid Crystal Library takes care for almost everything. From the Arduino’s official website you can find and see the functions of the library which enable easy use of the LCD. We can use the Library in 4 or 8 bit mode. In this tutorial we will use it in 4 bit mode, or we will just use 4 of the 8 data pins.

16×2 LCD Pinout

It has 16 pins and the first one from left to right is the Ground pin. The second pin is the VCC which we connect the 5 volts pin on the Arduino Board. Next is the Vo pin on which we can attach a potentiometer for controlling the contrast of the display.

Next, The RS pin or register select pin is used for selecting whether we will send commands or data to the LCD. For example if the RS pin is set on low state or zero volts, then we are sending commands to the LCD like: set the cursor to a specific location, clear the display, turn off the display and so on. And when RS pin is set on High state or 5 volts we are sending data or characters to the LCD.

Next comes the R/W pin which selects the mode whether we will read or write to the LCD. Here the write mode is obvious and it is used for writing or sending commands and data to the LCD. The read mode is used by the LCD itself when executing the program which we don’t have a need to discuss about it in this tutorial.

Next is the E pin which enables the writing to the registers, or the next 8 data pins from D0 to D7. So through this pins we are sending the 8 bits data when we are writing to the registers or for example if we want to see the latter uppercase A on the display we will send 0100 0001 to the registers according to the ASCII table. The last two pins A and K, or anode and cathode are for the LED back light.

After all we don’t have to worry much about how the LCD works, as the Liquid Crystal Library takes care for almost everything. From the Arduino’s official website you can find and see the functions of the library which enable easy use of the LCD. We can use the Library in 4 or 8 bit mode. In this tutorial we will use it in 4 bit mode, or we will just use 4 of the 8 data pins.

How to Set Up and Program an LCD on the Arduino
How to Set Up and Program an LCD on the Arduino

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

Conclusion

So, we have covered pretty much everything we need to know about using an LCD with Arduino. These LCD Character displays are really handy for displaying information for many electronics project. In the examples above I used 16×2 LCD, but the same working principle applies for any other size of these character displays.

I hope you enjoyed this tutorial and learned something new. Feel free to ask any question in the comments section below and don’t forget to check out my full collection of 30+ Arduino Projects.

Liquid Crystal Displays (LCD) with Arduino

Find out how to wire an LCD to an Arduino, and how to use the LiquidCrystal library through a set of useful examples.

This article was revised on 2021/11/18 by Karl Söderby.

The LiquidCrystal library allows you to control LCD displays that are compatible with the Hitachi HD44780 driver. There are many of them out there, and you can usually tell them by the 16-pin interface.

The LCDs have a parallel interface, meaning that the microcontroller has to manipulate several interface pins at once to control the display. The interface consists of the following pins:

  • A register select (RS) pin that controls where in the LCD’s memory you’re writing data to. You can select either the data register, which holds what goes on the screen, or an instruction register, which is where the LCD’s controller looks for instructions on what to do next.
  • A Read/Write (R/W) pin that selects reading mode or writing mode
  • An Enable pin that enables writing to the registers
  • 8 data pins (D0 -D7). The states of these pins (high or low) are the bits that you’re writing to a register when you write, or the values you’re reading when you read.

There’s also a display contrast pin (Vo), power supply pins (+5V and GND) and LED Backlight (Bklt+ and BKlt-) pins that you can use to power the LCD, control the display contrast, and turn on and off the LED backlight, respectively.

The process of controlling the display involves putting the data that form the image of what you want to display into the data registers, then putting instructions in the instruction register. The LiquidCrystal Library simplifies this for you so you don’t need to know the low-level instructions.

The Hitachi-compatible LCDs can be controlled in two modes: 4-bit or 8-bit. The 4-bit mode requires seven I/O pins from the Arduino, while the 8-bit mode requires 11 pins. For displaying text on the screen, you can do most everything in 4-bit mode, so example shows how to control a 16×2 LCD in 4-bit mode.

How to Set Up and Program an LCD on the Arduino
How to Set Up and Program an LCD on the Arduino

LCD Arduino Code

Here’s a simple code through which we can explain the working principle of the Liquid Crystal library. This is the code of the first example from the video:


/* * Arduino LCD Tutorial * * Crated by Dejan Nedelkovski, * www.HowToMechatronics.com * */ LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7) void setup() { lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display } } void loop() { lcd.print("Arduino"); // Prints "Arduino" on the LCD delay(3000); // 3 seconds delay lcd.setCursor(2,1); // Sets the location at which subsequent text written to the LCD will be displayed lcd.print("LCD Tutorial"); delay(3000); lcd.clear(); // Clears the display lcd.blink(); //Displays the blinking LCD cursor delay(4000); lcd.setCursor(7,1); delay(3000); lcd.noBlink(); // Turns off the blinking LCD cursor lcd.cursor(); // Displays an underscore (line) at the position to which the next character will be written delay(4000); lcd.noCursor(); // Hides the LCD cursor lcd.clear(); // Clears the LCD screen }

Code language: Arduino (arduino)

Code description:

First thing we need to do is it insert the Liquid Crystal Library. We can do that like this: Sketch > Include Library > Liquid Crystal. Then we have to create an LC object. The parameters of this object should be the numbers of the Digital Input pins of the Arduino Board respectively to the LCD’s pins as follow: (RS, Enable, D4, D5, D6, D7). In the setup we have to initialize the interface to the LCD and specify the dimensions of the display using the begin() function.

In the loop we write our main program. Using the print() function we print on the LCD.


lcd.print("Arduino"); // Prints "Arduino" on the LCD

Code language: Arduino (arduino)

The setCursor() function is used for setting the location at which subsequent text written to the LCD will be displayed.


lcd.setCursor(2,1); // Sets the location at which subsequent text written to the LCD will be displayed

Code language: Arduino (arduino)

The blink() function is used for displaying a blinking cursor and the noBlink() function for turning off.


lcd.blink(); //Displays the blinking LCD cursor

Code language: Arduino (arduino)

The cursor() function is used for displaying underscore cursor and the noCursor() function for turning off. Using the clear() function we can clear the LCD screen.


lcd.clear(); // Clears the LCD screen

Code language: Arduino (arduino)

Challenge Yourself

Use LCD to do one of the following projects:

  • Sending text from PC (via Serial Monitor) and display on LCD. Hint: Refer to How to send data from PC to Aduino
  • Displaying the pressed key of the keypad on LCD. Hint: Refer to Arduino – Keypad

In this tutorial, I’ll explain how to set up an LCD on an Arduino and show you all the different ways you can program it. I’ll show you how to print text, scroll text, make custom characters, blink text, and position text. They’re great for any project that outputs data, and they can make your project a lot more interesting and interactive.

The display I’m using is a 16×2 LCD display that I bought for about $5. You may be wondering why it’s called a 16×2 LCD. The part 16×2 means that the LCD has 2 lines, and can display 16 characters per line. Therefore, a 16×2 LCD screen can display up to 32 characters at once. It is possible to display more than 32 characters with scrolling though.

The code in this article is written for LCD’s that use the standard Hitachi HD44780 driver. If your LCD has 16 pins, then it probably has the Hitachi HD44780 driver. These displays can be wired in either 4 bit mode or 8 bit mode. Wiring the LCD in 4 bit mode is usually preferred since it uses four less wires than 8 bit mode. In practice, there isn’t a noticeable difference in performance between the two modes. In this tutorial, I’ll connect the LCD in 4 bit mode.

Watch the video for this tutorial here:

Tutorial LCD 20 Ribuan | Arduino Untuk Pemula
Tutorial LCD 20 Ribuan | Arduino Untuk Pemula

Scrolling text example on 16×2 LCD and Arduino

In case we have a text with length greater than 16 characters, we can scroll the text using the scrollDisplayLeft() or scrollDisplayRight() function from the LiquidCrystal library.

Here’s an example code:


LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7) void setup() { lcd.begin(16, 2); lcd.print("Scrolling Text Example"); } void loop() { lcd.scrollDisplayLeft(); delay(500); }

Code language: Arduino (arduino)

We can choose whether the text will scroll left or right, using the scrollDisplayLeft() or scrollDisplayRight() functions. With the delay() function we can set the scrolling speed.

If you want more control over how the text is scrolling, you could also make the scrolling on your own using a “for” loop. Here’s an example:


LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7) void setup() { lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display } void loop() { // scroll text to the right for (int i = 0; i <= 13; i++) { lcd.setCursor(i, 0); // Sets the location at which subsequent text written to the LCD will be displayed lcd.print("LCD"); delay(500); // 1 second delay lcd.clear(); // Write a character to the LCD } // scroll text to the left for (int i = 12; i >= 1; i--) { lcd.setCursor(i, 0); lcd.print("LCD"); delay(500); lcd.clear(); } }

Code language: Arduino (arduino)

Connecting the LCD to the Arduino

Here’s a diagram of the pins on the LCD I’m using. The connections from each pin to the Arduino will be the same, but your pins might be arranged differently on the LCD. Be sure to check the datasheet or look for labels on your particular LCD:

Also, you might need to solder a 16 pin header to your LCD before connecting it to a breadboard. Follow the diagram below to wire the LCD to your Arduino:

The resistor in the diagram above sets the backlight brightness. A typical value is 220 Ohms, but other values will work too. Smaller resistors will make the backlight brighter.

The potentiometer is used to adjust the screen contrast. I typically use a 10K Ohm potentiometer, but other values will also work.

Here’s the datasheet for the 16×2 LCD with all of the technical information about the display:

Arduino | Hướng dẫn sử dụng chi tiết LCD
Arduino | Hướng dẫn sử dụng chi tiết LCD

How to Connect Arduino to LCD – Wiring Diagram

Here’s how we need to connect the 16×2 LCD display to an Arduino board.

We will use just 6 digital input pins from the Arduino Board. The LCD’s registers from D4 to D7 will be connected to Arduino’s digital pins from 4 to 7. The Enable pin will be connected to pin number 2 and the RS pin will be connected to pin number 1. The R/W pin will be connected to Ground and the Vo pin will be connected to the potentiometer middle pin.

You can get these components from any of the sites below:

  • 16×2 Character LCD…………………….. Amazon / Banggood / AliExpress
  • Potentiometer ……………………………. Amazon / Banggood / AliExpress
  • Arduino Board …………………………… Amazon / Banggood / AliExpress
  • Breadboard and Jump Wires ……… Amazon / Banggood / AliExpress

Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.

What is LCD Character Display?

An LCD character display is a unique type of display that can only output individual ASCII characters with fixed size. Using these individual characters then we can form a text.

If we take a closer look at the display we can notice that there are small rectangular areas composed of 5×8 pixels grid. Each pixel can light up individually, and so we can generate characters within each grid.

The number of the rectangular areas define the size of the LCD. The most popular LCD is the 16×2 LCD, which has two rows with 16 rectangular areas or characters. Of course, there are other sizes like 16×1, 16×4, 20×4 and so on, but they all work on the same principle. Also, these LCDs can have different background and text color.

Using LCD Displays with Arduino
Using LCD Displays with Arduino

Code For Scrolling the letters


#include int speed=0; LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); //initializes 16x2 LCD lcd.setCursor(0,1) lcd.print("Tutorial by Robu.in"); //Scrolling Text } void loop() { for(speed=0; speed<2; speed++) { lcd.scrollDisplayRight(); //scrolls display right by two positions } delay(500); //sets the speed at which display moves Please addjust this value if cant see letters clearly. }

Interfacing LCD with Arduino

The Following components You will need to interface the LCD with Arduino

  1. Hardware Parts

2. Software Parts

The following image shows the interfacing diagram of the LCD with the Arduino board. In this diagram, we are powering the LCD display through the Arduino.

The VEE pin is connected to the output of the potentiometer and using the potentiometer we are adjusting the contrast of the display but if you do not have a potentiometer you can connect this VEE pin directly to the 3.3 pin of the Arduino board.

The LED- and LED + pins are connected to the GND and VCC pins of the Arduino and the data pins DB4, DB5, DB6 and DB7 are connected to the Arduino board of digital pins 5, 4, 3 and 2.

RS and E are interfered with D11 and D12, while R / W pins are connected to GND pins to enable write mode.

{916} i2c LCD configuration with Arduino UNO in Tinkercad
{916} i2c LCD configuration with Arduino UNO in Tinkercad

Pinout Details of 16 * 2 LCD Display

VCC and GND

The LCD display powered through the Arduino with the help of these pins.

VEE

The brightness of the LCD display can be adjusted using this pin.

LED +, LED-

These pins used to power the LCD display’s backlight and should be connected to VCC and GND pin of the Arduino.

RS (Register Select)

The LCD consists of two registers, a data register and a command register. These are special-purpose registers.

If we make this pin HIGH and put the data on the data line then that data is accepted as the data to be displayed by the LCD. If we make this pin LOW then that data is accepted as a command to the LCD.

R/W

R/W pin is an active-high pin when the input on this pin is low, the LCD performs in a write operation. If the input is high, the LCD performs the read operation

DB0- DB7

Data or commands are given to the LCD display through these pins.

EN (Enable)

As the name itself suggests, This pin enables the LCD module.

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ế
Tutorial LCD 2x16 Arduino Lengkap
Tutorial LCD 2×16 Arduino Lengkap

Adjusting the contrast of the LCD

We can adjust the contrast of the LCD by adjusting the voltage input at the Vo pin. We are using a potentiometer because in that way we can easily fine tune the contrast, by adjusting input voltage from 0 to 5V.

Can I use the LCD without Potentiometer?

Yes, in case we don’t have a potentiometer, we can still adjust the LCD contrast by using a voltage divider made out of two resistors. Using the voltage divider we need to set the voltage value between 0 and 5V in order to get a good contrast on the display. I found that voltage of around 1V worked worked great for my LCD. I used 1K and 220 ohm resistor to get a good contrast.

There’s also another way of adjusting the LCD contrast, and that’s by supplying a PWM signal from the Arduino to the Vo pin of the LCD. We can connect the Vo pin to any Arduino PWM capable pin, and in the setup section, we can use the following line of code:


analogWrite(11,100); // Generate PWM signal at pin D11, value of 100 (out of 255)

Code language: Arduino (arduino)

It will generate PWM signal at pin D11, with value of 100 out of 255, which translated into voltage from 0 to 5V, it will be around 2V input at the Vo LCD pin.

Why Use The LCD In The Arduino Project?

When designing an embedded system, each process of the system requires continuous monitoring until the process is finished, in such cases, we should use an LCD in our project. Using the LCD in the project brings the project to life for the user and helps him with the appropriate information he is seeking.

The most commonly used system in which we see LCD the most is the coffee making machine. The LCD on the coffee grinder displays the amount of coffee available in the machine.

Apart from this, it also helps the user to choose the preferred option.

If we check in the market then different types of displays are also available in the market like seven-segment display, TFT LCD, OLED and more. But if we compare the price-wise then using 16 * 2 LCD is the cheapest option.

TFT Display and Arduino: Setup and Programming | How to connect LCD to arduino 1.8\
TFT Display and Arduino: Setup and Programming | How to connect LCD to arduino 1.8\” 2.8\” 3.5\”

What is LCD Character Display?

An LCD character display is a unique type of display that can only output individual ASCII characters with fixed size. Using these individual characters then we can form a text.

If we take a closer look at the display we can notice that there are small rectangular areas composed of 5×8 pixels grid. Each pixel can light up individually, and so we can generate characters within each grid.

The number of the rectangular areas define the size of the LCD. The most popular LCD is the 16×2 LCD, which has two rows with 16 rectangular areas or characters. Of course, there are other sizes like 16×1, 16×4, 20×4 and so on, but they all work on the same principle. Also, these LCDs can have different background and text color.

Circuit

Note that this circuit was originally designed for the Arduino UNO. As the Arduino is communicating with the display using SPI, pin 11 & 12 will change depending on what board you are using. For example, on a MKR WiFi 1010, the SPI bus is attached to pin 8 & 11.

Before wiring the LCD screen to your Arduino board we suggest to solder a pin header strip to the 14 (or 16) pin count connector of the LCD screen, as you can see in the image further up.

To wire your LCD screen to your board, connect the following pins:

  • LCD RS pin to digital pin 12
  • LCD Enable pin to digital pin 11
  • LCD D4 pin to digital pin 5
  • LCD D5 pin to digital pin 4
  • LCD D6 pin to digital pin 3
  • LCD D7 pin to digital pin 2
  • LCD R/W pin to GND
  • LCD VSS pin to GND
  • LCD VCC pin to 5V
  • LCD LED+ to 5V through a 220 ohm resistor
  • LCD LED- to GND

Additionally, wire a 10k potentiometer to +5V and GND, with it’s wiper (output) to LCD screens VO pin (pin3).

Top 20 Arduino LCD Animation Projects || animation lcd 16x2 || LCD Custom Animation Project
Top 20 Arduino LCD Animation Projects || animation lcd 16×2 || LCD Custom Animation Project

How to Connect Arduino to LCD – Wiring Diagram

Here’s how we need to connect the 16×2 LCD display to an Arduino board.

We will use just 6 digital input pins from the Arduino Board. The LCD’s registers from D4 to D7 will be connected to Arduino’s digital pins from 4 to 7. The Enable pin will be connected to pin number 2 and the RS pin will be connected to pin number 1. The R/W pin will be connected to Ground and the Vo pin will be connected to the potentiometer middle pin.

You can get these components from any of the sites below:

  • 16×2 Character LCD…………………….. Amazon / Banggood / AliExpress
  • Potentiometer ……………………………. Amazon / Banggood / AliExpress
  • Arduino Board …………………………… Amazon / Banggood / AliExpress
  • Breadboard and Jump Wires ……… Amazon / Banggood / AliExpress

Disclosure: These are affiliate links. As an Amazon Associate I earn from qualifying purchases.

Schematic

Hello World Example

This example sketch prints

to the LCD and shows the time in seconds since the Arduino was reset.

Hello World!

1/*2 LiquidCrystal Library – Hello World34 Demonstrates the use a 16×2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.89 This sketch prints “Hello World!” to the LCD10 and shows the time.1112 The circuit:13 * LCD RS pin to digital pin 1214 * LCD Enable pin to digital pin 1115 * LCD D4 pin to digital pin 516 * LCD D5 pin to digital pin 417 * LCD D6 pin to digital pin 318 * LCD D7 pin to digital pin 219 * LCD R/W pin to ground20 * LCD VSS pin to ground21 * LCD VCC pin to 5V22 * 10K resistor:23 * ends to +5V and ground24 * wiper to LCD VO pin (pin 3)2526 Library originally added 18 Apr 200827 by David A. Mellis28 library modified 5 Jul 200929 by Limor Fried (http://www.ladyada.net)30 example added 9 Jul 200931 by Tom Igoe32 modified 22 Nov 201033 by Tom Igoe34 modified 7 Nov 201635 by Arturo Guadalupi3637 This example code is in the public domain.3839 https://docs.arduino.cc/learn/electronics/lcd-displays4041*/4243// include the library code:44#include 4546// initialize the library by associating any needed LCD interface pin47// with the arduino pin number it is connected to48const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;49LiquidCrystal lcd(rs, en, d4, d5, d6, d7);5051void setup() {52 // set up the LCD’s number of columns and rows:53 lcd.begin(16, 2);54 // Print a message to the LCD.55 lcd.print(“hello, world!”);56}5758void loop() {59 // set the cursor to column 0, line 160 // (note: line 1 is the second row, since counting begins with 0):61 lcd.setCursor(0, 1);62 // print the number of seconds since reset:63 lcd.print(millis() / 1000);64}

Autoscroll Example

This example sketch shows how to use the

and

autoscroll()

methods to move all the text on the display left or right.

noAutoscroll()

  • moves all the text one space to the left each time a letter is added


    autoscroll()

  • turns scrolling off


    noAutoscroll()

This sketch prints the characters

to

with autoscroll off, then moves the cursor to the bottom right, turns autoscroll on, and prints them again.

1/*23 LiquidCrystal Library – Autoscroll45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch demonstrates the use of the autoscroll()1415 and noAutoscroll() functions to make new text scroll or not.1617 The circuit:1819 * LCD RS pin to digital pin 122021 * LCD Enable pin to digital pin 112223 * LCD D4 pin to digital pin 52425 * LCD D5 pin to digital pin 42627 * LCD D6 pin to digital pin 32829 * LCD D7 pin to digital pin 23031 * LCD R/W pin to ground3233 * 10K resistor:3435 * ends to +5V and ground3637 * wiper to LCD VO pin (pin 3)3839 Library originally added 18 Apr 20084041 by David A. Mellis4243 library modified 5 Jul 20094445 by Limor Fried (http://www.ladyada.net)4647 example added 9 Jul 20094849 by Tom Igoe5051 modified 22 Nov 20105253 by Tom Igoe5455 modified 7 Nov 20165657 by Arturo Guadalupi5859 This example code is in the public domain.6061 http://www.arduino.cc/en/Tutorial/LiquidCrystalAutoscroll6263*/6465// include the library code:66#include 6768// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to7071const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7273LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7475void setup() {7677 // set up the LCD’s number of columns and rows:7879 lcd.begin(16, 2);80}8182void loop() {8384 // set the cursor to (0,0):8586 lcd.setCursor(0, 0);8788 // print from 0 to 9:8990 for (int thisChar = 0; thisChar < 10; thisChar++) {9192 lcd.print(thisChar);9394 delay(500);9596 }9798 // set the cursor to (16,1):99100 lcd.setCursor(16, 1);101102 // set the display to automatically scroll:103104 lcd.autoscroll();105106 // print from 0 to 9:107108 for (int thisChar = 0; thisChar < 10; thisChar++) {109110 lcd.print(thisChar);111112 delay(500);113114 }115116 // turn off automatic scrolling117118 lcd.noAutoscroll();119120 // clear screen for the next loop:121122 lcd.clear();123}

Blink Example

This example sketch shows how to use the

and

blink()

methods to blink a block-style cursor.

noBlink()

1/*23 LiquidCrystal Library – Blink45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch prints “Hello World!” to the LCD and makes the1415 cursor block blink.1617 The circuit:1819 * LCD RS pin to digital pin 122021 * LCD Enable pin to digital pin 112223 * LCD D4 pin to digital pin 52425 * LCD D5 pin to digital pin 42627 * LCD D6 pin to digital pin 32829 * LCD D7 pin to digital pin 23031 * LCD R/W pin to ground3233 * 10K resistor:3435 * ends to +5V and ground3637 * wiper to LCD VO pin (pin 3)3839 Library originally added 18 Apr 20084041 by David A. Mellis4243 library modified 5 Jul 20094445 by Limor Fried (http://www.ladyada.net)4647 example added 9 Jul 20094849 by Tom Igoe5051 modified 22 Nov 20105253 by Tom Igoe5455 modified 7 Nov 20165657 by Arturo Guadalupi5859 This example code is in the public domain.6061 http://www.arduino.cc/en/Tutorial/LiquidCrystalBlink6263*/6465// include the library code:66#include 6768// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to7071const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7273LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7475void setup() {7677 // set up the LCD’s number of columns and rows:7879 lcd.begin(16, 2);8081 // Print a message to the LCD.8283 lcd.print(“hello, world!”);84}8586void loop() {8788 // Turn off the blinking cursor:8990 lcd.noBlink();9192 delay(3000);9394 // Turn on the blinking cursor:9596 lcd.blink();9798 delay(3000);99}

Cursor

This example sketch shows how to use the

and

cursor()

methods to control an underscore-style cursor.

noCursor()

1/*23 LiquidCrystal Library – Cursor45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch prints “Hello World!” to the LCD and1415 uses the cursor() and noCursor() methods to turn1617 on and off the cursor.1819 The circuit:2021 * LCD RS pin to digital pin 122223 * LCD Enable pin to digital pin 112425 * LCD D4 pin to digital pin 52627 * LCD D5 pin to digital pin 42829 * LCD D6 pin to digital pin 33031 * LCD D7 pin to digital pin 23233 * LCD R/W pin to ground3435 * 10K resistor:3637 * ends to +5V and ground3839 * wiper to LCD VO pin (pin 3)4041 Library originally added 18 Apr 20084243 by David A. Mellis4445 library modified 5 Jul 20094647 by Limor Fried (http://www.ladyada.net)4849 example added 9 Jul 20095051 by Tom Igoe5253 modified 22 Nov 20105455 by Tom Igoe5657 modified 7 Nov 20165859 by Arturo Guadalupi6061 This example code is in the public domain.6263 http://www.arduino.cc/en/Tutorial/LiquidCrystalCursor6465*/6667// include the library code:68#include 6970// initialize the library by associating any needed LCD interface pin71// with the arduino pin number it is connected to7273const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7475LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7677void setup() {7879 // set up the LCD’s number of columns and rows:8081 lcd.begin(16, 2);8283 // Print a message to the LCD.8485 lcd.print(“hello, world!”);86}8788void loop() {8990 // Turn off the cursor:9192 lcd.noCursor();9394 delay(500);9596 // Turn on the cursor:9798 lcd.cursor();99100 delay(500);101}

Display Example

This example sketch shows how to use the

and

display()

methods to turn on and off the display. The text to be displayed will still be preserved when you use noDisplay() so it’s a quick way to blank the display without losing everything on it.

noDisplay()

1/*2 LiquidCrystal Library – display() and noDisplay()34 Demonstrates the use a 16×2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.89 This sketch prints “Hello World!” to the LCD and uses the10 display() and noDisplay() functions to turn on and off11 the display.1213 The circuit:14 * LCD RS pin to digital pin 1215 * LCD Enable pin to digital pin 1116 * LCD D4 pin to digital pin 517 * LCD D5 pin to digital pin 418 * LCD D6 pin to digital pin 319 * LCD D7 pin to digital pin 220 * LCD R/W pin to ground21 * 10K resistor:22 * ends to +5V and ground23 * wiper to LCD VO pin (pin 3)2425 Library originally added 18 Apr 200826 by David A. Mellis27 library modified 5 Jul 200928 by Limor Fried (http://www.ladyada.net)29 example added 9 Jul 200930 by Tom Igoe31 modified 22 Nov 201032 by Tom Igoe33 modified 7 Nov 201634 by Arturo Guadalupi3536 This example code is in the public domain.3738 http://www.arduino.cc/en/Tutorial/LiquidCrystalDisplay3940*/4142// include the library code:43#include 4445// initialize the library by associating any needed LCD interface pin46// with the arduino pin number it is connected to47const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;48LiquidCrystal lcd(rs, en, d4, d5, d6, d7);4950void setup() {51 // set up the LCD’s number of columns and rows:52 lcd.begin(16, 2);53 // Print a message to the LCD.54 lcd.print(“hello, world!”);55}5657void loop() {58 // Turn off the display:59 lcd.noDisplay();60 delay(500);61 // Turn on the display:62 lcd.display();63 delay(500);64}

Scroll Example

This example sketch shows how to use the

and

scrollDisplayLeft()

methods to reverse the direction the text is flowing. It prints “Hello World!”, scrolls it offscreen to the left, then offscreen to the right, then back to home.

scrollDisplayRight()

1/*2 LiquidCrystal Library – scrollDisplayLeft() and scrollDisplayRight()34 Demonstrates the use a 16×2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.89 This sketch prints “Hello World!” to the LCD and uses the10 scrollDisplayLeft() and scrollDisplayRight() methods to scroll11 the text.1213 The circuit:14 * LCD RS pin to digital pin 1215 * LCD Enable pin to digital pin 1116 * LCD D4 pin to digital pin 517 * LCD D5 pin to digital pin 418 * LCD D6 pin to digital pin 319 * LCD D7 pin to digital pin 220 * LCD R/W pin to ground21 * 10K resistor:22 * ends to +5V and ground23 * wiper to LCD VO pin (pin 3)2425 Library originally added 18 Apr 200826 by David A. Mellis27 library modified 5 Jul 200928 by Limor Fried (http://www.ladyada.net)29 example added 9 Jul 200930 by Tom Igoe31 modified 22 Nov 201032 by Tom Igoe33 modified 7 Nov 201634 by Arturo Guadalupi3536 This example code is in the public domain.3738 http://www.arduino.cc/en/Tutorial/LiquidCrystalScroll3940*/4142// include the library code:43#include 4445// initialize the library by associating any needed LCD interface pin46// with the arduino pin number it is connected to47const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;48LiquidCrystal lcd(rs, en, d4, d5, d6, d7);4950void setup() {51 // set up the LCD’s number of columns and rows:52 lcd.begin(16, 2);53 // Print a message to the LCD.54 lcd.print(“hello, world!”);55 delay(1000);56}5758void loop() {59 // scroll 13 positions (string length) to the left60 // to move it offscreen left:61 for (int positionCounter = 0; positionCounter < 13; positionCounter++) {62 // scroll one position left:63 lcd.scrollDisplayLeft();64 // wait a bit:65 delay(150);66 }6768 // scroll 29 positions (string length + display length) to the right69 // to move it offscreen right:70 for (int positionCounter = 0; positionCounter < 29; positionCounter++) {71 // scroll one position right:72 lcd.scrollDisplayRight();73 // wait a bit:74 delay(150);75 }7677 // scroll 16 positions (display length + string length) to the left78 // to move it back to center:79 for (int positionCounter = 0; positionCounter < 16; positionCounter++) {80 // scroll one position left:81 lcd.scrollDisplayLeft();82 // wait a bit:83 delay(150);84 }8586 // delay at the end of the full loop:87 delay(1000);8889}

Serial to Display Example

This example sketch accepts serial input from a host computer and displays it on the LCD. To use it, upload the sketch, then open the Serial Monitor and type some characters and click Send. The text will appear on your LCD.

1/*2 LiquidCrystal Library – Serial Input34 Demonstrates the use a 16×2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.89 This sketch displays text sent over the serial port10 (e.g. from the Serial Monitor) on an attached LCD.1112 The circuit:13 * LCD RS pin to digital pin 1214 * LCD Enable pin to digital pin 1115 * LCD D4 pin to digital pin 516 * LCD D5 pin to digital pin 417 * LCD D6 pin to digital pin 318 * LCD D7 pin to digital pin 219 * LCD R/W pin to ground20 * 10K resistor:21 * ends to +5V and ground22 * wiper to LCD VO pin (pin 3)2324 Library originally added 18 Apr 200825 by David A. Mellis26 library modified 5 Jul 200927 by Limor Fried (http://www.ladyada.net)28 example added 9 Jul 200929 by Tom Igoe30 modified 22 Nov 201031 by Tom Igoe32 modified 7 Nov 201633 by Arturo Guadalupi3435 This example code is in the public domain.3637 http://www.arduino.cc/en/Tutorial/LiquidCrystalSerialDisplay3839*/4041// include the library code:42#include 4344// initialize the library by associating any needed LCD interface pin45// with the arduino pin number it is connected to46const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;47LiquidCrystal lcd(rs, en, d4, d5, d6, d7);4849void setup() {50 // set up the LCD’s number of columns and rows:51 lcd.begin(16, 2);52 // initialize the serial communications:53 Serial.begin(9600);54}5556void loop() {57 // when characters arrive over the serial port…58 if (Serial.available()) {59 // wait a bit for the entire message to arrive60 delay(100);61 // clear the screen62 lcd.clear();63 // read all the available characters64 while (Serial.available() > 0) {65 // display each character to the LCD66 lcd.write(Serial.read());67 }68 }69}

Set Cursor Example

This example sketch shows how to use the

method to reposition the cursor. To move the cursor, just call

setCursor()

with a row and column position. For example, for a 2×16 display:

setCursor()

1lcd.setCursor(0, 0); // top left2lcd.setCursor(15, 0); // top right3lcd.setCursor(0, 1); // bottom left4lcd.setCursor(15, 1); // bottom right

Here is the full example:

1/*23 LiquidCrystal Library – setCursor45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch prints to all the positions of the LCD using the1415 setCursor() method:1617 The circuit:1819 * LCD RS pin to digital pin 122021 * LCD Enable pin to digital pin 112223 * LCD D4 pin to digital pin 52425 * LCD D5 pin to digital pin 42627 * LCD D6 pin to digital pin 32829 * LCD D7 pin to digital pin 23031 * LCD R/W pin to ground3233 * 10K resistor:3435 * ends to +5V and ground3637 * wiper to LCD VO pin (pin 3)3839 Library originally added 18 Apr 20084041 by David A. Mellis4243 library modified 5 Jul 20094445 by Limor Fried (http://www.ladyada.net)4647 example added 9 Jul 20094849 by Tom Igoe5051 modified 22 Nov 20105253 by Tom Igoe5455 modified 7 Nov 20165657 by Arturo Guadalupi5859 This example code is in the public domain.6061 http://www.arduino.cc/en/Tutorial/LiquidCrystalSetCursor6263*/6465// include the library code:66#include 6768// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to7071const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7273LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7475// these constants won’t change. But you can change the size of76// your LCD using them:7778const int numRows = 2;7980const int numCols = 16;8182void setup() {8384 // set up the LCD’s number of columns and rows:8586 lcd.begin(numCols, numRows);87}8889void loop() {9091 // loop from ASCII ‘a’ to ASCII ‘z’:9293 for (int thisLetter = ‘a’; thisLetter <= ‘z’; thisLetter++) {9495 // loop over the columns:9697 for (int thisRow = 0; thisRow < numRows; thisRow++) {9899 // loop over the rows:100101 for (int thisCol = 0; thisCol < numCols; thisCol++) {102103 // set the cursor position:104105 lcd.setCursor(thisCol, thisRow);106107 // print the letter:108109 lcd.write(thisLetter);110111 delay(200);112113 }114115 }116117 }118}

Text Direction Example

This example sketch shows how to use the

and

leftToRight()

methods. These methods control which way text flows from the cursor.

rightToLeft()

  • causes text to flow to the left from the cursor, as if the display is right-justified.


    rightToLeft()

  • causes text to flow to the right from the cursor, as if the display is left-justified.


    leftToRight()

This sketch prints

through

right to left, then

through

left to right, then

through

right to left again.

1/*23 LiquidCrystal Library – TextDirection45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch demonstrates how to use leftToRight() and rightToLeft()1415 to move the cursor.1617 The circuit:1819 * LCD RS pin to digital pin 122021 * LCD Enable pin to digital pin 112223 * LCD D4 pin to digital pin 52425 * LCD D5 pin to digital pin 42627 * LCD D6 pin to digital pin 32829 * LCD D7 pin to digital pin 23031 * LCD R/W pin to ground3233 * 10K resistor:3435 * ends to +5V and ground3637 * wiper to LCD VO pin (pin 3)3839 Library originally added 18 Apr 20084041 by David A. Mellis4243 library modified 5 Jul 20094445 by Limor Fried (http://www.ladyada.net)4647 example added 9 Jul 20094849 by Tom Igoe5051 modified 22 Nov 20105253 by Tom Igoe5455 modified 7 Nov 20165657 by Arturo Guadalupi5859 This example code is in the public domain.6061 http://www.arduino.cc/en/Tutorial/LiquidCrystalTextDirection6263*/6465// include the library code:66#include 6768// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to7071const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7273LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7475int thisChar = ‘a’;7677void setup() {7879 // set up the LCD’s number of columns and rows:8081 lcd.begin(16, 2);8283 // turn on the cursor:8485 lcd.cursor();86}8788void loop() {8990 // reverse directions at ‘m’:9192 if (thisChar == ‘m’) {9394 // go right for the next letter9596 lcd.rightToLeft();9798 }99100 // reverse again at ‘s’:101102 if (thisChar == ‘s’) {103104 // go left for the next letter105106 lcd.leftToRight();107108 }109110 // reset at ‘z’:111112 if (thisChar > ‘z’) {113114 // go to (0,0):115116 lcd.home();117118 // start again at 0119120 thisChar = ‘a’;121122 }123124 // print the character125126 lcd.write(thisChar);127128 // wait a second:129130 delay(1000);131132 // increment the letter:133134 thisChar++;135}

Custom Character

This example demonstrates how to add custom characters on an LCD display.

Note that this example requires an additional potentiometer:

  • Outer pins connected to 5V and GND.
  • Inner pin (wiper) connected to A0.

This potentiometer controls the

variable.

delayTime

1/*2 LiquidCrystal Library – Custom Characters34 Demonstrates how to add custom characters on an LCD display.5 The LiquidCrystal library works with all LCD displays that are6 compatible with the Hitachi HD44780 driver. There are many of7 them out there, and you can usually tell them by the 16-pin interface.89 This sketch prints “I

Arduino!” and a little dancing man10 to the LCD.1112 The circuit:13 * LCD RS pin to digital pin 1214 * LCD Enable pin to digital pin 1115 * LCD D4 pin to digital pin 516 * LCD D5 pin to digital pin 417 * LCD D6 pin to digital pin 318 * LCD D7 pin to digital pin 219 * LCD R/W pin to ground20 * 10K potentiometer:21 * ends to +5V and ground22 * wiper to LCD VO pin (pin 3)23 * 10K poterntiometer on pin A02425 created 21 Mar 201126 by Tom Igoe27 modified 11 Nov 201328 by Scott Fitzgerald29 modified 7 Nov 201630 by Arturo Guadalupi3132 Based on Adafruit’s example at33 https://github.com/adafruit/SPI_VFD/blob/master/examples/createChar/createChar.pde3435 This example code is in the public domain.36 https://docs.arduino.cc/learn/electronics/lcd-displays#custom-character3738 Also useful:39 http://icontexto.com/charactercreator/4041*/4243// include the library code:44#include 4546// initialize the library by associating any needed LCD interface pin47// with the arduino pin number it is connected to48const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;49LiquidCrystal lcd(rs, en, d4, d5, d6, d7);5051// make some custom characters:52byte heart[8] = {53 0b00000,54 0b01010,55 0b11111,56 0b11111,57 0b11111,58 0b01110,59 0b00100,60 0b0000061};6263byte smiley[8] = {64 0b00000,65 0b00000,66 0b01010,67 0b00000,68 0b00000,69 0b10001,70 0b01110,71 0b0000072};7374byte frownie[8] = {75 0b00000,76 0b00000,77 0b01010,78 0b00000,79 0b00000,80 0b00000,81 0b01110,82 0b1000183};8485byte armsDown[8] = {86 0b00100,87 0b01010,88 0b00100,89 0b00100,90 0b01110,91 0b10101,92 0b00100,93 0b0101094};9596byte armsUp[8] = {97 0b00100,98 0b01010,99 0b00100,100 0b10101,101 0b01110,102 0b00100,103 0b00100,104 0b01010105};106107void setup() {108 // initialize LCD and set up the number of columns and rows:109 lcd.begin(16, 2);110111 // create a new character112 lcd.createChar(0, heart);113 // create a new character114 lcd.createChar(1, smiley);115 // create a new character116 lcd.createChar(2, frownie);117 // create a new character118 lcd.createChar(3, armsDown);119 // create a new character120 lcd.createChar(4, armsUp);121122 // set the cursor to the top left123 lcd.setCursor(0, 0);124125 // Print a message to the lcd.126 lcd.print(“I “);127 lcd.write(byte(0)); // when calling lcd.write() ‘0’ must be cast as a byte128 lcd.print(” Arduino! “);129 lcd.write((byte)1);130131}132133void loop() {134 // read the potentiometer on A0:135 int sensorReading = analogRead(A0);136 // map the result to 200 – 1000:137 int delayTime = map(sensorReading, 0, 1023, 200, 1000);138 // set the cursor to the bottom row, 5th position:139 lcd.setCursor(4, 1);140 // draw the little man, arms down:141 lcd.write(3);142 delay(delayTime);143 lcd.setCursor(4, 1);144 // draw him arms up:145 lcd.write(4);146 delay(delayTime);147}

Suggested changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.

In this Arduino tutorial we will learn how to connect and use an LCD (Liquid Crystal Display) with Arduino. LCD displays like these are very popular and broadly used in many electronics projects because they are great for displaying simple information, like sensors data, while being very affordable.

I’ve already used them in several of my Arduino projects, and you can check them out here:

  • Arduino Security and Alarm System Project
  • DIY Vending Machine – Arduino based Mechatronics Project
  • Arduino Range Measurer and Digital Spirit Level Project

You can watch the following video or read the written tutorial below. It includes everything you need to know about using an LCD character display with Arduino, such as, LCD pinout, wiring diagram and several example codes.

Arduino TFT LCD Touch Screen Tutorial
Arduino TFT LCD Touch Screen Tutorial

How to Generate and Display Custom Characters on the LCD

In addition to the ASCII characters, with the LiquidCrystal library it is also possible to generate and display custom characters on the LCD.

We can specify the appearance of each character by an array of 8 bytes. Here’s an example code:


byte heart[8] = { // Array of bytes B00000, // B stands for binary formatter and the five numbers are the pixels B01010, B11111, B11111, B01110, B00100, B00000, B00000 }; byte smile[8] = { B00000, B00000, B01010, B00000, B10001, B01110, B00000, B00000 }; byte lock[8] = { B01110, B10001, B10001, B11111, B11011, B11011, B11111, B00000 }; byte character[8] = { B11111, B10101, B11111, B01010, B01110, B11111, B01110, B01110 }; LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) void setup() { lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display lcd.createChar(0, heart); // Create a custom character lcd.createChar(1, smile); lcd.createChar(2, lock); lcd.createChar(3, character); // Clears the LCD screen lcd.clear(); // Print a message to the LCD lcd.print("Custom Character"); } void loop() { lcd.setCursor(1, 1); lcd.write(byte(0)); // Display the custom character 0, the heart lcd.setCursor(5, 1); lcd.write(byte(1)); lcd.setCursor(9, 1); lcd.write(byte(2)); lcd.setCursor(13, 1); lcd.write(byte(3)); }

Code language: Arduino (arduino)

We can notice how we can specify the appearance of the character by changing the 0s into 1s within the 5×8 pixels grid.

In the setup we have to create the custom character using the createChar() function.


lcd.createChar(0, heart); // Create a custom character

Code language: Arduino (arduino)

The first parameter in this function is a number between 0 and 7, or we have to reserve one of the 8 supported custom characters. The second parameter is the name of the array of bytes.

We write the custom character to the display using the write() function and as a parameter we use the number of the character.


lcd.write(byte(0)); // Display the custom character 0, or the heart

Code language: Arduino (arduino)

See also: Arduino Touch Screen Tutorial | TFT LCD

Conclusion

In this blog, we have discussed the basics of LCD modules, how to print characters on an LCD module, use of the lcd.print () function, and learned how to scroll through text on an LCD.

I hope you liked my presentation, if you have any questions, please let us know in the comments section and we will answer them for sure.

The Arduino Reference text is licensed under a

Creative Commons Attribution-Share Alike 3.0 License

.

Find anything that can be improved?

Suggest corrections and new documentation via GitHub.

Doubts on how to use Github? Learn everything you need to know in

this tutorial

.

The Arduino Reference text is licensed under a

Creative Commons Attribution-Share Alike 3.0 License

.

Find anything that can be improved?

Suggest corrections and new documentation via GitHub.

Doubts on how to use Github? Learn everything you need to know in

this tutorial

.

Xin chào các bạn!

Nay mình xin share ít bộ code để tạo các hiệu ứng mới cho LCD 16X2 với Arduino Uno

Các bạn cắm mạch kết nối giữa Arduino với LCD16x2 như phía dưới hình .

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

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

In this Arduino LCD tutorial, we will learn how to connect an LCD (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.

※ NOTE THAT:

If you want to simplify the wiring, You can use LCD I2C instead. See LCD I2C tutorial

LCD Arduino Code

Here’s a simple code through which we can explain the working principle of the Liquid Crystal library. This is the code of the first example from the video:


/* * Arduino LCD Tutorial * * Crated by Dejan Nedelkovski, * www.HowToMechatronics.com * */ LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7) void setup() { lcd.begin(16,2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display } } void loop() { lcd.print("Arduino"); // Prints "Arduino" on the LCD delay(3000); // 3 seconds delay lcd.setCursor(2,1); // Sets the location at which subsequent text written to the LCD will be displayed lcd.print("LCD Tutorial"); delay(3000); lcd.clear(); // Clears the display lcd.blink(); //Displays the blinking LCD cursor delay(4000); lcd.setCursor(7,1); delay(3000); lcd.noBlink(); // Turns off the blinking LCD cursor lcd.cursor(); // Displays an underscore (line) at the position to which the next character will be written delay(4000); lcd.noCursor(); // Hides the LCD cursor lcd.clear(); // Clears the LCD screen }

Code language: Arduino (arduino)

Code description:

First thing we need to do is it insert the Liquid Crystal Library. We can do that like this: Sketch > Include Library > Liquid Crystal. Then we have to create an LC object. The parameters of this object should be the numbers of the Digital Input pins of the Arduino Board respectively to the LCD’s pins as follow: (RS, Enable, D4, D5, D6, D7). In the setup we have to initialize the interface to the LCD and specify the dimensions of the display using the begin() function.

In the loop we write our main program. Using the print() function we print on the LCD.


lcd.print("Arduino"); // Prints "Arduino" on the LCD

Code language: Arduino (arduino)

The setCursor() function is used for setting the location at which subsequent text written to the LCD will be displayed.


lcd.setCursor(2,1); // Sets the location at which subsequent text written to the LCD will be displayed

Code language: Arduino (arduino)

The blink() function is used for displaying a blinking cursor and the noBlink() function for turning off.


lcd.blink(); //Displays the blinking LCD cursor

Code language: Arduino (arduino)

The cursor() function is used for displaying underscore cursor and the noCursor() function for turning off. Using the clear() function we can clear the LCD screen.


lcd.clear(); // Clears the LCD screen

Code language: Arduino (arduino)

Arduino LCD Tutorial ST7920
Arduino LCD Tutorial ST7920

LCD Display Options

There are 19 different functions in the LiquidCrystal library available for us to use. These functions do things like change the position of the text, move text across the screen, or make the display turn on or off. What follows is a short description of each function, and how to use it in a program.


LiquidCrystal()

The

LiquidCrystal()

function sets the pins the Arduino uses to connect to the LCD. You can use any of the Arduino’s digital pins to control the LCD. Just put the Arduino pin numbers inside the parentheses in this order:


LiquidCrystal(RS, E, D4, D5, D6, D7)

RS, E, D4, D5, D6, D7 are the LCD pins.

For example, say you want LCD pin D7 to connect to Arduino pin 12. Just put “12” in place of D7 in the function like this:


LiquidCrystal(RS, E, D4, D5, D6, 12)

This function needs to be placed before the

void setup()

section of the program.


lcd.begin()

This function sets the dimensions of the LCD. It needs to be placed before any other LiquidCrystal function in the

void setup()

section of the program. The number of rows and columns are specified as

lcd.begin(columns, rows)

. For a 16×2 LCD, you would use

lcd.begin(16, 2)

, and for a 20×4 LCD you would use

lcd.begin(20, 4)

.


lcd.clear()

This function clears any text or data already displayed on the LCD. If you use

lcd.clear()

with

lcd.print()

and the

delay()

function in the

void loop()

section, you can make a simple blinking text program:


#include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); } void loop() { lcd.print("hello, world!"); delay(500); lcd.clear(); delay(500); }


lcd.home()

This function places the cursor in the upper left hand corner of the screen, and prints any subsequent text from that position. For example, this code replaces the first three letters of “hello world!” with X’s:


#include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); lcd.print("hello, world!"); } void loop() { lcd.home(); lcd.print("XXX"); }


lcd.setCursor()

Similar, but more useful than

lcd.home()

is

lcd.setCursor()

. This function places the cursor (and any printed text) at any position on the screen. It can be used in the

void setup()

or

void loop()

section of your program.

The cursor position is defined with

lcd.setCursor(column, row)

. The column and row coordinates start from zero (0-15 and 0-1 respectively). For example, using

lcd.setCursor(2, 1)

in the

void setup()

section of the “hello, world!” program above prints “hello, world!” to the lower line and shifts it to the right two spaces:


#include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); lcd.setCursor(2, 1); lcd.print("hello, world!"); } void loop() { }


lcd.write()

You can use this function to write different types of data to the LCD, for example the reading from a temperature sensor, or the coordinates from a GPS module. You can also use it to print custom characters that you create yourself (more on this below). Use

lcd.write()

in the

void setup()

or

void loop()

section of your program.


lcd.print()

This function is used to print text to the LCD. It can be used in the

void setup()

section or the

void loop()

section of the program.

To print letters and words, place quotation marks (” “) around the text. For example, to print hello, world!, use

lcd.print("hello, world!")

.

To print numbers, no quotation marks are necessary. For example, to print 123456789, use

lcd.print(123456789)

.


lcd.print()

can print numbers in decimal, binary, hexadecimal, and octal bases. For example:


  • lcd.print(100, DEC)

    prints “100”;

  • lcd.print(100, BIN)

    prints “1100100”

  • lcd.print(100, HEX)

    prints “64”

  • lcd.print(100, OCT)

    prints “144”


lcd.cursor()

This function creates a visible cursor. The cursor is a horizontal line placed below the next character to be printed to the LCD.

The function

lcd.noCursor()

turns the cursor off.

lcd.cursor()

and

lcd.noCursor()

can be used together in the

void loop()

section to make a blinking cursor similar to what you see in many text input fields:


#include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); lcd.print("hello, world!"); } void loop() { lcd.cursor(); delay(500); lcd.noCursor(); delay(500); }

This places a blinking cursor after the exclamation point in “hello, world!”

Cursors can be placed anywhere on the screen with the

lcd.setCursor()

function. This code places a blinking cursor directly below the exclamation point in “hello, world!”:


#include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); lcd.print("hello, world!"); } void loop() { lcd.setCursor(12, 1); lcd.cursor(); delay(500); lcd.setCursor(12, 1); lcd.noCursor(); delay(500); }


lcd.blink()

This function creates a block style cursor that blinks on and off at approximately 500 milliseconds per cycle. Use it in the

void loop()

section. The function

lcd.noBlink()

disables the blinking block cursor.


lcd.display()

This function turns on any text or cursors that have been printed to the LCD screen. The function

lcd.noDisplay()

turns off any text or cursors printed to the LCD, without clearing it from the LCD’s memory.

These two functions can be used together in the

void loop()

section to create a blinking text effect. This code will make the “hello, world!” text blink on and off:


#include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); lcd.print("hello, world!"); } void loop() { lcd.display(); delay(500); lcd.noDisplay(); delay(500); }


lcd.scrollDisplayLeft()

This function takes anything printed to the LCD and moves it to the left. It should be used in the

void loop()

section with a delay command following it. The function will move the text 40 spaces to the left before it loops back to the first character. This code moves the “hello, world!” text to the left, at a rate of one second per character:


#include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); lcd.print("hello, world!"); } void loop() { lcd.scrollDisplayLeft(); delay(1000); }

Text strings longer than 40 spaces will be printed to line 1 after the 40th position, while the start of the string will continue printing to line 0.


lcd.scrollDisplayRight()

This function behaves like

lcd.scrollDisplayLeft()

, but moves the text to the right.


lcd.autoscroll()

This function takes a string of text and scrolls it from right to left in increments of the character count of the string. For example, if you have a string of text that is 3 characters long, it will shift the text 3 spaces to the left with each step:


#include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); } void loop() { lcd.setCursor(0, 0); lcd.autoscroll(); lcd.print("ABC"); delay(500); }

Like the

lcd.scrollDisplay()

functions, the text can be up to 40 characters in length before repeating. At first glance, this function seems less useful than the

lcd.scrollDisplay()

functions, but it can be very useful for creating animations with custom characters.


lcd.noAutoscroll()


lcd.noAutoscroll()

turns the

lcd.autoscroll()

function off. Use this function before or after

lcd.autoscroll()

in the

void loop()

section to create sequences of scrolling text or animations.


lcd.rightToLeft()

This function sets the direction that text is printed to the screen. The default mode is from left to right using the command

lcd.leftToRight()

, but you may find some cases where it’s useful to output text in the reverse direction:


#include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); lcd.setCursor(12, 0); lcd.rightToLeft(); lcd.print("hello, world!"); } void loop() { }

This code prints the “hello, world!” text as “!dlrow ,olleh”. Unless you specify the placement of the cursor with

lcd.setCursor()

, the text will print from the (0, 1) position and only the first character of the string will be visible.


lcd.createChar()

This command allows you to create your own custom characters. Each character of a 16×2 LCD has a 5 pixel width and an 8 pixel height. Up to 8 different custom characters can be defined in a single program. To design your own characters, you’ll need to make a binary matrix of your custom character from an LCD character generator or map it yourself. This code creates a degree symbol (°):


#include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); byte customChar[8] = { 0b00110, 0b01001, 0b01001, 0b00110, 0b00000, 0b00000, 0b00000, 0b00000 }; void setup() { lcd.createChar(0, customChar); lcd.begin(16, 2); lcd.write((uint8_t)0); } void loop() { }

There are a lot of cool things you can make happen with these 16×2 LCDs! Try combining some of these functions and see what happens.

Here’s a video version of this tutorial so you can see what each function does on the LCD in real time:

If you found this article useful, subscribe via email to get notified when we publish of new posts! And as always, if you are having trouble with anything, just leave a comment and I’ll try to help you out.

Adjusting the contrast of the LCD

We can adjust the contrast of the LCD by adjusting the voltage input at the Vo pin. We are using a potentiometer because in that way we can easily fine tune the contrast, by adjusting input voltage from 0 to 5V.

Can I use the LCD without Potentiometer?

Yes, in case we don’t have a potentiometer, we can still adjust the LCD contrast by using a voltage divider made out of two resistors. Using the voltage divider we need to set the voltage value between 0 and 5V in order to get a good contrast on the display. I found that voltage of around 1V worked worked great for my LCD. I used 1K and 220 ohm resistor to get a good contrast.

There’s also another way of adjusting the LCD contrast, and that’s by supplying a PWM signal from the Arduino to the Vo pin of the LCD. We can connect the Vo pin to any Arduino PWM capable pin, and in the setup section, we can use the following line of code:


analogWrite(11,100); // Generate PWM signal at pin D11, value of 100 (out of 255)

Code language: Arduino (arduino)

It will generate PWM signal at pin D11, with value of 100 out of 255, which translated into voltage from 0 to 5V, it will be around 2V input at the Vo LCD pin.

Arduino TFT LCD Touch Screen Tutorial (2.8\
Arduino TFT LCD Touch Screen Tutorial (2.8\” ILI9341 Driver) also for ESP32

LCD Arduino Code


#include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); // sets the interfacing pins void setup() { lcd.begin(16, 2); // initializes the 16x2 LCD lcd.setCursor(0,0); //sets the cursor at row 0 column 0 lcd.print("LCD Tutorial"); // prints LCD Tutorial lcd.setCursor(5,1); //sets the cursor at row 1 column 5 lcd.print("HELLO WORLD"); // prints Robu.in } void loop() { // Your Code }

In this code, we are using the liquid crystal library, this library is easy to use and easy to understand. Here I have mentioned some methods which will help you in this project.


#include LiquidCrystal.h

This line of code initializes the library and makes the LiquidCrystal library available for the entire code.


lcd.begin()

This line of code initiates communication between the LCD and the Arduino. It also tells the Arduino the dimensions of the LCD.


lcd.print()

Using the above line of code, you can print letters on the screen.


lcd.setCursor()

To adjust the cursor on the screen. Where can you use this function? Suppose you do not want to start printing characters from the first position. In that case, you can use this method to skip the first position and start printing from the required position.

For more understanding of this library, we request you to check the documentation of this library.

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

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

Scrolling text example on 16×2 LCD and Arduino

In case we have a text with length greater than 16 characters, we can scroll the text using the scrollDisplayLeft() or scrollDisplayRight() function from the LiquidCrystal library.

Here’s an example code:


LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7) void setup() { lcd.begin(16, 2); lcd.print("Scrolling Text Example"); } void loop() { lcd.scrollDisplayLeft(); delay(500); }

Code language: Arduino (arduino)

We can choose whether the text will scroll left or right, using the scrollDisplayLeft() or scrollDisplayRight() functions. With the delay() function we can set the scrolling speed.

If you want more control over how the text is scrolling, you could also make the scrolling on your own using a “for” loop. Here’s an example:


LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LCD object. Parameters: (rs, enable, d4, d5, d6, d7) void setup() { lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display } void loop() { // scroll text to the right for (int i = 0; i <= 13; i++) { lcd.setCursor(i, 0); // Sets the location at which subsequent text written to the LCD will be displayed lcd.print("LCD"); delay(500); // 1 second delay lcd.clear(); // Write a character to the LCD } // scroll text to the left for (int i = 12; i >= 1; i--) { lcd.setCursor(i, 0); lcd.print("LCD"); delay(500); lcd.clear(); } }

Code language: Arduino (arduino)

I.Một số bộ code

1.1.Blink

Nhấp nháy chữ trên LCD:

//Thêm thư viện LiquidCrystal – nó có sẵn vì vậy bạn không cần cài thêm gì cả #include //Khởi tạo với các chân LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2);//Thông báo đây là LCD 1602 lcd.print(“Hello World!”);//In ra dòng chữ, bạn có thể chỉnh chữ lại tuỳ ý lcd.setCursor(0 , 1); lcd.print(“arduino.vn”); } void loop() { lcd.noDisplay();//Xoá màn hình hiển thị delay(500);//chờ 0,5 giây lcd.Display();//Hiển thị trở lại delay(500); }

Kết quả:

1.2.Tự động cuộn chữ

Chương trình tự dịch chuyển chữ ở dòng thứ 1 trên LCD khi bắt đầu có kí tự ở dòng thứ 2:

// Thêm thư viện #include //Khai báo các chân LCD LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { // Cấu hình hàng và cột LCD lcd.begin(16, 2); } void loop() { // Đưa con trỏ về vị trí (0,0): lcd.setCursor(0, 0); // In ra giá trị từ 0 – 9: for (int thisChar = 0; thisChar < 10; thisChar++) { lcd.print(thisChar); delay(500); } // Đặt con trỏ tới vị trí (16,1): lcd.setCursor(16, 1); // Cấu hình hiển thị tự cuộn chữ: lcd.autoscroll(); //IN giá trị từ 0 – 9: for (int thisChar = 0; thisChar < 10; thisChar++) { lcd.print(thisChar); delay(500); } // Tắt chức năng tự động cuộn lcd.noAutoscroll(); // Xóa màn hình cho vòng lặp kế tiếp lcd.clear(); }

Kết quả:

1.3.Blink con trỏ

Chương trình chớp tắt con trỏ cuối dòng:

// Thêm thư viện #include //Khai báo các chân LCD LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { // Cấu hình hàng và cột: lcd.begin(16, 2); // In thông báo hello ra LCD. lcd.print(“hello, world!”); } void loop() { // Tắt blink con trỏ lcd.noBlink(); delay(3000); // Bật blink con trỏ lcd.blink(); delay(3000); }

Kết quả:

1.4.Blink con trỏ dưới dạng dấu gạch nối

Chương trình chớp tắt con trỏ cuối dòng dưới dạng gạch nối:

// Thêm thư viện #include //Khai báo các chân LCD LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { // Khởi tạo LCD: lcd.begin(16, 2); // In thông báo trên LCD. lcd.print(“hello, world!”); } void loop() { // Tắt con trỏ: lcd.noCursor(); delay(500); // Hiện con trỏ: lcd.cursor(); delay(500); }

Kết quả:

1.5.Chạy chữ

Cho tất cả chữ chạy từ trái sang phải và ngược lại.

// Thêm thư viện #include //Khai báo các chân LCD LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { // Cấu hình LCD lcd.begin(16, 2); // In hello ra LCD. lcd.print(“hello, world!”); delay(1000); } void loop() { // Cuộn 13 vị trí sang trái for (int positionCounter = 0; positionCounter < 13; positionCounter++) { // Cuộn 1 vị trí sang trái: lcd.scrollDisplayLeft(); // delay: delay(150); } // Cuộn 29 vị trí sang phải: for (int positionCounter = 0; positionCounter < 29; positionCounter++) { // Cuộn 1 vị trí sang phải: lcd.scrollDisplayRight(); // wdelay: delay(150); } // Cuộn 16 vị trí sang trái for (int positionCounter = 0; positionCounter < 16; positionCounter++) { // Cuộn 1 vị trí sang trái: lcd.scrollDisplayLeft(); // delay: delay(150); } // delay: delay(1000); }

Kết quả:

1.6.In chữ và tự xuống dòng

Chương trình tự xuống dòng khi vượt quá số ký tự trong 1 dòng:

// Thêm thư viện #include // Khai báo 2 biến lưu hàng và cột LCD const int numRows = 2; const int numCols = 16; //Khai báo các chân LCD LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { // Cấu hình LCD: lcd.begin(numCols, numRows); } void loop() { // Hiển thị ký tự từ a – z: for (int thisLetter = ‘a’; thisLetter <= ‘z’; thisLetter++) { // Lặp lại trên cột: for (int thisRow = 0; thisRow < numRows; thisRow++) { // lặp lại trên hàng: for (int thisCol = 0; thisCol < numCols; thisCol++) { // Đặt giá trị con trỏ hiện tại: lcd.setCursor(thisCol, thisRow); // In các ký tự lên LCD: lcd.write(thisLetter); delay(200); } } } }

Kết quả :

1.7.Text Direction

Kiểm soát vị trí con trỏ, ví dụ này phù hợp cho các ứng dụng điều khiển chọn menu dùng nút bấm:

// Thêm thư viện #include //Khai báo các chân LCD LiquidCrystal lcd(12, 11, 5, 4, 3, 2); int thisChar = ‘a’; void setup() { // Cấu hình LCD: lcd.begin(16, 2); // Bật con trỏ: lcd.cursor(); } void loop() { // Đảo chiều tại ‘m’: if (thisChar == ‘m’) { // Dịch phải cho ký tự kế tiếp lcd.rightToLeft(); } // Đảo chiều tiếp tục tại ‘s’: if (thisChar == ‘s’) { // Dịch trái cho ký tự kế tiếp lcd.leftToRight(); } // reset tại ‘z’: if (thisChar > ‘z’) { // Tới vị trí (0,0): lcd.home(); // Bắt đầu in a tại 0 thisChar = ‘a’; } // In ký tự lcd.write(thisChar); // delay: delay(1000); // Tăng ký tự tiếp theo: thisChar++; }

Kết quả:

Mình đã chỉ các bạn cách sử dụng các hiệu ứng khác nhau, cơ bản nhất của Text LCD 16×2.Chúc các bạn may mắn với các hiệu ứng này và ứng dụng được vào nhiều project hay hơn.

“Hello World!”

The LiquidCrystal library allows you to control LCD displays that are compatible with the Hitachi HD44780 driver. There are many of them out there, and you can usually tell them by the 16-pin interface.

This example sketch prints “Hello World!” to the LCD and shows the time in seconds since the Arduino was reset.

output of the sketch on a 16×2 LCD

The LCDs have a parallel interface, meaning that the microcontroller has to manipulate several interface pins at once to control the display. The interface consists of the following pins:

A register select (RS) pin that controls where in the LCD’s memory you’re writing data to. You can select either the data register, which holds what goes on the screen, or an instruction register, which is where the LCD’s controller looks for instructions on what to do next.

A Read/Write (R/W) pin that selects reading mode or writing mode

An Enable pin that enables writing to the registers

8 data pins (D0 -D7). The states of these pins (high or low) are the bits that you’re writing to a register when you write, or the values you’re reading when you read.

There’s also a display constrast pin (Vo), power supply pins (+5V and Gnd) and LED Backlight (Bklt+ and BKlt-) pins that you can use to power the LCD, control the display contrast, and turn on and off the LED backlight, respectively.

The process of controlling the display involves putting the data that form the image of what you want to display into the data registers, then putting instructions in the instruction register. The LiquidCrystal Library simplifies this for you so you don’t need to know the low-level instructions.

The Hitachi-compatible LCDs can be controlled in two modes: 4-bit or 8-bit. The 4-bit mode requires seven I/O pins from the Arduino, while the 8-bit mode requires 11 pins. For displaying text on the screen, you can do most everything in 4-bit mode, so example shows how to control a 16×2 LCD in 4-bit mode.

Hardware Required

  • Arduino Board

  • LCD Screen (compatible with Hitachi HD44780 driver)

  • pin headers to solder to the LCD display pins

  • 10k ohm potentiometer

  • 220 ohm resistor

  • hook-up wires

  • breadboard

Circuit

Before wiring the LCD screen to your Arduino board we suggest to solder a pin header strip to the 14 (or 16) pin count connector of the LCD screen, as you can see in the image above. To wire your LCD screen to your board, connect the following pins:

  • LCD RS pin to digital pin 12

  • LCD Enable pin to digital pin 11

  • LCD D4 pin to digital pin 5

  • LCD D5 pin to digital pin 4

  • LCD D6 pin to digital pin 3

  • LCD D7 pin to digital pin 2

  • LCD R/W pin to GND

  • LCD VSS pin to GND

  • LCD VCC pin to 5V

  • LCD LED+ to 5V through a 220 ohm resistor

  • LCD LED- to GND

Additionally, wire a 10k pot to +5V and GND, with it’s wiper (output) to LCD screens VO pin (pin3).

click the images to enlarge

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Schematic

click the images to enlarge

image developed using Fritzing. For more circuit examples, see the Fritzing project page

Code

See Also

  • Liquid Crystal Library – Your reference for the Liquid Crystal library.

  • Blink – Control of the block-style cursor.

  • Cursor – Control of the underscore-style cursor.

  • Display – Quickly blank the display without losing what’s on it.

  • TextDirection – Control which way text flows from the cursor.

  • Scroll – Scroll text left and right.

  • Serial display – Accepts serial input, displays it.

  • SetCursor – Set the cursor position.

  • Autoscroll – Shift text right and left.

Last revision 2019/04/05 by SM

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

Arduino LCD Shield - Coding Menus the Easy Way
Arduino LCD Shield – Coding Menus the Easy Way

Programming the Arduino

All of the code below uses the LiquidCrystal library that comes pre-installed with the Arduino IDE. A library is a set of functions that can be easily added to a program in an abbreviated format.

In order to use a library, it needs be included in the program. Line 1 in the code below does this with the command

#include

. When you include a library in a program, all of the code in the library gets uploaded to the Arduino along with the code for your program.

Now we’re ready to get into the programming! I’ll go over more interesting things you can do in a moment, but for now lets just run a simple test program. This program will print “hello, world!” to the screen. Enter this code into the Arduino IDE and upload it to the board:


#include LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); lcd.print("hello, world!"); } void loop() { }

Your LCD screen should look like this:

Circuit

Note that this circuit was originally designed for the Arduino UNO. As the Arduino is communicating with the display using SPI, pin 11 & 12 will change depending on what board you are using. For example, on a MKR WiFi 1010, the SPI bus is attached to pin 8 & 11.

Before wiring the LCD screen to your Arduino board we suggest to solder a pin header strip to the 14 (or 16) pin count connector of the LCD screen, as you can see in the image further up.

To wire your LCD screen to your board, connect the following pins:

  • LCD RS pin to digital pin 12
  • LCD Enable pin to digital pin 11
  • LCD D4 pin to digital pin 5
  • LCD D5 pin to digital pin 4
  • LCD D6 pin to digital pin 3
  • LCD D7 pin to digital pin 2
  • LCD R/W pin to GND
  • LCD VSS pin to GND
  • LCD VCC pin to 5V
  • LCD LED+ to 5V through a 220 ohm resistor
  • LCD LED- to GND

Additionally, wire a 10k potentiometer to +5V and GND, with it’s wiper (output) to LCD screens VO pin (pin3).

I2C LCD not showing Text | I2C LCD Errors Fixing || 16x2 LCD not displaying Text || 1602 LCD
I2C LCD not showing Text | I2C LCD Errors Fixing || 16×2 LCD not displaying Text || 1602 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)

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.
Arduino | Simple GUI on LCD TFT Touch Screen to Control LED and Display Temperature and Humidity
Arduino | Simple GUI on LCD TFT Touch Screen to Control LED and Display Temperature and Humidity

Conclusion

So, we have covered pretty much everything we need to know about using an LCD with Arduino. These LCD Character displays are really handy for displaying information for many electronics project. In the examples above I used 16×2 LCD, but the same working principle applies for any other size of these character displays.

I hope you enjoyed this tutorial and learned something new. Feel free to ask any question in the comments section below and don’t forget to check out my full collection of 30+ Arduino Projects.

In this Arduino tutorial we will learn how to connect and use an LCD (Liquid Crystal Display) with Arduino. LCD displays like these are very popular and broadly used in many electronics projects because they are great for displaying simple information, like sensors data, while being very affordable.

I’ve already used them in several of my Arduino projects, and you can check them out here:

  • Arduino Security and Alarm System Project
  • DIY Vending Machine – Arduino based Mechatronics Project
  • Arduino Range Measurer and Digital Spirit Level Project

You can watch the following video or read the written tutorial below. It includes everything you need to know about using an LCD character display with Arduino, such as, LCD pinout, wiring diagram and several example codes.

How to Generate and Display Custom Characters on the LCD

In addition to the ASCII characters, with the LiquidCrystal library it is also possible to generate and display custom characters on the LCD.

We can specify the appearance of each character by an array of 8 bytes. Here’s an example code:


byte heart[8] = { // Array of bytes B00000, // B stands for binary formatter and the five numbers are the pixels B01010, B11111, B11111, B01110, B00100, B00000, B00000 }; byte smile[8] = { B00000, B00000, B01010, B00000, B10001, B01110, B00000, B00000 }; byte lock[8] = { B01110, B10001, B10001, B11111, B11011, B11011, B11111, B00000 }; byte character[8] = { B11111, B10101, B11111, B01010, B01110, B11111, B01110, B01110 }; LiquidCrystal lcd(1, 2, 4, 5, 6, 7); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7) void setup() { lcd.begin(16, 2); // Initializes the interface to the LCD screen, and specifies the dimensions (width and height) of the display lcd.createChar(0, heart); // Create a custom character lcd.createChar(1, smile); lcd.createChar(2, lock); lcd.createChar(3, character); // Clears the LCD screen lcd.clear(); // Print a message to the LCD lcd.print("Custom Character"); } void loop() { lcd.setCursor(1, 1); lcd.write(byte(0)); // Display the custom character 0, the heart lcd.setCursor(5, 1); lcd.write(byte(1)); lcd.setCursor(9, 1); lcd.write(byte(2)); lcd.setCursor(13, 1); lcd.write(byte(3)); }

Code language: Arduino (arduino)

We can notice how we can specify the appearance of the character by changing the 0s into 1s within the 5×8 pixels grid.

In the setup we have to create the custom character using the createChar() function.


lcd.createChar(0, heart); // Create a custom character

Code language: Arduino (arduino)

The first parameter in this function is a number between 0 and 7, or we have to reserve one of the 8 supported custom characters. The second parameter is the name of the array of bytes.

We write the custom character to the display using the write() function and as a parameter we use the number of the character.


lcd.write(byte(0)); // Display the custom character 0, or the heart

Code language: Arduino (arduino)

See also: Arduino Touch Screen Tutorial | TFT LCD

Arduino LCD tutorial | Using LCD Displays with Arduino [code explained] | Arduino tutorial 16
Arduino LCD tutorial | Using LCD Displays with Arduino [code explained] | Arduino tutorial 16

Schematic

Hello World Example

This example sketch prints

to the LCD and shows the time in seconds since the Arduino was reset.

Hello World!

1/*2 LiquidCrystal Library – Hello World34 Demonstrates the use a 16×2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.89 This sketch prints “Hello World!” to the LCD10 and shows the time.1112 The circuit:13 * LCD RS pin to digital pin 1214 * LCD Enable pin to digital pin 1115 * LCD D4 pin to digital pin 516 * LCD D5 pin to digital pin 417 * LCD D6 pin to digital pin 318 * LCD D7 pin to digital pin 219 * LCD R/W pin to ground20 * LCD VSS pin to ground21 * LCD VCC pin to 5V22 * 10K resistor:23 * ends to +5V and ground24 * wiper to LCD VO pin (pin 3)2526 Library originally added 18 Apr 200827 by David A. Mellis28 library modified 5 Jul 200929 by Limor Fried (http://www.ladyada.net)30 example added 9 Jul 200931 by Tom Igoe32 modified 22 Nov 201033 by Tom Igoe34 modified 7 Nov 201635 by Arturo Guadalupi3637 This example code is in the public domain.3839 https://docs.arduino.cc/learn/electronics/lcd-displays4041*/4243// include the library code:44#include 4546// initialize the library by associating any needed LCD interface pin47// with the arduino pin number it is connected to48const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;49LiquidCrystal lcd(rs, en, d4, d5, d6, d7);5051void setup() {52 // set up the LCD’s number of columns and rows:53 lcd.begin(16, 2);54 // Print a message to the LCD.55 lcd.print(“hello, world!”);56}5758void loop() {59 // set the cursor to column 0, line 160 // (note: line 1 is the second row, since counting begins with 0):61 lcd.setCursor(0, 1);62 // print the number of seconds since reset:63 lcd.print(millis() / 1000);64}

Autoscroll Example

This example sketch shows how to use the

and

autoscroll()

methods to move all the text on the display left or right.

noAutoscroll()

  • moves all the text one space to the left each time a letter is added


    autoscroll()

  • turns scrolling off


    noAutoscroll()

This sketch prints the characters

to

with autoscroll off, then moves the cursor to the bottom right, turns autoscroll on, and prints them again.

1/*23 LiquidCrystal Library – Autoscroll45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch demonstrates the use of the autoscroll()1415 and noAutoscroll() functions to make new text scroll or not.1617 The circuit:1819 * LCD RS pin to digital pin 122021 * LCD Enable pin to digital pin 112223 * LCD D4 pin to digital pin 52425 * LCD D5 pin to digital pin 42627 * LCD D6 pin to digital pin 32829 * LCD D7 pin to digital pin 23031 * LCD R/W pin to ground3233 * 10K resistor:3435 * ends to +5V and ground3637 * wiper to LCD VO pin (pin 3)3839 Library originally added 18 Apr 20084041 by David A. Mellis4243 library modified 5 Jul 20094445 by Limor Fried (http://www.ladyada.net)4647 example added 9 Jul 20094849 by Tom Igoe5051 modified 22 Nov 20105253 by Tom Igoe5455 modified 7 Nov 20165657 by Arturo Guadalupi5859 This example code is in the public domain.6061 http://www.arduino.cc/en/Tutorial/LiquidCrystalAutoscroll6263*/6465// include the library code:66#include 6768// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to7071const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7273LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7475void setup() {7677 // set up the LCD’s number of columns and rows:7879 lcd.begin(16, 2);80}8182void loop() {8384 // set the cursor to (0,0):8586 lcd.setCursor(0, 0);8788 // print from 0 to 9:8990 for (int thisChar = 0; thisChar < 10; thisChar++) {9192 lcd.print(thisChar);9394 delay(500);9596 }9798 // set the cursor to (16,1):99100 lcd.setCursor(16, 1);101102 // set the display to automatically scroll:103104 lcd.autoscroll();105106 // print from 0 to 9:107108 for (int thisChar = 0; thisChar < 10; thisChar++) {109110 lcd.print(thisChar);111112 delay(500);113114 }115116 // turn off automatic scrolling117118 lcd.noAutoscroll();119120 // clear screen for the next loop:121122 lcd.clear();123}

Blink Example

This example sketch shows how to use the

and

blink()

methods to blink a block-style cursor.

noBlink()

1/*23 LiquidCrystal Library – Blink45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch prints “Hello World!” to the LCD and makes the1415 cursor block blink.1617 The circuit:1819 * LCD RS pin to digital pin 122021 * LCD Enable pin to digital pin 112223 * LCD D4 pin to digital pin 52425 * LCD D5 pin to digital pin 42627 * LCD D6 pin to digital pin 32829 * LCD D7 pin to digital pin 23031 * LCD R/W pin to ground3233 * 10K resistor:3435 * ends to +5V and ground3637 * wiper to LCD VO pin (pin 3)3839 Library originally added 18 Apr 20084041 by David A. Mellis4243 library modified 5 Jul 20094445 by Limor Fried (http://www.ladyada.net)4647 example added 9 Jul 20094849 by Tom Igoe5051 modified 22 Nov 20105253 by Tom Igoe5455 modified 7 Nov 20165657 by Arturo Guadalupi5859 This example code is in the public domain.6061 http://www.arduino.cc/en/Tutorial/LiquidCrystalBlink6263*/6465// include the library code:66#include 6768// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to7071const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7273LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7475void setup() {7677 // set up the LCD’s number of columns and rows:7879 lcd.begin(16, 2);8081 // Print a message to the LCD.8283 lcd.print(“hello, world!”);84}8586void loop() {8788 // Turn off the blinking cursor:8990 lcd.noBlink();9192 delay(3000);9394 // Turn on the blinking cursor:9596 lcd.blink();9798 delay(3000);99}

Cursor

This example sketch shows how to use the

and

cursor()

methods to control an underscore-style cursor.

noCursor()

1/*23 LiquidCrystal Library – Cursor45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch prints “Hello World!” to the LCD and1415 uses the cursor() and noCursor() methods to turn1617 on and off the cursor.1819 The circuit:2021 * LCD RS pin to digital pin 122223 * LCD Enable pin to digital pin 112425 * LCD D4 pin to digital pin 52627 * LCD D5 pin to digital pin 42829 * LCD D6 pin to digital pin 33031 * LCD D7 pin to digital pin 23233 * LCD R/W pin to ground3435 * 10K resistor:3637 * ends to +5V and ground3839 * wiper to LCD VO pin (pin 3)4041 Library originally added 18 Apr 20084243 by David A. Mellis4445 library modified 5 Jul 20094647 by Limor Fried (http://www.ladyada.net)4849 example added 9 Jul 20095051 by Tom Igoe5253 modified 22 Nov 20105455 by Tom Igoe5657 modified 7 Nov 20165859 by Arturo Guadalupi6061 This example code is in the public domain.6263 http://www.arduino.cc/en/Tutorial/LiquidCrystalCursor6465*/6667// include the library code:68#include 6970// initialize the library by associating any needed LCD interface pin71// with the arduino pin number it is connected to7273const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7475LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7677void setup() {7879 // set up the LCD’s number of columns and rows:8081 lcd.begin(16, 2);8283 // Print a message to the LCD.8485 lcd.print(“hello, world!”);86}8788void loop() {8990 // Turn off the cursor:9192 lcd.noCursor();9394 delay(500);9596 // Turn on the cursor:9798 lcd.cursor();99100 delay(500);101}

Display Example

This example sketch shows how to use the

and

display()

methods to turn on and off the display. The text to be displayed will still be preserved when you use noDisplay() so it’s a quick way to blank the display without losing everything on it.

noDisplay()

1/*2 LiquidCrystal Library – display() and noDisplay()34 Demonstrates the use a 16×2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.89 This sketch prints “Hello World!” to the LCD and uses the10 display() and noDisplay() functions to turn on and off11 the display.1213 The circuit:14 * LCD RS pin to digital pin 1215 * LCD Enable pin to digital pin 1116 * LCD D4 pin to digital pin 517 * LCD D5 pin to digital pin 418 * LCD D6 pin to digital pin 319 * LCD D7 pin to digital pin 220 * LCD R/W pin to ground21 * 10K resistor:22 * ends to +5V and ground23 * wiper to LCD VO pin (pin 3)2425 Library originally added 18 Apr 200826 by David A. Mellis27 library modified 5 Jul 200928 by Limor Fried (http://www.ladyada.net)29 example added 9 Jul 200930 by Tom Igoe31 modified 22 Nov 201032 by Tom Igoe33 modified 7 Nov 201634 by Arturo Guadalupi3536 This example code is in the public domain.3738 http://www.arduino.cc/en/Tutorial/LiquidCrystalDisplay3940*/4142// include the library code:43#include 4445// initialize the library by associating any needed LCD interface pin46// with the arduino pin number it is connected to47const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;48LiquidCrystal lcd(rs, en, d4, d5, d6, d7);4950void setup() {51 // set up the LCD’s number of columns and rows:52 lcd.begin(16, 2);53 // Print a message to the LCD.54 lcd.print(“hello, world!”);55}5657void loop() {58 // Turn off the display:59 lcd.noDisplay();60 delay(500);61 // Turn on the display:62 lcd.display();63 delay(500);64}

Scroll Example

This example sketch shows how to use the

and

scrollDisplayLeft()

methods to reverse the direction the text is flowing. It prints “Hello World!”, scrolls it offscreen to the left, then offscreen to the right, then back to home.

scrollDisplayRight()

1/*2 LiquidCrystal Library – scrollDisplayLeft() and scrollDisplayRight()34 Demonstrates the use a 16×2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.89 This sketch prints “Hello World!” to the LCD and uses the10 scrollDisplayLeft() and scrollDisplayRight() methods to scroll11 the text.1213 The circuit:14 * LCD RS pin to digital pin 1215 * LCD Enable pin to digital pin 1116 * LCD D4 pin to digital pin 517 * LCD D5 pin to digital pin 418 * LCD D6 pin to digital pin 319 * LCD D7 pin to digital pin 220 * LCD R/W pin to ground21 * 10K resistor:22 * ends to +5V and ground23 * wiper to LCD VO pin (pin 3)2425 Library originally added 18 Apr 200826 by David A. Mellis27 library modified 5 Jul 200928 by Limor Fried (http://www.ladyada.net)29 example added 9 Jul 200930 by Tom Igoe31 modified 22 Nov 201032 by Tom Igoe33 modified 7 Nov 201634 by Arturo Guadalupi3536 This example code is in the public domain.3738 http://www.arduino.cc/en/Tutorial/LiquidCrystalScroll3940*/4142// include the library code:43#include 4445// initialize the library by associating any needed LCD interface pin46// with the arduino pin number it is connected to47const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;48LiquidCrystal lcd(rs, en, d4, d5, d6, d7);4950void setup() {51 // set up the LCD’s number of columns and rows:52 lcd.begin(16, 2);53 // Print a message to the LCD.54 lcd.print(“hello, world!”);55 delay(1000);56}5758void loop() {59 // scroll 13 positions (string length) to the left60 // to move it offscreen left:61 for (int positionCounter = 0; positionCounter < 13; positionCounter++) {62 // scroll one position left:63 lcd.scrollDisplayLeft();64 // wait a bit:65 delay(150);66 }6768 // scroll 29 positions (string length + display length) to the right69 // to move it offscreen right:70 for (int positionCounter = 0; positionCounter < 29; positionCounter++) {71 // scroll one position right:72 lcd.scrollDisplayRight();73 // wait a bit:74 delay(150);75 }7677 // scroll 16 positions (display length + string length) to the left78 // to move it back to center:79 for (int positionCounter = 0; positionCounter < 16; positionCounter++) {80 // scroll one position left:81 lcd.scrollDisplayLeft();82 // wait a bit:83 delay(150);84 }8586 // delay at the end of the full loop:87 delay(1000);8889}

Serial to Display Example

This example sketch accepts serial input from a host computer and displays it on the LCD. To use it, upload the sketch, then open the Serial Monitor and type some characters and click Send. The text will appear on your LCD.

1/*2 LiquidCrystal Library – Serial Input34 Demonstrates the use a 16×2 LCD display. The LiquidCrystal5 library works with all LCD displays that are compatible with the6 Hitachi HD44780 driver. There are many of them out there, and you7 can usually tell them by the 16-pin interface.89 This sketch displays text sent over the serial port10 (e.g. from the Serial Monitor) on an attached LCD.1112 The circuit:13 * LCD RS pin to digital pin 1214 * LCD Enable pin to digital pin 1115 * LCD D4 pin to digital pin 516 * LCD D5 pin to digital pin 417 * LCD D6 pin to digital pin 318 * LCD D7 pin to digital pin 219 * LCD R/W pin to ground20 * 10K resistor:21 * ends to +5V and ground22 * wiper to LCD VO pin (pin 3)2324 Library originally added 18 Apr 200825 by David A. Mellis26 library modified 5 Jul 200927 by Limor Fried (http://www.ladyada.net)28 example added 9 Jul 200929 by Tom Igoe30 modified 22 Nov 201031 by Tom Igoe32 modified 7 Nov 201633 by Arturo Guadalupi3435 This example code is in the public domain.3637 http://www.arduino.cc/en/Tutorial/LiquidCrystalSerialDisplay3839*/4041// include the library code:42#include 4344// initialize the library by associating any needed LCD interface pin45// with the arduino pin number it is connected to46const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;47LiquidCrystal lcd(rs, en, d4, d5, d6, d7);4849void setup() {50 // set up the LCD’s number of columns and rows:51 lcd.begin(16, 2);52 // initialize the serial communications:53 Serial.begin(9600);54}5556void loop() {57 // when characters arrive over the serial port…58 if (Serial.available()) {59 // wait a bit for the entire message to arrive60 delay(100);61 // clear the screen62 lcd.clear();63 // read all the available characters64 while (Serial.available() > 0) {65 // display each character to the LCD66 lcd.write(Serial.read());67 }68 }69}

Set Cursor Example

This example sketch shows how to use the

method to reposition the cursor. To move the cursor, just call

setCursor()

with a row and column position. For example, for a 2×16 display:

setCursor()

1lcd.setCursor(0, 0); // top left2lcd.setCursor(15, 0); // top right3lcd.setCursor(0, 1); // bottom left4lcd.setCursor(15, 1); // bottom right

Here is the full example:

1/*23 LiquidCrystal Library – setCursor45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch prints to all the positions of the LCD using the1415 setCursor() method:1617 The circuit:1819 * LCD RS pin to digital pin 122021 * LCD Enable pin to digital pin 112223 * LCD D4 pin to digital pin 52425 * LCD D5 pin to digital pin 42627 * LCD D6 pin to digital pin 32829 * LCD D7 pin to digital pin 23031 * LCD R/W pin to ground3233 * 10K resistor:3435 * ends to +5V and ground3637 * wiper to LCD VO pin (pin 3)3839 Library originally added 18 Apr 20084041 by David A. Mellis4243 library modified 5 Jul 20094445 by Limor Fried (http://www.ladyada.net)4647 example added 9 Jul 20094849 by Tom Igoe5051 modified 22 Nov 20105253 by Tom Igoe5455 modified 7 Nov 20165657 by Arturo Guadalupi5859 This example code is in the public domain.6061 http://www.arduino.cc/en/Tutorial/LiquidCrystalSetCursor6263*/6465// include the library code:66#include 6768// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to7071const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7273LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7475// these constants won’t change. But you can change the size of76// your LCD using them:7778const int numRows = 2;7980const int numCols = 16;8182void setup() {8384 // set up the LCD’s number of columns and rows:8586 lcd.begin(numCols, numRows);87}8889void loop() {9091 // loop from ASCII ‘a’ to ASCII ‘z’:9293 for (int thisLetter = ‘a’; thisLetter <= ‘z’; thisLetter++) {9495 // loop over the columns:9697 for (int thisRow = 0; thisRow < numRows; thisRow++) {9899 // loop over the rows:100101 for (int thisCol = 0; thisCol < numCols; thisCol++) {102103 // set the cursor position:104105 lcd.setCursor(thisCol, thisRow);106107 // print the letter:108109 lcd.write(thisLetter);110111 delay(200);112113 }114115 }116117 }118}

Text Direction Example

This example sketch shows how to use the

and

leftToRight()

methods. These methods control which way text flows from the cursor.

rightToLeft()

  • causes text to flow to the left from the cursor, as if the display is right-justified.


    rightToLeft()

  • causes text to flow to the right from the cursor, as if the display is left-justified.


    leftToRight()

This sketch prints

through

right to left, then

through

left to right, then

through

right to left again.

1/*23 LiquidCrystal Library – TextDirection45 Demonstrates the use a 16×2 LCD display. The LiquidCrystal67 library works with all LCD displays that are compatible with the89 Hitachi HD44780 driver. There are many of them out there, and you1011 can usually tell them by the 16-pin interface.1213 This sketch demonstrates how to use leftToRight() and rightToLeft()1415 to move the cursor.1617 The circuit:1819 * LCD RS pin to digital pin 122021 * LCD Enable pin to digital pin 112223 * LCD D4 pin to digital pin 52425 * LCD D5 pin to digital pin 42627 * LCD D6 pin to digital pin 32829 * LCD D7 pin to digital pin 23031 * LCD R/W pin to ground3233 * 10K resistor:3435 * ends to +5V and ground3637 * wiper to LCD VO pin (pin 3)3839 Library originally added 18 Apr 20084041 by David A. Mellis4243 library modified 5 Jul 20094445 by Limor Fried (http://www.ladyada.net)4647 example added 9 Jul 20094849 by Tom Igoe5051 modified 22 Nov 20105253 by Tom Igoe5455 modified 7 Nov 20165657 by Arturo Guadalupi5859 This example code is in the public domain.6061 http://www.arduino.cc/en/Tutorial/LiquidCrystalTextDirection6263*/6465// include the library code:66#include 6768// initialize the library by associating any needed LCD interface pin69// with the arduino pin number it is connected to7071const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;7273LiquidCrystal lcd(rs, en, d4, d5, d6, d7);7475int thisChar = ‘a’;7677void setup() {7879 // set up the LCD’s number of columns and rows:8081 lcd.begin(16, 2);8283 // turn on the cursor:8485 lcd.cursor();86}8788void loop() {8990 // reverse directions at ‘m’:9192 if (thisChar == ‘m’) {9394 // go right for the next letter9596 lcd.rightToLeft();9798 }99100 // reverse again at ‘s’:101102 if (thisChar == ‘s’) {103104 // go left for the next letter105106 lcd.leftToRight();107108 }109110 // reset at ‘z’:111112 if (thisChar > ‘z’) {113114 // go to (0,0):115116 lcd.home();117118 // start again at 0119120 thisChar = ‘a’;121122 }123124 // print the character125126 lcd.write(thisChar);127128 // wait a second:129130 delay(1000);131132 // increment the letter:133134 thisChar++;135}

Custom Character

This example demonstrates how to add custom characters on an LCD display.

Note that this example requires an additional potentiometer:

  • Outer pins connected to 5V and GND.
  • Inner pin (wiper) connected to A0.

This potentiometer controls the

variable.

delayTime

1/*2 LiquidCrystal Library – Custom Characters34 Demonstrates how to add custom characters on an LCD display.5 The LiquidCrystal library works with all LCD displays that are6 compatible with the Hitachi HD44780 driver. There are many of7 them out there, and you can usually tell them by the 16-pin interface.89 This sketch prints “I

Arduino!” and a little dancing man10 to the LCD.1112 The circuit:13 * LCD RS pin to digital pin 1214 * LCD Enable pin to digital pin 1115 * LCD D4 pin to digital pin 516 * LCD D5 pin to digital pin 417 * LCD D6 pin to digital pin 318 * LCD D7 pin to digital pin 219 * LCD R/W pin to ground20 * 10K potentiometer:21 * ends to +5V and ground22 * wiper to LCD VO pin (pin 3)23 * 10K poterntiometer on pin A02425 created 21 Mar 201126 by Tom Igoe27 modified 11 Nov 201328 by Scott Fitzgerald29 modified 7 Nov 201630 by Arturo Guadalupi3132 Based on Adafruit’s example at33 https://github.com/adafruit/SPI_VFD/blob/master/examples/createChar/createChar.pde3435 This example code is in the public domain.36 https://docs.arduino.cc/learn/electronics/lcd-displays#custom-character3738 Also useful:39 http://icontexto.com/charactercreator/4041*/4243// include the library code:44#include 4546// initialize the library by associating any needed LCD interface pin47// with the arduino pin number it is connected to48const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;49LiquidCrystal lcd(rs, en, d4, d5, d6, d7);5051// make some custom characters:52byte heart[8] = {53 0b00000,54 0b01010,55 0b11111,56 0b11111,57 0b11111,58 0b01110,59 0b00100,60 0b0000061};6263byte smiley[8] = {64 0b00000,65 0b00000,66 0b01010,67 0b00000,68 0b00000,69 0b10001,70 0b01110,71 0b0000072};7374byte frownie[8] = {75 0b00000,76 0b00000,77 0b01010,78 0b00000,79 0b00000,80 0b00000,81 0b01110,82 0b1000183};8485byte armsDown[8] = {86 0b00100,87 0b01010,88 0b00100,89 0b00100,90 0b01110,91 0b10101,92 0b00100,93 0b0101094};9596byte armsUp[8] = {97 0b00100,98 0b01010,99 0b00100,100 0b10101,101 0b01110,102 0b00100,103 0b00100,104 0b01010105};106107void setup() {108 // initialize LCD and set up the number of columns and rows:109 lcd.begin(16, 2);110111 // create a new character112 lcd.createChar(0, heart);113 // create a new character114 lcd.createChar(1, smiley);115 // create a new character116 lcd.createChar(2, frownie);117 // create a new character118 lcd.createChar(3, armsDown);119 // create a new character120 lcd.createChar(4, armsUp);121122 // set the cursor to the top left123 lcd.setCursor(0, 0);124125 // Print a message to the lcd.126 lcd.print(“I “);127 lcd.write(byte(0)); // when calling lcd.write() ‘0’ must be cast as a byte128 lcd.print(” Arduino! “);129 lcd.write((byte)1);130131}132133void loop() {134 // read the potentiometer on A0:135 int sensorReading = analogRead(A0);136 // map the result to 200 – 1000:137 int delayTime = map(sensorReading, 0, 1023, 200, 1000);138 // set the cursor to the bottom row, 5th position:139 lcd.setCursor(4, 1);140 // draw the little man, arms down:141 lcd.write(3);142 delay(delayTime);143 lcd.setCursor(4, 1);144 // draw him arms up:145 lcd.write(4);146 delay(delayTime);147}

Suggested changes

The content on docs.arduino.cc is facilitated through a public GitHub repository. You can read more on how to contribute in the contribution policy.

License

The Arduino documentation is licensed under the Creative Commons Attribution-Share Alike 4.0 license.

Here’s a detailed guide to learn how to interface LCD with an Arduino.

Many times, when designing an embedded project you may need a serial monitor to check if all the things are working. But you do not think it is a time consuming and it can be stopped if we interface LCD with Arduino board. Yes, this is possible so let’s get ready, in this blog we are going to learn how to interface a 16 x2 LCD display with an Arduino board.

Keywords searched by users: lcd coding in arduino

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 Display Message On Lcd Using Serial Monitor Of Ardunio
How To Display Message On Lcd Using Serial Monitor Of Ardunio
16X2 Lcd With Arduino: Code And Circuit - Electronics Fun
16X2 Lcd With Arduino: Code And Circuit – Electronics Fun
Liquid Crystal Displays (Lcd) With Arduino | Arduino Documentation
Liquid Crystal Displays (Lcd) With Arduino | Arduino Documentation
Interfacing 20X4 Lcd With Arduino : 5 Steps - Instructables
Interfacing 20X4 Lcd With Arduino : 5 Steps – Instructables
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
Arduino - Lcd | Arduino Tutorial
Arduino – Lcd | Arduino Tutorial
How To Interface I2C Lcd Display With Arduino ? - Geeksforgeeks
How To Interface I2C Lcd Display With Arduino ? – Geeksforgeeks
How To Use An Lcd Display - Arduino Tutorial : 5 Steps (With Pictures) -  Instructables
How To Use An Lcd Display – Arduino Tutorial : 5 Steps (With Pictures) – Instructables
Arduino Lcd I2C Simple Use And Direct Write From Serial Monitor – Surtr  Technology
Arduino Lcd I2C Simple Use And Direct Write From Serial Monitor – Surtr Technology
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
Arduino - Keypad - Lcd | Arduino Tutorial
Arduino – Keypad – Lcd | Arduino Tutorial
Lcd 1602 With Arduino Uno R3 : 6 Steps - Instructables
Lcd 1602 With Arduino Uno R3 : 6 Steps – Instructables
Arduino - Lcd Clock | Arduino Tutorial
Arduino – Lcd Clock | Arduino Tutorial
Making I2C Lcd Work With C++ Style Code - Programming Questions - Arduino  Forum
Making I2C Lcd Work With C++ Style Code – Programming Questions – Arduino Forum
How To Interface I2C Lcd With Arduino Nano
How To Interface I2C Lcd With Arduino Nano

See more here: kientrucannam.vn

Leave a Reply

Your email address will not be published. Required fields are marked *