The liquid crystal display (LCD) is pretty useful to view data from an Arduino Uno. This data can be anything that you wish. It could be a funny joke or some sensor readings. You can also connect the Arduino with an ESP8266 to display data wirelessly. In this Arduino tutorial, we will take an in-depth look at the process of interfacing an LCD screen with the Arduino Uno. Additionally, we will also break down common problems that might arise while using the LCD module.
Contents
What are the components required for interfacing an Arduino Uno with an LCD?
Hardware components required for interfacing LCD with Arduino
- Arduino Uno
- Liquid crystal display screen-JHD162A
- 10kΩ Potentiometer.
- Breadboard.
- Jumper wires – as required.
- Arduino Uno
- Liquid crystal display screen-JHD162A
- 10kΩ Potentiometer.
- Breadboard.
- Jumper wires – as required.
Understanding the 16×2 LCD
What is a Liquid crystal display (LCD)?
LCDs are devices that display data. Liquid crystal displays show data using the principle of ‘light blocking’. They produce an image using a backlight. An LCD is made up of a large number of pixels. The resolution depends on the number of pixels. It is available in various backlight colors like green, yellow, blue. Mostly we use the standard 16×2 Liquid crystal display. That 16×2 indicates that it has 16 rows and two columns of cells made up of a 5×7 or 5×8 dot matrix. Each dot matrix has a 5×8 dot resolution. Sounds a bit confusing? Don’t worry. The image below will clear up your doubts.
How does an LCD screen work?
What are the specifications and features of the 16×2 LCD screen?
An LCD module has 16 pins with backlight and contrast adjustment functions.
Features
- Operating Voltage of an LCD is 4.7V to 5.3V.
- The current consumption is 1mA without backlight.
- Alphanumeric LCD module. It can display alphabets and numbers and some characters.
- Consists of two rows and each row can print 16 characters.
- Each character is built by a 5×8 (or 5×7) pixel box.
- Can work on both 8-bit and 4-bit mode. This feature is handy when you don’t feel like using a lot of pins for one module. More on this later.
- It can also display any custom generated characters.
- Most commonly available in Green and Blue Backlight.
Item | Symbol | Standard Value Typ. |
Unit |
---|---|---|---|
Input Voltage | VDD | 3/5 | V |
Recommended LCD Driving Voltage for Normal Temp. Version module @25°C |
VDD-VO | 3.70 | V |
What are the physical dimensions of the LCD that we are going to connect to the Arduino Uno?
Item | Standard Value | Unit |
---|---|---|
Module Dimension | 80.0 × 36.0 | mm |
Viewing Area | 66.0 × 16.0 | mm |
Mounting Hole | 75.0 × 31.0 | mm |
Character Size | 2.95 × 5.55 | mm |
What are the functions of the pins present on the LCD screen?
In JHD162A LCD which has 16 pins,
Pin No. | Function | Name |
1 | Ground (0V) | Ground |
2 | Supply voltage; 5V (4.7V – 5.3V) | Vcc |
3 | Contrast adjustment; the best way is to use a variable resistor such as a potentiometer. The output of the potentiometer is connected to this pin. Rotate the potentiometer knob forward and backward to adjust the LCD contrast. | Vo / VEE |
4 | Selects command register when low, and data register when high | RS (Register Select ) |
5 | Read when low and write when high.
The Read/Write (RW) pin (pin 5) of an HD44780 LCD is the pin which either enables a user to write to the LCD so that it displays characters (write mode) or to read the characters from the LCD, so that is on it The dominant majority of the time, the RW pin will be set to Write mode. In some rare cases, we may need to read from the LCD what it is displaying. In such cases, the RW pin will be set high. However, the vast majority of the time, the RW pin will be set low, since we are concerned about writing characters to the LCD. Modes of RW Pin |
Read/write |
6 | Sends an enable HIGH for a few milliseconds when we need to execute an instruction. Then back to LOW. | Enable |
7 | 8-bit data pins | DB0 |
8 | DB1 | |
9 | DB2 | |
10 | DB3 | |
11 | DB4 | |
12 | DB5 | |
13 | DB6 | |
14 | DB7 | |
15 | Backlight VCC (5V) | Led+ |
16 | Backlight Ground (0V) | Led-
|
How to send commands and data to an LCD module?
16×2 LCDs have two built-in registers. Namely,
- Command register
- Data register
Command register:
We use this register to insert a command into the LCD. These commands can be things like telling the LCD to clear the screen, set the cursor, move to line 1, move to character 1, etc. Here’s the table of all the commands that you can send to the LCD.
Sr.No. | Hex Code | Command to LCD instruction Register |
1 | 01 | Clear display screen |
2 | 02 | Return home |
3 | 04 | Decrement cursor (shift cursor to the left) |
4 | 06 | Increment cursor (shift cursor to the right) |
5 | 05 | Shift display right |
6 | 07 | Shift display left |
7 | 08 | Display off, cursor off |
8 | 0A | Display off, cursor on |
9 | 0C | Display on, cursor off |
10 | 0E | Display on, cursor blinking |
11 | 0F | Display on, cursor blinking |
12 | 10 | Shift cursor position to left |
13 | 14 | Shift cursor position to the right |
14 | 18 | Shift the entire display to the left |
15 | 1C | Shift the entire display to the right |
16 | 80 | Force cursor to the beginning (1st line) |
17 | C0 | Force cursor to the beginning (2nd line) |
18 | 38 | Two lines and 5×7 matrix (8-bit mode) |
18 | 30 | 1 line and 5×7 matrix (8-bit mode) |
18 | 20 | 1 line and 5×7 matrix (4-bit mode) |
18 | 28 | Two lines and 5×7 matrix (4-bit mode) |
Here’s a slightly detailed version of the above table. Don’t get too caught up in reading these. You’ll most probably not even need a bunch of these codes. Also, the ones you need, you’ll pick up as you code. Don’t sweat trying to remember these. Just glance at them and move on. It’s just a bunch of hex codes that control stuff on the LCD.
Data register
You can send the ASCII codes of the characters you would like to display into this register. Here’s the ASCII table for all the characters that you can view on a 16×2 LCD screen.
Difference and use case of 4-bit and 8-bit functions feature of the LCD.
8-bit mode working of the 16×2 LCD
In our case for the 8-bit mode, the 8 data pins (D0-D7) are the data and address bus while the three control pins(RS, R/W, and E) are the control bus. Thus using these, we can control the LCD peripheral that we are interfacing.
We are greedy, so we want to interface as many peripherals as possible with the same microcontroller. This requires a large number of ports. Or we need to be smart and utilize what we have to the fullest. Throughout this free Arduino course, you’ll notice us stressing about the importance of pin real estate.
Thus the first thing we try to do is reduce the number of pins required for controlling the peripheral. Here comes the need for 4-bit mode. Using this mode, we end up cutting the port pins needed from 11 to 7. It might not seem much, but for a small microcontroller with limited port pins, this is a significant amount.
4-bit mode working of the 16×2 LCD
In the 4 bit mode, we send the data nibble (4 bits/half a byte) by nibble. First upper nibble and then lower nibble. For those of you who don’t know what a nibble is: a nibble is a group of four bits, so the lower four bits (B0-B3) of a byte from the lower nibble while the upper four bits (B4-B7) of a byte from the higher nibble. This enables us to send 8-bit data or ASCII code or the command code by using four pins instead of 8. The connections remain identical. The only change is that the lower nibble pins of LCD are unused (D0-D3).
How to connect the LCD with the Arduino Uno?
The Arduino Uno has a microcontroller that can read data from various inputs like sensors and also can compute it according to the program coded. Here we use Arduino Uno, which has 14 digital input/output pins and six analog pins.
Check out the schematic below to hook up your LCD module with an Arduino Uno.
Circuit for connecting an Arduino with an LCD. (Source)
- LCD pin 1 (GND) is connected to the ground pin of the Arduino and to one end of the 10k potentiometer
- Pin 2 (Vcc) connects with the 5V Vcc pin of Arduino.
- Connect the third pin of the LCD (Vee) with the middle pin of the 10 k potentiometer.
- You can connect the fourth pin (RS) with any of the digital pins on the Arduino. However, make sure that you include this connection in the code accurately. The Register select pin controls whether commands or data is being sent to the LCD. When the pin is given a 0 (LOW), it accepts commands as input. When it is given a 1 (HIGH), it takes data as input. We will see this implementation in the code.
- Connect pin 5 (R/W) with the ground pin of the Arduino. Since we are writing to the LCD, we will select the Write mode by connecting it to a LOW value.
- LCD pin 6(EN) connects with any of the digital pins on Arduino. This is the enable pin, and it enables the next seven pins of the LCD (D0-D7) to send 8-bit data.
- LCD pin 7- pin 14(DB0-DB7) connects with any of the digital pins of the Arduino. However, remember to declare these as data pins in the code.
- Next, connect Pin 15(LED+) with the 5V Vcc pin of Arduino. The function of this pin is to control the backlight of the LCD screen.
- LCD pin 16(LED-) should be connected with the ground pin of the Arduino.
Coding for connecting the LCD screen with an Arduino Uno
The Arduino program for displaying data on LCD is done in the Arduino IDE. First, we need to connect the Arduino environment with our Arduino Uno board by selecting a suitable port.
The Arduino code should contain two functions as void setup() and void loop() all pin mode declarations are made in void setup() and the function to be performed, and the computing process is coded in void loop(). This part will run infinitely.
Programming in Arduino is usually done with embedded C. For displaying data on an LCD, we’ll require a header file (library) – called LiquidCrystal.h. This file is available in the Arduino IDE. Here’s how you can include a library in your program.
In void setup() function, we need to begin the function as object.begin(16,2), which indicates the 16 rows and two columns. And simply print “Hello world” by writing a function called object.print(“Hello world”). Use delay() function to keep the data stay on display for some seconds. The argument of this delay() function should be in milliseconds.
In the void loop() function, we need to set the cursor using the object set cursor(0,0) function. The arguments are column and row at which the data starts to be printed and print the information whatever you want. It is necessary to pass arguments for the object created in LiquidCrystal library which has six arguments as digital pin of Arduino to which the Read/write pin of LCD is connected, digital pin of Arduino to which the enable pin of LCD is connected, and followed by digital pins of Arduino to which the six data pins of LCD is connected.
Some of the built-in functions of the LiquidCrystal library are mentioned here:
- LiquidCrystal(R/W, EN, D4, D5, D6, D7) – to initialize the digital pin modes of Arduino to wich the Read/Write, enable, and data pins of LCD are connected
- clear()- to clear the display.
- home()- to set the cursor at the top left of the LCD screen.
- setCursor(col, row)- to set the cursor at position wherever we want. It has two arguments as column and row, which are the coordinates of the point where we want to set the cursor.
- write(data) – to write a character on the LCD screen.
- print(data, base)- to print the data on the LCD screen. The base argument is optional, which determines the base in which the data should be printed as binary(base 2), decimal(base 10), octal(base 8), hexadecimal(base 16).
- cursor()- to show the cursor at which the next character will be written.
- no cursor()- to hide the cursor()
- blink()- it makes the cursor blinks.
- blink()- it should turn off the blinking cursor
- right to left()- it causes the text flow from right to left.
- left to right()- it causes the text flow from left to right.
- auto-scroll()- it should move all the text one space to the left.
- scroll()- it should stop the scrolling of text.
Program to connect the LCD screen with the Arduino Uno
Code to display hello world on 16×2 LCD
#include <LiquidCrystal.h> LiquidCrystal lcd(12, 11, 5, 4, 3, 2); void setup() { lcd.begin(16, 2); lcd.print("hello, world!"); } void loop() { }
Example code for displaying sensor readings on liquid crystal display using Arduino Uno
// include the library code: #include <LiquidCrystal.h> // initialize an object with the numbers of the interface pins LiquidCrystal lcd(R/W,EN,D4,D5,D6,D7); int data1 = A1;//analog pins int data2 = A2; void setup() { // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.clear(); pinMode(data1, INPUT); pinMode(data2, INPUT); } void loop() { lcd.setCursor(0,0); // Sets the cursor at col 0 and row 0 lcd.print("Sensor1: "); // Prints Sensor1: on LCD lcd.print(analogRead(data1)); // Prints data1 on LCD lcd.setCursor(0,1); // Sets the cursor to col 1 and row 0 lcd.print("Sensor2: "); // Prints Sensor2: on LCD lcd.print(analogRead(data2)); // Prints data2 on LCD }
Common errors with the interfacing of the Arduino and the LCD screen and their troubleshooting
Some of the common problems arising while interfacing Arduino Uno with LCD screen are listed below.
There is no problem with data pins of LCD. It could be connected with any of the Arduino’s digital pins, which should be declared in the program.
It is necessary to connect a potentiometer with Vout to control the backlight. If you didn’t connect the potentiometer, the screen would appear shady.
A resistor of 1kΩ or 3.9kΩ should be connected with the LED+ Anode pin of the LCD to control the contrast. Otherwise will fall into some issues like not showing the data clearly.
Liquid crystal display requires an input voltage Vcc in the range between 4.7V to 5.3V. Fluctuations in the voltage given by 5V Vcc out pin of Arduino may cause problems in getting an output even if the connections are correct. To avoid this issue, use a stable power supply for Arduino and check whether there is fluctuation in 5V Vcc out the pin. In this case, use resistors with the corresponding ohm.
Many users prefer to connect 10kΩ with anode pin of a backlight in LCD, but it works well with a 1kΩ resistor too.
Applications of the 16×2 LCD
- The liquid crystal displays (LCDs) are used in aircraft cockpit displays.
- It is used as a display screen in calculators.
- We can also use the LCD to display images in digital cameras.
- You can find its use extensively in Arduino courses. We will use it in the upcoming posts to display temperature and humidity sensor data using an Arduino.
Alternatives to using a 16×2 LCD
You can use a seven-segment display instead of an LCD. Those look way sharper and brighter. Check out our post on connecting a seven-segment display with an Arduino Uno.
Were you able to successfully build this project? If you have any queries or doubts, let us know in the comments section below, and we would love to help you out.