Interfacing I2C LCD with PIC microcontroller | MPLAB Projects

Interfacing LCD display with a PIC microcontroller requires at least 6 data pins (for LCD pins: RS, E, D4, D5, D6 and D7). Number of pins needed for the LCD can be reduced to 2 by using an I2C I/O (Input/Output) expander like PCF8574 or PCF8574A. Adding the I2C I/O expander builds an I2C LCD. The I2C LCD is connected with the microcontroller via 2 lines: SDA (serial data) and SCL (serial clock). I2C = IIC = Inter-Integrated Circuit.

This post shows an example for connecting LCDs (with HD44780 or complaint controller) with Microchip PIC12F1822 8-bit microcontroller, this microcontroller has only 8 pins of which 5 can be used as outputs, therefore an I2C LCD is required.
This project works also with DFRobot I2C LCD displays.

The compiler used in this project is Microchip MPLAB XC8 (MPLAB X IDE with MPLAB XC8 compiler). An I2C LCD driver for the MPLAB XC8 compiler is required to simplify example C code. This driver is just a C file named (with extension) I2C_LCD.c, we’ve to add this file to project folder, download link is below:
I2C LCD driver for MPLAB XC8 compiler

Related Project:
Interfacing LCD with PIC microcontroller | MPLAB Projects

Hardware Required:
The components listed below are required for this project.

  • PIC12F1822 microcontroller  —  datasheet
  • LCD screen (16×2, 20×4 …)
  • PCF8574 I/O expander (or PCF8574A)   —   PCF8574 datasheet
  • 5 x 10k ohm resistor
  • 330 ohm resistor
  • 10k ohm variable resistor or potentiometer
  • 5V source
  • Breadboard
  • Jumper wires

PIC12F1822 MCU with I2C LCD circuit

Interfacing I2C LCD with PIC microcontroller circuit:
Project circuit schematic diagram is shown below.

PIC12F1822 I2C LCD circuit with PCF8574

(All grounded terminals are connected together)

