Interfacing PIC Microcontroller with LCD Display – MPLAB XC8

This tutorial shows how to connect and interface 8-bit PIC microcontroller family with LCD (Liquid Crystal Display) screens based on HD44780U or compatible controller. Even it is very old, the HD44780U controller or similar is widely used in character type LCD displays which known as 16×2 LCD and 20×4 LCD. A 16×2 (also known as 1602) means that the display has 16 column & 2 rows and it can show up to 32 characters, whereas a 20×4 (also known as 2004) has 20 columns & 4 rows and can show up to 80 characters.
In this post we’ll see how to interface 1602 and 2004 LCDs with different types of Microchip 8-bit PIC microcontrollers, MPLAB XC8 compiler will be used for all examples.

About the HD44780U controller based LCD:
The HD44780U is an integrated circuit (IC) manufactured by Hitachi that is used to control liquid crystal displays (LCDs). It was developed in the 1980s and is still widely used today because of its low cost, versatility, and ease of use.
This type of  LCD comes in different sizes depending of number of characters the display can show, the most common types are 16×2 and 20×4.
Some characteristics and features of this type of displays are listed below:

  • Character display: The HD44780U controller based LCD modules are primarily designed for displaying alphanumeric characters, symbols, and some other graphical elements.
  • Display size: The most popular LCD sizes are the 16×2 (16 characters and 2 lines –> total 32 characters) and 20×4 (20 characters and 4 lines –> total 80 characters), other size are also available such as 16×1…
  • Display Interfacing: The HD44780U based display modules typically interface with microcontrollers using either a 4-bit or 8-bit parallel interface. It may be also interfaced using I2C or SPI protocol using another additional module.
  • Backlight: Most displays come with integrated backlight LED that improves the visibility in low-light conditions.
  • Widely used: Even it is very old type of display (more than 30 years) but it still used in industrial applications, hobbyist and educational projects, instrumentation, consumer electronics…
  • Simple Commands: The HD44780U controller based LCD module has a set of very simple commands which makes it very simple to interface with the microcontroller system.

The most popular types of the HD44780 controller display are shown below:

HD44780U controller based LCD display 2004 and 1602
HD44780U controller based LCD display 2004 and 1602

HD44780 Controller LCD pinout:
The 16×2 and 20×4 display modules have the same total number of pins which is 16, they also have the same pinout as described below:

  • 1. VSS:  Also known as GND (ground), it is ground connection of the LCD module
  • 2. VDD: Also known as VCC and it is power supply positive terminal of the module
  • 3. V0: Also known as VEE, this pin is used to control the contrast of the display, it is usually connected to a potentiometer or variable resistor output
  • 4. RS (Register Select): Selects whether data is being sent to the display is a command (instruction) or a display data. A logic low for command and logic high for display data
  • 5. R/W (Read/Write): This pin selects whether data is being read from the display or written to it. Logic high for Read operation and logic low for Write operation. If just write operations are required then this pin could be connected directly to GND
  • 6. E (Enable): A pulse input to the enable pin triggers Read/Write operation of the display whatever it is a command or display data
  • 7 -10. DB0 to DB3: Four low order bidirectional data pins, used for data transfer between the microcontroller and the LCD. These pins are used only in 8-bit operation and they are not used in 4-bit mode.
  • 11-14. DB4 to DB7: Four high order bidirectional data pins, used for data transfer between the microcontroller and the LCD
  • 15. A (Anode): This pin is the anode terminal of the backlight LED
  • 16. K (Cathode): This is the cathode terminal pin of the backlight LED

Note that the traditional 16×2 and 20×4 LCD modules require power supply with voltage ranging from 4.5V to 5.5V (typically 5V), 3.3V is not sufficient for this type of displays. But when the display is powered with 5V it can read logic high inputs with voltage not less than 2.2V, which means it will work properly with a 3.3V microcontroller if it is supplied with 5 Volts between its VDD and GND pins as what is shown in the example below:
Interfacing STM32 Blue Pill with 1602 LCD

