In this article, you will learn how to interface LCD with an 8051 microcontroller. It is not very hard for interfacing LCD with an 8051 microcontroller if you already know how to use Keil for programming of 8051 and how to use input-output ports of the 8051 microcontroller. LCD is used for displaying alphabets, numbers or messages, etc.
16×2 LCD is very simple and easy to understand for beginners and is commonly used in several electronic products. LCD (Liquid Crystal Display) provides a user-friendly interface and can be very useful for debugging purposes. After completion of this tutorial, you will be able to display data on an LCD using 8051 and Keil compiler.
The reason LCD is more popular than LED, Seven Segment displays. Because we can display characters, numbers, and custom characters with ease ( Just by easily programming a module).
Why do We Need to Interface LCD with 8051?
The first question that must come to your mind is that why we need to interface LCD with 8051? There are many applications in embedded projects in which wants to display different types of data on LCD. For example, it is used to display different sensors data such as temperature, humidity, pressure, light intensity, voltage, current and many others. Hence, the use of liquid crystal displays is very popular and common in 8051 project. It is also recommended to learn LCD interfacing at the start. Because when you learn to interface sensors with 8051, you will already have a coding experience to display that sensor value.
Liquid Crystal Display Introduction
As its name suggests LCDs are electronic devices that are used to display texts, custom characters, and numbers. Although, we can also display pictures on LCDs. But results will be not comparable to graphical LCDs (GLCDs). GLCDs are used to display images. Coming back to LCDs, they are available in different sizes and features. For example, 16×2, 20×2, and 16×1 are different sizes available.
Types
They come in two types serial and parallel LCDs. Serial type uses serial communication, such as the UART module, to interface with 8051. They are easier to interface with 8051 than parallel LCDs. But they are expensive. In contrast, parallel LCDs, for example, Hitachi HD44780, are exceptionally used and interfacing with 8051 is done using digital I/O pins of 8051. For instance, Hitachi HD44780 type 16×2 LCD can be interfaced with 8051 using 4-8 data lines and a few control pins of the display. In this tutorial, we will be using 16×2 parallel LCD only. Because it is the most popular choice among the 8051 community and embedded application developers.
16×2 LCD Introduction
We can use any type of LCD like 16×2, 8×1, 16×4, 8×2, 16×1, 20×1, 20×2 etc. Here we will use 16×2 Liquid Crystal Display. It can display 32 characters at a time in two rows. LM016L is a 16×2 LCD module.
There are two types of pins on the whole 16×2 LCD module. Some pins are used to send to 16×2 LCD and some are command pins. In other words, every pin has a role in controlling a single pixel on the display.
16 x 2 LCD has sixteen columns and two rows. That means, it can display sixteen characters per row and it has two such rows. Similarly, a 20×4 LCD has four rows and 20 columns. That means, it can display 20 characters per row.
Pinout Diagram
The diagram shows the pin configuration of the 16×2 display. It has sixteen pins.