The PIC12F1822 microcontroller has one hardware I2C module with SDA on pin RA2 (#5) and SCL on pin RA1 (#6). SDA and SCL pins of the PIC12F1822 MCU are connected to SDA (#15) and SCL (#14) pins of the PCF8574 I/O expander respectively.

A2, A1 and A0 pins of the PCF8574 are connected to +5V (each one through 10k resistor) which means the I2C address of the PCF8574 (therefor the I2C LCD) is: 0 1 0 0 A2 A1 A0 0 = 0 1 0 0 1 1 1 0 = 0x4E. The 10k ohm resistors for A2, A1 and A0 are optional and each pin can be connected directly to +5V.

PCF8574A can be used instead of the PCF8574, the I2C address becomes: 0x7E.

In this project the PIC12F1822 microcontroller runs with its internal oscillator @ 8 MHz, MCLR pin is configured as an input pin.

Interfacing I2C LCD with PIC microcontroller C code:
The C code below is for MPLAB XC8 compiler, it was tested with version 2.00 installed on MPLAB X IDE version 5.05.

The I2C LCD driver file is included with the line:
#include “I2C_LCD.c”

The hardware I2C module of the PIC12F1822 is initialized with a clock frequency of 100KHz (100000Hz):
I2C_Init(100000);

The I2C LCD is initialized with an I2C address of 0x4E:
LCD_Begin(0x4E);

The microcontroller used in this example is PIC12F1822, configuration words are:

Where:

  • Fail-Safe Clock Monitor enabled
  • Internal/External Switchover mode enabled
  • CLKOUT function is disabled, I/O function on the CLKOUT pin
  • Brown-out Reset (BOR) enabled
  • Data memory code protection disabled
  • Program memory code protection disabled
  • RE3/MCLR pin function is digital input, MCLR internally tied to VDD
  • Power-up Timer (PWRT) disabled
  • Watchdog Timer (WDT) disabled
  • INTOSCIO oscillator: I/O function on CLKIN pin
  • Low voltage programming disabled
  • In-Circuit Debugger disabled
  • Brown-out Reset voltage (Vbor), low trip point selected
  • Stack Overflow or Underflow will cause a Reset
  • 4xPLL disabled
  • Flash Program Memory Self Write disabled

MPLAB XC8 code:


Discover more from Simple Circuit

Subscribe to get the latest posts sent to your email.

14 thoughts on “Interfacing I2C LCD with PIC microcontroller | MPLAB Projects”

  1. Hello, I got this working with 16F887. 07/31/23 Thanks!
    Would like to get 4 Line LCD working next.
    Mike, 78 Years and still trying.

  2. I had the problem everyone had. There needs to be a header file or something with the PIC registers defined.
    Can you tell us how to do it?
    Thanks
    MPLI2C_LCD.c:63:11: error: use of undeclared identifier ‘SSPSTAT’
    while ((SSPSTAT & 0x04) || (SSPCON2 & 0x1F)); // wait for MSSP module to be free (not busy)
    ^
    I2C_LCD.c:63:31: error: use of undeclared identifier ‘SSPCON2’
    while ((SSPSTAT & 0x04) || (SSPCON2 & 0x1F)); // wait for MSSP module to be free (not busy)
    ^
    I2C_LCD.c:64:3: error: use of undeclared identifier ‘SEN’
    SEN = 1; // initiate start condition
    ^
    I2C_LCD.c:69:11: error: use of undeclared identifier ‘SSPSTAT’
    while ((SSPSTAT & 0x04) || (SSPCON2 & 0x1F)); // wait for MSSP module to be free (not busy)
    ^
    I2C_LCD.c:69:31: error: use of undeclared identifier ‘SSPCON2’ABX can’t find these definitions.

  3. Hi buddy. In the article, you talk about functions like LCD_Begin(), LCD_Goto() etc., but your zip files declares functions like LCD_Init(), LCD_Set_Cursor() and so on. So, which the code really should we follow? ?

  4. Cannot get the project to build. Can you please post the entire ZIP file of a working project?

    I2C_LCD.c:55:1: error: (192) undefined identifier “SSPCON”
    I2C_LCD.c:56:1: error: (192) undefined identifier “SSPADD”
    I2C_LCD.c:56:11: error: (192) undefined identifier “_XTAL_FREQ”
    I2C_LCD.c:57:1: error: (192) undefined identifier “SSPSTAT”
    I2C_LCD.c:62:9: error: (192) undefined identifier “SSPSTAT”
    I2C_LCD.c:62:29: error: (192) undefined identifier “SSPCON2”
    I2C_LCD.c:63:1: error: (192) undefined identifier “SEN”
    I2C_LCD.c:68:9: error: (192) undefined identifier “SSPSTAT”
    I2C_LCD.c:68:29: error: (192) undefined identifier “SSPCON2”
    I2C_LCD.c:69:1: error: (192) undefined identifier “PEN”
    I2C_LCD.c:74:9: error: (192) undefined identifier “SSPSTAT”
    I2C_LCD.c:74:29: error: (192) undefined identifier “SSPCON2”
    I2C_LCD.c:75:1: error: (192) undefined identifier “SSPBUF”
    I2C_LCD.c:80:9: error: (192) undefined identifier “SSPSTAT”
    I2C_LCD.c:80:29: error: (192) undefined identifier “SSPCON2”
    I2C_LCD.c:81:1: error: (192) undefined identifier “RSEN”
    I2C_LCD.c:87:9: error: (192) undefined identifier “SSPSTAT”
    I2C_LCD.c:87:29: error: (192) undefined identifier “SSPCON2”
    I2C_LCD.c:88:1: error: (192) undefined identifier “RCEN”
    I2C_LCD.c:90:9: error: (192) undefined identifier “SSPBUF”
    I2C_LCD.c:93:1: advisory: (1) too many errors (21)

      1. These are registers in the Pic you choose to use? They need to be defined. Usually in Pic Assembly, in the pic part number include file? Is this an XC8 problem?

  5. Can you post the fully working project folder here in ZIP format, instead of us having to piece this together on our own? Thanks

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