Example 1:
Interfacing PIC16F877A microcontroller with 16×2 LCD:
In the first example, mid-range 8-bit microcontroller PIC16F877A is used. Although more than 20 years is passed from the release of this MCU but it is still in production by Microchip Technology. This microcontroller can run with a maximum clock frequency of 20MHz and achieves 5 MIPS (Million Instruction Per Second). It is very easy to start with which makes it a good choice for beginners who tend to learn PIC microcontrollers,  and also it is still used in many application especially educational projects as it contains many integrated peripherals such as:

  • 10-Bit Analog-to-Digital Converter (ADC)
  • 2 x 10-bit PWM (Pulse Width Modulation) outputs
  • 1 x USART (Universal Synchronous Asynchronous Receiver Transmitter)
  • 1 x I2C (Inter-Integrated Circuit)
  • 1 x SPI (Serial Peripheral Interface)
  • 2  x 8-bit Timers (Timer 0 and Timer 1)
  • 1 x 16-bit Timer (Timer 1)
PIC16F877A Microcontroller with 1602 LCD
PIC16F877A Microcontroller with 1602 LCD

PIC16F877A MCU with 16×2 Connection Circuit diagram:
The image below shows a basic circuit schematic of the example, only few components are required.

Interfacing PIC16F877A microcontroller with 16x2 LCD HD44780 controller MPLAB XC8 compiler
Interfacing PIC16F877A microcontroller with 16×2 LCD based on HD44780 controller (4-bit mode)

