4 Pin LCD
4 Pin LCD
4 Pin LCD
(/wiki) (/shop)
8051
As per the name the 2x16 has 2 lines with 16 chars on each line. It supports all the ASCII chars
and is basically used for displaying the alphanumeric characters. Here each character is
displayed in a matrix of 5x7 pixels. Apart from alphanumeric chars it also provides the
provision to display the custom characters by creating the pattern.
LCD UNIT
Let us look at a pin diagram of a commercially available LCD like JHD162 which uses a
HD44780 controller and then describe its operation.
(/wiki/File:Pic16f877aLcdInterface.png)
1 VSS Ground
2 VCC +5v
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 1/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials
11 D4 Data Bit 4
12 D5 Data Bit 5
13 D6 Data Bit 6
Apart from the voltage supply connections the important pins from the programming
perspective are the data lines(8-bit Data bus), Register select, Read/Write and Enable pin.
Data Bus: As shown in the above gure and table, an alphanumeric LCD has an 8-bit data bus
referenced as D0-D7. As it is an 8-bit data bus, we can send the data/cmd to LCD in bytes. It
also provides the provision to send the data/cmd in chunks of 4-bit, which is used when there
are limited number of GPIO lines on the microcontroller.
Register Select(RS): The LCD has two register namely a Data register and Command register.
Any data that needs to be displayed on the LCD has to be written to the data register of LCD.
Command can be issued to LCD by writing it to Command register of LCD. This signal is used
to differentiate the data/cmd received by the LCD.
If the RS signal is LOW then the LCD interprets the 8-bit info as Command and writes it
Command register and performs the action as per the command.
If the RS signal is HIGH then the LCD interprets the 8-bit info as data and copies it to data
register. After that the LCD decodes the data for generating the 5x7 pattern and nally
displays on the LCD.
Read/Write(RW): This signal is used to write the data/cmd to LCD and reads the busy ag of
LCD. For write operation the RW should be LOW and for read operation the R/W should be
HIGH.
Enable(EN): This pin is used to send the enable trigger to LCD. After sending the data/cmd,
Selecting the data/cmd register, Selecting the Write operation. An HIGH-to-LOW pulse has to
be sent on this enable pin which will latch the info into the LCD register and triggers the LCD
to act accordingly.
Schematic
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 2/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials
Below schematic shows the minimum connection required for interfacing the LCD with the
microcontroller.
As we are interfacing the LCD in 4-bit mode, only the higher 4 data lines are used as the data
bus.
Port Connection
This section shows how to con gure the GPIO for interfacing the LCD.
The below con guration is as per the above schematic. You can connect the LCD to any of the
PORT pins available on your boards and update this section accordingly
1 /* Configure the data bus and Control bus as per the hardware connection
2 Databus is connected to P2.4:P2.7 and control bus P2.0:P2.2*/
3 #define LcdDataBus P2
4 sbit LCD_RS = P2^0;
5 sbit LCD_RW = P2^1;
6 sbit LCD_EN = P2^2;
wan/2c051cee1f15cfd4329a945e2e3fa63e/raw/4704e1ac27a0312aa72478bc8886f8470b3740b5/8051_Lcd4bitConnection.c)
8051_Lcd4bitConnection.c (https://gist.github.com/SaheblalBagwan/2c051cee1f15cfd4329a945e2e3fa63e#file-
8051_lcd4bitconnection-c) hosted with ❤ by GitHub (https://github.com)
LCD Operation
In this section, we are going to see how to send the data/cmd to the LCD along with the timing
diagrams. First let's see the timing diagram for sending the data and the command
signals(RS,RW,EN), accordingly, we write the algorithm and nally the code.
Timing Diagram
The below image shows the timing diagram for sending the data to the LCD.
As shown in the timing diagram the data is written after sending the RS and RW signals. It is
still ok to send the data before these signals.
The only important thing is the data should be available on the databus before generating the
High-to-Low pulse on EN pin.
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 3/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials
(/wiki/File:LCD_CmdWrite.jpg)
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 4/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials
wan/4a521e33274ae73b7b492611ed20d762/raw/c12d802b957fc86e46366c96096c4cb713b6c351/8051_Lcd4BitCmdWrite.c)
8051_Lcd4BitCmdWrite.c (https://gist.github.com/SaheblalBagwan/4a521e33274ae73b7b492611ed20d762#file-
8051_lcd4bitcmdwrite-c) hosted with ❤ by GitHub (https://github.com)
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 5/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials
18 delay(1000);
19 LCD_EN = 0;
20
21 delay(10000);
22 }
wan/19146e69651dc5465bdac99f75c31a29/raw/777921f2b8c0804936a3d8d5d0c73bb7020d7f9b/8051_Lcd4BitDataWrite.c)
8051_Lcd4BitDataWrite.c (https://gist.github.com/SaheblalBagwan/19146e69651dc5465bdac99f75c31a29#file-
8051_lcd4bitdatawrite-c) hosted with ❤ by GitHub (https://github.com)
Hardware Connections
(/wiki/File:Lcd_4-Bit_Tutorial.png)
Code Examples
Here is the complete code for displaying the data on 2x16 LCD in 4-bit mode.
1 #include<reg51.h>
2 /* Configure the data bus and Control bus as per the hardware connection
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 6/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 7/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 8/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials
87 Lcd_DataWrite(' ');
88 Lcd_DataWrite('w');
89 Lcd_DataWrite('o');
90 Lcd_DataWrite('r');
91 Lcd_DataWrite('l');
92 Lcd_DataWrite('d');
93
94 Lcd_CmdWrite(0xc0); //Go to Next line and display Good Morning
95 for(i=0;a[i]!=0;i++)
96 {
97 Lcd_DataWrite(a[i]);
98 }
99
100 while(1);
101 }
gwan/3827aa5155b70f2745ea53ef2dd44574/raw/79cc2b57123fc8a91908f8d4630539c1806677e3/8051_Lcd4BitExample1.c)
8051_Lcd4BitExample1.c (https://gist.github.com/SaheblalBagwan/3827aa5155b70f2745ea53ef2dd44574#file-
8051_lcd4bitexample1-c) hosted with ❤ by GitHub (https://github.com)
The below sample code shows how to use the already available LCD functions.
LCD 1x16
1 #include "lcd.h"
2
3 int main()
4 {
5 /*Connect RS->P2.0, RW->P2.1, EN->P2.2 and data bus to P2.4 to P2.7*/
6 LCD_SetUp(P2_0,P2_1,P2_2,P_NC,P_NC,P_NC,P_NC,P2_4,P2_5,P2_6,P2_7);
7 LCD_Init(1,16);
8
9 LCD_DisplayString("Explore Embedded\n");
10 while(1);
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 9/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials
11
12 return (0);
13 }
SaheblalBagwan/3329928508b185f1fad661a07f28d6c6/raw/59607f067c4c1f0297081043292ed0abff4862ff/8051_lcd_1x16.c)
8051_lcd_1x16.c (https://gist.github.com/SaheblalBagwan/3329928508b185f1fad661a07f28d6c6#file-8051_lcd_1x16-c)
hosted with ❤ by GitHub (https://github.com)
LCD 2x16
1 #include "lcd.h"
2
3 int main()
4 {
5 /*Connect RS->P2.0, RW->P2.1, EN->P2.2 and data bus to P2.4 to P2.7*/
6 LCD_SetUp(P2_0,P2_1,P2_2,P_NC,P_NC,P_NC,P_NC,P2_4,P2_5,P2_6,P2_7);
7 LCD_Init(1,16);
8
9 LCD_DisplayString("Explore Embedded");
10 LCD_GoToLine(1);
11 LCD_DisplayString("Lcd 4-Bit mode");
12 while(1);
13
14 return (0);
15 }
heblalBagwan/05cb28c2b0ee15b69bd630c498e1bcdb/raw/b1c87916efcb0d7ff8565d682d331eeec3e2f0ef/8051_lcd_2x16.c)
8051_lcd_2x16.c (https://gist.github.com/SaheblalBagwan/05cb28c2b0ee15b69bd630c498e1bcdb#file-8051_lcd_2x16-c)
hosted with ❤ by GitHub (https://github.com)
LCD 4x20
1 #include "lcd.h"
2
3 int main()
4 {
5 /*Connect RS->P2.0, RW->P2.1, EN->P2.2 and data bus to P2.4 to P2.7*/
6 LCD_SetUp(P2_0,P2_1,P2_2,P_NC,P_NC,P_NC,P_NC,P2_4,P2_5,P2_6,P2_7);
7 LCD_Init(4,20);
8
9 LCD_DisplayString("Explore Embedded\n");
10 LCD_DisplayString("LCD 4-bit Mode\n");
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 10/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials
11 LCD_DisplayString("20 x 4 \n");
12 LCD_DisplayString(":) :O");
13 while(1);
14
15 return (0);
16 }
heblalBagwan/7c4da4bc35c7ffc5e964e1c31c673497/raw/582f1db625d0b5633e9550afbd2ef1bebe1635f6/8051_lcd_4x20.c)
8051_lcd_4x20.c (https://gist.github.com/SaheblalBagwan/7c4da4bc35c7ffc5e964e1c31c673497#file-8051_lcd_4x20-c)
hosted with ❤ by GitHub (https://github.com)
(/wiki/File:01LCD_4bit.png)
Downloads
Download the sample code and design les from this link
(https://github.com/ExploreEmbedded/8051_DevelopmentBoard).
Have an opinion, suggestion , question or feedback about the article let it out here!
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 11/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials
4 Comments exploreembedded.com/wiki
1 Login
LOG IN WITH
OR SIGN UP WITH DISQUS ?
Name
Could be delay problem. Don't check the busy flag. Give some extra
delay after each nibble write and char write.
△ ▽ • Reply • Share ›
ALSO ON EXPLOREEMBEDDED.COM/WIKI
AWS IOT with Arduino ESP32 FunTime, a DIY wall clock that doubles up
22 comments • a year ago as automatic night lamp
prerana gawale — I'm getting following 1 comment • 2 years ago
Error:Connected to wifiE (14135) aws_iot: Sandeep Patil — Test comment
f il d! b d l d
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 12/13
10/5/2018 Interfacing LCD in 4-bit mode with 8051 - Tutorials
name@example.com SUBSCRIBE
Contact (/contact) About (/about) Warranty (/refund) Terms & Conditions (/terms) Reward points (/rewards)
(https://twitter.com/exploreembedded) (https://www.facebook.com/ExploreEmbedded/)
(https://www.youtube.com/channel/UCvXGpvPuosEI-ALxvCrSbaA) (https://github.com/ExploreEmbedded)
https://exploreembedded.com/wiki/Interfacing_LCD_in_4-bit_mode_with_8051 13/13