D0 – D7: Pin numbers 7-14 are data bus lines that are used to send data from 8051 which you want to display on LCD. With these 8 data lines, data can be transferred either in an 8-bit format or in a 4-bit format. In a 4-bit format, only the upper four bits (D4-D7) are used to send data from 8051 microcontroller to LCD. The full byte is transmitted in two successive transmissions. A 4-bit format is used to save GPIO pins of 8051. Because fewer GPIO pins of 8051 will be required to transfer data.
Contrast Select (VEE): Pin3 will connect with power and ground through 3 pin potentiometers. It will help to control the contrast of PIXELS according to the 16X2 LCD light. 10K ohm variable resistor is connected with the VEE pin to adjust light contrast. Variable resistor one side is connected with 5 volts and the other side is connected with ground. The third terminal is connected with the VEE pin.
RS: This pin is known as a register select pin. It helps to toggle the command/data register.
R/W: The signal on Pin5 will decide whether it is going to read from LCD or write on it.
EN: Enable pin will help to transfer the instruction from the data pins and another command pin to the LCD. It act as permission to internal registers.
VSS: It’s a ground pin for common grounds.
VDD: The power pin will use for voltage input to the 16X2 LCD.
16×2 LCD Registers and Commands
EN pin is for enabling the module. A high to low transition at this pin will enable the LCD module. ThisLM016L has two built in registers namely:
- Data register
- Command register
Data register: It is for placing the data which is to be displayed. Data can be any character, alphabet or number.High logic at the RS pin will select the data register. By making RS pin high and putting data in the 8 bit data line (DB0 to DB7), the LCD module will recognize it as a data to be displayed.
Command register:It is for placing the commands. There is a set of commands for LCD to perform specific tasks. Low logic at the RS pin will select the command register.By making RS pin low and putting data on the data line, the LCD module will recognize it as a command.Some of the instructions/commands are given below:
Code (in Hex) Working of LCD commands
0x01 Clear displays
0x02 return home
0x06 for entry mode
0x80 Force cursor to beginning of 1st line
0xC0 Force cursor to beginning of 2nd line
0x90 Force cursor to beginning of 3rd line
0xD0 Force cursor to beginning of 4th line
0x08 Display off, cursor off
0x0E Display on, cursor on
0x0C Display on, cursor off
0x0F Display on, cursor blinking
0x10 Shift cursor position to left
0x14 Shift cursor position to right
0x38 2 lines and 5×7 matrix (8-bit mode)
0x28 2 lines and 5×7 matrix (4-bit mode)
R/W pin is for selecting between read and write modes. A high level at this pin enables read mode and a low level at this pin enables write mode.
DB0 to DB7 are the data pins. The data to be displayed and the commands are placed on these pins.
For glowing backlight LED, LED+ (anode of back light LED)is connected to Vcc through a suitable series current limiting resistor (for contrast adjustment). LED-(cathode of the back light LED) is connected to ground.
How to Interface 16X2 LCD with 8051 Microcontroller?
First of all, to interface LCD with an 8051, we used GPIO pins. GPIO pins are general-purpose input-output pins. Because we send control and data signals to LCD through these I/O pins. Therefore, you should know how to use the digital input-output pins of the 8061 To know about GPIO pins programming, check these tutorials:
- HOW TO WRITE FIRST PROGRAM IN KEIL FOR 8051
- How to use input-output ports 8051 microcontroller|LED blinking
This LCD can be operated in 4-bit mode (using only 4 data lines) or 8-bit mode (using all 8 data lines). Here we will useit in 8-bit mode. First of all LCD is initialized and then it can be used for sending data and commands. Reset and Enable of LCD is connected to port 1 of 8051 microcontroller. Data lines of LCD are connected to port 2 of 8051 microcontroller. R/W is connected to ground since we have to just write data and command (not read operation). This gives a little ease so that we don’t need to make this pin low in the code.
Commands to send Data to LCD with 8051
To initialize LCD with 8051 microcontroller, following instruction are to be executed:
- 0x38Â Â Â (is used for 8-bit data initialization)
- 0x0CÂ Â (display on, cursor off)
- 0x01Â Â Â (for clearing screen)
- 0x80Â Â Â (force cursor to beginning of 1st line)
Sending Data Command
For displaying any character whether it is number, alphabet or character, following steps should be follow:
- For display data, register select (RS pin) should be high, RS = 1.
- Place data byte on the data register.
- Pulse the Enable pin (EN pin) from high to low.
- Configure the R/W to write mode. For write mode,R/W = 0.
- Repeat above steps for sending another data.
TO SEND COMMAND:
To instruct LCD for performing specific task for example displaying character in second row instead of one, following steps should be taken:
- For command mode, register select (RS pin) should be low, RS = 0.
- Place data byte on the command register.
- Pulse the Enable pin (EN pin) from high to low.
- Read/Write should be low (write mode), R/W = 0.
- Repeat above steps for sending another command.
8051 16×2 LCD Code
#include<reg51.h> voidlcd_init(void); voidwritecmd(int); voidwritedata(char); void delay(int); sbit RS = P1^0; sbit E = P1^1; sbit led = P1^2; inti=0; void main() { P0 = 0x00; //not used P1 = 0x00; //output port for setting RS and EN P2 = 0x00; //used as data output port P3 = 0x00; //not used led = 1; lcd_init(); writedata('W'); delay(5000000); writedata('e'); delay(5000000); writedata('l'); delay(5000000); writedata('c'); delay(5000000); writedata('o'); delay(5000000); writedata('m'); delay(5000000); writedata('e'); delay(5000000); writedata(' '); delay(5000000); writedata('T'); delay(5000000); writedata('o'); delay(5000000); writecmd(0x01); //clear display writedata('w'); writedata('w'); writedata('w'); writedata('.'); writedata('m'); writedata('i'); writedata('c'); writedata('r'); writedata('o'); writedata('c'); writedata('o'); writedata('n'); writedata('t'); writedata('r'); writedata('o'); writedata('l'); writecmd(0xc0); //enter in second row writedata('l'); writedata('e'); writedata('r'); writedata('s'); writedata('l'); writedata('a'); writedata('b'); writedata('.'); writedata('c'); writedata('o'); writedata('m'); delay(5000000); delay(5000000); delay(5000000); delay(5000000); } void lcd_init(void) { writecmd(0x38); //for 8 bit mode writecmd(0x0C); //display on, cursor off writecmd(0x01); //clear display writecmd(0x80); //force cursor to beginning of 1st line } void writedata(char t) //data function { RS = 1; P2 = t; //Data transfer E = 1; delay(150); E = 0; delay(150); } voidwritecmd(int z) //command function { RS = 0; P2 = z; //Data transfer E = 1; delay(150); E = 0; delay(150); } void delay(int a) //Delay function { inti; for(i=0;i<a;i++); }
Thanks for the invaluable information. But I have a question for you, how much second is the delay given by delay(5000000) and delay(150)?
Thanks in advance for your feedback!
could you send me interfacing pir.gsm.buzzer with 8051 microcontroller