As the PIC16F877A microcontroller has no internal oscillator circuit, a crystal oscillator labeled as Y1 with 8MHz is used as a clock source for the microcontroller. The MCLR pin of the microcontroller is connected to +5V through 10k Ohm resistor (R1).
The microcontroller is supplied with 5 Volts where the positive terminal of the power source is connected to VDD pins (#11 & #32) and the negative terminal is connected to VSS pins (#12 & #32).

The LCD used in this example has 2 lines and 16 columns, its connection is as described below:
VDD & VSS pins are power supply pins of the LCD module where VDD is connected to positive terminal of the power supply and VSS is connected to negative terminal.
VEE Pin (also known as V0) of the LCD module is connected to a 10k Ohm variable resistor output (R2). The usage of the variable resistor allows us to easily control the contrast of the LCD display.
RS (Register Select) Pin is connected to pin RD0 of PIC16F877A microcontroller.
RW (Read/Write) Pin is connected to ground.
E (Enable) Pin of the LCD is connected to pin RD1 of the PIC16F877A MCU.
Data pins, D4, D5, D6, & D7 are respectively connected to MCU pins RD2, RD3, RD4, & RD5.
A and K pins of the LCD are backlight LED anode and cathode terminal, they are respectively connected to GND and +5V.
Pins D0, D1, D2, & D3 are not connected pins because 4-bit mode is used, alternatively, they can be connected to ground.

PIC16F877A MCU with 1602 LCD display MPLAB XC8 compiler C code:
Microchip MPLAB XC8 compiler v2.40 is used in this example, all MPLAB project files including LCD library files can be accessed through the below GitHub link. The readme file in the GitHub link gives all library functions.
Main files of MPLAB XC8 library (driver) for LCD are: lcd.h which is library header file, and lcd.c which is library source code file.

LCD Library for MPLAB XC8 – GitHub

C Code description:
The first lines of example C code is the include of some files including LCD library header file.

The first file config.h contains the setting (values) of the configuration registers with detailed comments. The PIC16F877A microcontroller has one configuration word mapped at address location 2007h.
The second included file is xc.h and it is main compiler header file, it must be included in any project.
The third file is stdio.h which is header file for standard I/O functions, for example the well used printf function.
The last file is lcd.h and it is header file of the LCD library.
As an addition to included files, the clock frequency have to be defined and in our example 8 MHz external crystal oscillator is used.

For the definition of the connection between the microcontroller and the LCD display, another function in main code is used:

This simple example shows how to print texts on a 1602 LCD display. The below C code is the main code only, you can find complete MPLAB project files in above GitHub link.

Interfacing PIC16F877A with 16×2 LCD Proteus Simulation Video:
Simulation should be performed before the realization of any project if the required tools are available. Proteus simulation software contains a huge library for electronic circuit simulation and PCB design, the simplicity of its use makes it very popular, especially among students and hobbyists.
I’m adding the following short time video to help understand example much more.

Proteus simulation file download:
Simulation file can be downloaded from the link below, use Proteus version 8.15 or higher to open it.
PIC16F877A + 1602 LCD

Example 2:
Interfacing PIC18F46K22 microcontroller with 16×2 LCD:
In the second example, PIC18F46K22 is used, this MCU is also an 8-bit microcontroller but it is more advanced than the PIC16F877A.
For more information about the PIC18F46K22 microcontroller just go to its datasheet.

PIC18F46K22 Microcontroller with 1602 LCD hardware circuit example
PIC18F46K22 Microcontroller with 1602 LCD hardware circuit example

PIC18F46K22 MCU with 16×2 Connection Circuit diagram:
The image below shows a basic circuit schematic of the example, only few components are required.

Interfacing PIC18F46K22A microcontroller with 1602 LCD HD44780U circuit controller MPLAB XC8 compiler
Interfacing PIC18F46K22 microcontroller with 1602 LCD

In this example, 5V source is used to power up the PIC18F46K22 MCU and the 1602 LCD display.
For the microcontroller, the positive terminal of the power source is connected to VDD pins (#11 & #32) and the negative terminal is connected to VSS pins (#12 & #32).
The PIC18F46K22 has an internal oscillator which makes the circuit more simpler. External reset functionality of the microcontroller is disabled and RE3 pin (#1) is configured as an input pin.

The LCD used in this example has 2 lines and 16 columns, its connection is as described below:
VDD & VSS pins are power supply pins of the LCD module where VDD is connected to positive terminal of the power supply and VSS is connected to negative terminal.
VEE Pin (also known as V0) of the LCD is connected to a 10k Ohm variable resistor output (R1). The usage of the variable resistor allows us to easily control the contrast of the LCD display.
RS (Register Select) Pin is connected to pin RD0 of PIC18F46K22 microcontroller.
RW (Read/Write) Pin is connected to ground.
E (Enable) Pin of the LCD is connected to pin RD1 of the PIC18F46K22 MCU.
Data pins, D4, D5, D6, & D7 are respectively connected to MCU pins RD2, RD3, RD4, & RD5.
A and K pins of the LCD are backlight LED anode and cathode terminal, they are respectively connected to GND and +5V.
Pins D0, D1, D2, & D3 are not connected pins because 4-bit mode is used, alternatively, they can be connected to ground.

PIC18F46K22 Microcontroller with 1602 LCD display MPLAB XC8 compiler C code:
Microchip MPLAB XC8 compiler v2.40 is used in this example, all MPLAB project files including LCD library files can be accessed through the below GitHub link. The readme file in the GitHub link gives all library functions.
Main files of MPLAB XC8 library (driver) for LCD are: lcd.h which is library header file, and lcd.c which is library source code file.

LCD Library for MPLAB XC8 – GitHub

C Code description:
The first lines of example C code is the include of some files including LCD library header file.

The first file config.h contains the setting (values) of the configuration registers of the PIC18F46K22 microcontroller.
The second included file is xc.h which is main compiler header file, it must be included in any project.
The third file is stdio.h which is header file for standard I/O functions, for example the well used printf function.
The last file is lcd.h and it is header file of the LCD library. The
As an addition to included files, the clock frequency have to be defined and in our example 8 MHz external crystal oscillator is used.

For the definition of the connection between the microcontroller and the LCD display, another function in main code is used:

This simple example shows how to print characters, texts, and numbers on a 1602 LCD display. The below C code is the main code only, you can find complete MPLAB project files in above GitHub link.

Interfacing PIC18F46K22 MCU with 16×2 LCD Proteus Simulation Video:
The video below shows the simulation of this example using Proteus ISIS simulation software.

Proteus simulation file download:
Simulation file can be downloaded from the link below, use Proteus version 8.15 or higher to open it.
PIC18F46K22 + 1602 LCD

Example 3:
Interfacing PIC18F46K42 microcontroller with 16×2 LCD:
In the last example, a more newer and advanced 8-bit microcontroller is used which is PIC18F46K42.
For more information about the Microchip PIC18F46K42 microcontroller just go to its datasheet.

In this example two potentiometer connected to PIC18F46K42 microcontroller, POT 1 and POT 2, where the first potentiometer output is connected to analog channel ANA0 (pin #2) and the second potentiometer output is connected to analog channel ANA1 (pin #3).

Interfacing Microchip PIC18F46K42A microcontroller with 2004 LCD HD44780U circuit controller MPLAB XC8 compiler library
Interfacing Microchip PIC18F46K42A microcontroller with 2004 LCD

The PIC18F46K42 microcontroller has a built-in analog-to-digital converter (ADC) with resolution of 12 bits. This means digital output value varies between 0 and 4095.
Since the ADC positive reference voltage is VDD (+5V) and negative reference voltage is VSS (0V), a 0V applied to an analog channel is represented by a digital value of 0 and +5V is represented by 4095.

The PIC18F46K42 mcu is powered with 5V where the positive terminal of the power source is connected to VDD pins (#11 & #32) and the negative terminal is connected to VSS pins (#12 & #32).
The PIC18F46K42 has an internal oscillator which makes the circuit more simpler. External reset functionality of the microcontroller is disabled and RE3 pin (#1) is configured as an input pin.

In this example the digital representation of analog output voltage of POT 1 and POT 2 is printed on a 20×4 LCD screen which is connected as follows:
VDD & VSS are power supply pins of the LCD module where VDD is connected to positive terminal of the power supply and VSS is connected to negative terminal.
VEE Pin (also known as V0) of the LCD is connected to a 10k Ohm variable resistor output (R1). The usage of the variable resistor allows us to easily control the contrast of the LCD display.
RS (Register Select) Pin is connected to pin RD0 of PIC18F46K42 microcontroller.
RW (Read/Write) Pin is connected to ground.
E (Enable) Pin of the LCD is connected to pin RD1 of the PIC18F46K42 MCU.
Data pins, D4, D5, D6, & D7 are respectively connected to MCU pins RD2, RD3, RD4, & RD5.
A and K pins of the LCD are backlight LED anode and cathode terminal, they are respectively connected to GND and +5V.
Pins D0, D1, D2, & D3 are not connected pins because 4-bit mode is used, alternatively, they can be connected to ground.

Interfacing PIC18F46K42 mcu with 20x4 LCD prototype circuit
Interfacing PIC18F46K42 mcu with 20×4 LCD prototype circuit

Interfacing PIC18F46K42 Microcontroller with 20×4 LCD display MPLAB XC8 compiler C code:
Microchip MPLAB XC8 compiler v2.40 is used in this example, all MPLAB project files including LCD library files can be accessed through the below GitHub link. The readme file in the GitHub link gives all library functions.
Main files of MPLAB XC8 library (driver) for LCD are: lcd.h which is library header file, and lcd.c which is library source code file.

LCD Library for MPLAB XC8 – GitHub

C Code description:
The first lines of example C code is the include of some files including LCD library header file.

The first file config.h contains the setting (values) of the configuration registers of the PIC18F46K22 microcontroller.
The second included file is xc.h which is main compiler header file, it must be included in any project.
The third file is stdio.h which is header file for standard I/O functions, for example the well used printf function.
The last file is lcd.h and it is header file of the LCD library. The
As an addition to included files, the clock frequency have to be defined and in our example 8 MHz external crystal oscillator is used.

For the definition of the connection between the microcontroller and the LCD display, another function in main code is used:

This LCD example shows how to print characters and texts on the display. It also shows how to read data from analog channel and print its digital representation on the LCD. Main example code is below, full project files including LCD library (driver) for MPLAB XC8 compiler in the above GitHub link.

Interfacing PIC18F46K42 microcontroller with 16×2 LCD Video:
This example should work as shown in the following video with DIY hardware circuit:


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top