pcf8574 i2c lcd module free sample

Generally to be able to use an LCD display we need at least 6 free pins, but the number of pins can be minimized with the help of external components like PCF8574 (or PCF8574A) I2C I/O expander, that’s allows us to use only 2 pins from our microcontroller. This small post shows how to connect the Arduino with I2C LCD provided with PCF8574 I/O expander.

The main component of the I2C LCD display is the PCF8574 I/O expander, with only two pins SDA and SCL we get a maximum of 8 pins from P0 to P7. PCF8574A also can be used but it has a different address.

PCF8574 I/O expander A0, A1 and A2 pins are the address pins which decide the I2C address of the chip. In this example each pin is connected to +5V through a 10k ohm resistor (the 10k resistor is optional, each pin can be connected directly to +5V).

In this interfacing I used a small library named LiquidCrystal_I2C (LiquidCrystal_I2C.h), this library simplifies the Arduino code, it can be downloaded from the links below. After downloading the library, unzip the folder and add it to Arduino libraries folder (for example: C:\Program Files\Arduino\libraries):

pcf8574 i2c lcd module free sample

The PCF8574 I2C LCD uses just two lines to interface: SDA (serial data) and SCL (serial clock), plus 5VDC and Vss. This LCD, which is available from Parallax with yellow or blue backlight, was extensively demonstrated in the Propeller 2 Live Forum: Spin2 for Beginners Series with JonnyMac. The YouTube video demonstrating the code is linked under Additional Resources.

Three programs are included in this Quick Byte:02_jm_lcd_pcf8574_demo-Archive.zip (demo shown in the video above, providing a thorough use of the methods in the object);

The 02_jm_lcd_pcf8574_demo.spin2 and jm_lcd_pcf8574.spin objects are a complete, documented examples with all of the methods to display text and decimals, position the cursor, control the backlight, etc.

pcf8574 i2c lcd module free sample

In this project, we will see how to Connect I2C LCD with Arduino. We have already seen how to interface a regular 16×2 LCD with Arduino. By using an I2C LCD with Arduino, you can preserve all the digital I/O Pins of Arduino UNO and work with LCD using I2C Communication.

An alphanumeric character LCD like the one shown in the following image is one of the frequently used components in many DIY projects. It is often used with Arduino to display a wide range of information like sensor readings, messages from GSM Module, or any status information.

The simplest way to connected a 16×2 or 20×4 character LCD is to select a required sized LCD module and connect it Arduino UNO in a 4-bit mode. But the main drawback of this setup is that even in 4-bit mode, the LCD needs 6 digital IO pins of Arduino for proper communication.

If your project needs to interface with several sensors and other IO devices then you will probably need as many IO pins from Arduino as possible. If the LCD itself utilizes 6 of the available 13 digital IO pins, then you are left with just 7 pins for interfacing other components.

You can plug-in this module directly to the pins of the regular LCD and using I2C communication with Arduino (or any microcontroller) you can transmit the data.

PCF8574 is an I2C based I/O expander IC that provides 8-bit I/O expansion for microcontrollers with I2C interface. Using just two lines of the I2C Interface i.e. the SDA (Serial Data) and SCL (Serial Clock), you can configure 8 bidirectional I/O Pins.

NOTE: A separate tutorial on PCF8574 GPIO Extender will be presented. So, I will provide more detailed information on the PCF8574 IC in that tutorial.

After connecting the I2C Module to LCD, connect the GND and VCC pins of the PCF8574 Module to GND and 5V pins of Arduino. Finally, the SDA and SCL Pins. Connect them to pins A4 and A5 pins of Arduino UNO respectively.

An important step in I2C Communication is figuring out the address of the slave device. Based on the A0, A1 and A2 pins of the PCF8574 IC, the address of the I2C Slave module is fixed.

After uploading the code, open the serial monitor and set the baud rate to 9600. In my case, the address is 0X3F. So, in the actual programming (to display stuff on the LCD), I have to use this address.

Before going into the code, you need a special library called “LiquidCrystal_I2C”. It is based on the “LiquidCrystal” library we normally use but modified for I2C communication. You can download the library from this link.

Download the zip file, extract the contents and rename the directory to “LiquidCrystal-I2C” and place the folder in the libraries of your Arduino installation (usually, C:\Program Files (x86)\Arduino\libraries).

The working of the project is very simple. It is simply an I2C Protocol with an extension of LCD Display. Since the slave addresses on I2C Bus are important, be very careful in calculating them, as they are directly used in the program. Apart from the I2C Slave Address, all the other function of LCD are similar to a regular LCD Library.

By using an I2C based LCD, the number of Digital IO pins required to communicate with an LCD will become zero. This will be helpful in connecting other sensors and actuators to the microcontroller (Arduino, in this case).

Since the communication used is I2C, you can connect up to 8 similar LCD displays on the same I2C Bus. This is possible by modifying the Address pins of the PCF8574 IC for each LCD.

pcf8574 i2c lcd module free sample

In this tutorial, you’ll learn how to use ESP32 (or ESP8266) with the I2C LCD Display module (PCF8574) in Arduino IDE. We’ll be using the LiquidCrystal_I2C library with I2CWire.h library in Arduino Core to interface LCD 16×2 display via the I2C bus.

The library APIs are very similar to the standard LiquidCrystal library in Arduino that we’ve used in this previous tutorial. Except for the fact that it does use the I2C bus instead of parallel (4-Bit) data mode. Which does save a lot of GPIO pins and very suitable for many projects.

The I2C IO expander IC (PCF8574) is commonly used as a cheap solution to implement an I2C LCD interface that replaces the classic parallel connection to the LCDs (at least 6 pins) with an easy to use I2C bus (only 2 pins).

In this section, we’ll only discuss the important things in order to get started with these modules as quickly as possible and control them with ESP32 or ESP8266 boards.

The default address for the PCF8574 IC as stated in its datasheet is shown in the figure down below. It also shows you that 3 bits of the address are actually controllable by the user. You can solder these solder bridges to change the I2C address of the device.

As you know, the 3 non-fixed value bits in the address allow us to make 8 different combinations. And potentially be able to use up to 8 different I2C LCD devices on the same bus at the same time.

Here, I’ll solder the A0 bridge which forces the LSB in the address to become 0. Therefore, the I2C device address is now 0x26 instead of the default value 0x27.

Now, I’ve got 2 I2C LCD modules with 2 unique addresses (0x27, 0x26). And those are the two I’ll be using in LAB21 to show you how to use multiple I2C LCDs with ESP32 at the same time. All of this information does apply to any number of modules up to 8 on the same bus.

In this section, I’ll show you the library we’ll be using (LiquidCrystal_I2C), how to install it in Arduino IDE, how to make the ESP32 to I2C LCD module connections, and what are different API functions available to use in the library.

The library we’ll be using is called “LiquidCrysal_I2C”. It can be added manually by visiting GitHub and downloading the files to the Arduino directory. Or just by using the library manager within Arduino IDE itself, and this is what we’ll be doing hereafter.

And search for the LiquidCrystal I2C library and hit install. It’ll be automatically installed and added to your default Arduino libraries directory. If, for some reason, it didn’t complete properly. Then, you can try adding it manually after downloading it from GitHub.

The connection between ESP32 & I2C LCD module should be as follows. Note that the Wire library by default uses the I2C0 module & its default pins are GPIO21 & GPIO22, as we’ve stated in the previous I2C tutorial.

The code example down below does the following: We start with defining an object of LiquidCrystal_I2C, set its parameters, and initialize the LCD. Then, we’ll print “Hello World!” at the home position. Next, we’ll set the cursor to point at the 2nd row and print another string of text. And that’s all!

The LCD display’s controller (Hitachi HD44780) supports up to 8 custom characters that you can create and store on the LCD itself. Then you can send the Index of each custom character to be displayed later. Maybe 8 custom characters are not enough for your project, but it’s one little extra feature that you can occasionally use.

In this example, we’ll be defining two I2C LCD objects with similar parameters except for the address. The 1st LCD will be @ 0x26 and the 2nd one will be @ 0x27.

We’ll initialize both of them, and write a different message on each LCD to check that addressing is working as it should be. And that’s all about this LAB.

ESP32 I2C LCD module can be used in so many applications as we’ll see in future tutorials. I’ll keep updating this series of tutorials by adding more applications and techniques that may help you in your projects. Drop me a comment if you’ve got any questions or suggestions, I’ll be glad to help!

pcf8574 i2c lcd module free sample

A regular LCD requires a lot of wires (parallel interface) to be connected with a Microcontroller.The Serial LCD  backpack built on PCF8574 IC uses the I2C bus to convert  the parallel interface to a serial one.This needs only2 wires SDA & SCL , apart from the power connections.

The blue preset is to adjust the contrast of the LCD. The black jumper on the left is to enable the Backlight of LCD. The I2C device has a HEX address by which a microcontroller can communicate with it.This is set by the 3 bits A0,A1 ,A2 .If no jumper is present , it is HIGH &  a jumper means LOW. By default all the 3 jumpers are open . ie., A0,A1 A2 all are 1s.

The I2C bus has 2 bidirectional active wires SDA & SCL .They are joined to positive supply through a pull up resistor of 4k7.When the bus is  idle both lines are pulled high.

lcd.setBacklightPin(HIGH);  makes the P3 pin go High, which turns on the NPN transistor.This provides GND to the LED pin of LCD As the other LED pin is already connected to Vcc through the jumper , the LCD backlight glows.

pcf8574 i2c lcd module free sample

Using LCD modules with your Arduino is popular, however the amount of wiring requires time and patience to wire it up correctly – and also uses a lot of digital output pins. That’s why we love these serial backpack modules – they’re fitted to the back of your LCD module and allows connection to your Arduino (or other development board) with only four wires – power, GND, data and clock.

The backpack can also be used with 20 x 4 LCDs. The key is that your LCD must have the interface pads in a single row of sixteen, so it matches the pins on the backpack – for example:

Now let’s get started. First you need to solder the backpack to your LCD module. While your soldering iron is warming up, check that the backpack pins are straight and fit in the LCD module, for example:

Once you’ve finished trimming the header pins, get four male to female jumper wires and connect the LCD module to your Arduino as shown in the following image and table. Then connect your Arduino to the computer via USB:

The next step is to download and install the Arduino I2C LCD library for use with the backpack. First of all, rename the “LiquidCrystal” library folder in your Arduino libraries folder. We do this just to keep it as a backup.

Now restart the Arduino IDE if it was already running – or open it now. To test the module we have a demonstration sketch prepared, simply copy and upload the following sketch:

As opposed to using the LCD module without the backpack, there’s a few extra lines of code to include in your sketches. To review these, open the example sketch mentioned earlier.

You will need the libraries as shown in lines 3, 4 and 5 – and initialise the module as shown in line 7. Note that the default I2C bus address is 0x27 – and the first parameter in the LiquidCrystal_I2C function.

Finally the three lines used in void setup() are also required to initialise the LCD. If you’re using a 20×4 LCD module, change the parameters in the lcd.begin()function.

From this point you can use all the standard LiquidCrystal functions such as lcd.setCursor() to move the cursor and lcd.write() to display text or variables as normal. The backlight can also be turned on and off with lcd.setBacklight(HIGH) or lcd.setBacklight(LOW).

If you want to use more than one module, or have another device on the I2C bus with address 0x27 then you’ll need to change the address used on the module. There are eight options to choose from, and these are selected by soldering over one or more of the following spots:

There are eight possible combinations, and these are described in Table 4 of the PCF8574 data sheet which can be downloaded from the NXP website. If you’re unsure about the bus address used by the module, simply connect it to your Arduino as described earlier and run the I2C scanner sketch from the Arduino playground.

pcf8574 i2c lcd module free sample

This tutorial shows how to use the I2C LCD (Liquid Crystal Display) with the ESP32 using Arduino IDE. We’ll show you how to wire the display, install the library and try sample code to write text on the LCD: static text, and scroll long messages. You can also use this guide with the ESP8266.

Additionally, it comes with a built-in potentiometer you can use to adjust the contrast between the background and the characters on the LCD. On a “regular” LCD you need to add a potentiometer to the circuit to adjust the contrast.

Before displaying text on the LCD, you need to find the LCD I2C address. With the LCD properly wired to the ESP32, upload the following I2C Scanner sketch.

After uploading the code, open the Serial Monitor at a baud rate of 115200. Press the ESP32 EN button. The I2C address should be displayed in the Serial Monitor.

Displaying static text on the LCD is very simple. All you have to do is select where you want the characters to be displayed on the screen, and then send the message to the display.

In this simple sketch we show you the most useful and important functions from the LiquidCrystal_I2C library. So, let’s take a quick look at how the code works.

The next two lines set the number of columns and rows of your LCD display. If you’re using a display with another size, you should modify those variables.

Scrolling text on the LCD is specially useful when you want to display messages longer than 16 characters. The library comes with built-in functions that allows you to scroll text. However, many people experience problems with those functions because:

In a 16×2 LCD there are 32 blocks where you can display characters. Each block is made out of 5×8 tiny pixels. You can display custom characters by defining the state of each tiny pixel. For that, you can create a byte variable to hold  the state of each pixel.

In summary, in this tutorial we’ve shown you how to use an I2C LCD display with the ESP32/ESP8266 with Arduino IDE: how to display static text, scrolling text and custom characters. This tutorial also works with the Arduino board, you just need to change the pin assignment to use the Arduino I2C pins.

pcf8574 i2c lcd module free sample

Any parallelly interfaced character LCD you get these days will have a Hitachi HD44780 chip or a different one compatible with the HD44780. These usually have 14 pins (16 if have backlight)D0-D7is the bi-directional data busR/W determines if we read from or write to the LCDRSstands for "register select". RS=0 means that the instruction register is selected. RS=1 means that the data register is selected. In other words, according to the status of RS pin, the data on the data bus is treated either as a command or character data.E pin enables or disables the LCD module. When Enable is low the LCD is disabled and the status of RS,R/W and the data bus will be ignored. When Enable pin is high the LCD is enabled and the status of the other control pins and data bus will be processed by the LCD. When writing to the display, data is transferred only on the high to low transition of this signal.

The data sheet warns that under certain conditions, the lcd may fail to initialize properly when power is first applied. This is particulary likely if the Vdd supply does not rise to its correct operating voltage quickly enough. It is recommended that after power is applied a command sequence of 3 bytes of values $30 is sent to the module. This will guarantee that the module is in 8 bit mode and properly initialised.

The HDD44780 lcd control chip was designed to be compatible with 4-bit processor. Once the display is put in 4 bit mode, using the Function Set command, it is a simple matter of sending two nibbles instead of one byte, for each subsequent command or character. When using 4 bit mode only data lines D4 to D7 are used. Note that the Function set command for 4-bit interface, 2 lines, 5*7 Pixels is 0x28. So first use the Function set command $20 (4-bit interface, 1 line, 5*7 Pixels); from now on, all commands and data must be sent in two halves, the upper four bits first.

Important: the I2C standard prescribes pull-up resistors on the SDA and SCL line. This is one of the most common mistakes when first using an I2C interface. Forget the pull-ups and your interface will not work (sigh!). Picaxe manual show resistors of 4k7 ohms.

pcf8574 i2c lcd module free sample

In this one we’ll use it to connect a Keypad to an Arduino and again save some pins, and also have a quick overview on what and how the i2c protocol works.

I2C stands for “Inter-Integrated Circuit“, it allows to connect multiple modules or “slave”, and requires only 2 wires no matter the amount of connected modules, on an Arduino you can have up to 128 slave devices.

Those address are in hex values (ex. ox20), and most module give you the possibility to change the address by either soldering some pads or using dip switches like the I2C Expander module we used.

I2C uses two pins or signals, one is SCL and the other is SDA.  Most Arduinos have only one I2C bus, one exception would be the Arduino Due which has two seperate I2C bus.

The main difference between those two modules, is their pinouts, the LCD Backpack pinout is made to fit on an LCD with additional outputs for the backlight.

Since both the I2C port Expander and the I2C LCD Backpack are pretty much the same, couldn’t I just use the LCD Backpack to connect the Bourns encoder?

But if your project can make due with only 7 I/O pins then you can use the LCD Backpack as an I2C Expander since those other pins are properly connected.

So instead of using 7 Pins on the Arduino, were’s using the I2C protocol and using only 2 Pins to read the Keypad as well as display the results on the LCD screen.

pcf8574 i2c lcd module free sample

The next step is to download and install the Arduino I2C LCD library for use with the backpack. First of all, rename the "LiquidCrystal" library folder in your Arduino libraries folder. We do this just to keep it as a backup.

Now restart the Arduino IDE if it was already running - or open it now. To test the module we have a demonstration sketch prepared, simply copy and upload the following sketch:/* Demonstration sketch for PCF8574T I2C LCD Backpack

After a few moments the LCD will be initialised and start to display our URL and the value for millis, then blink the backlight off and on. If the text isn"t clear, or you just see white blocks - try adjusting the contrast using the potentiometer on the back of the module.

pcf8574 i2c lcd module free sample

I looked at a few libraries (Arduino ones) to get inspiration. In LCD_I2C_PCF8574.c I have added a lot of background and links to where you can get hold of other source, documentation and data on the PIC18F2685, I2C, the LCD and IO expander should you be so inclined. I also added a link to the library I ripped for the character generation. Thanks Mario. This file also contains details on how you may want to customise to your implementation, these are tagged with "TODO adapt" so you can use the MPLABX task list to grab them.

I took all my details/nomenclature etc. from an Hitachi hard copy LCD manual (yes hard copy, real paper an"all!) I obtained in the early 1980"s when we were still printing on flattened trees.

pcf8574 i2c lcd module free sample

This is a C library that you can use to connect a PCF8574 based LCD to any of your available GPIO ports. It uses software emulated (bit-banged) I2C bus.

Note this library uses wPi_soft_i2c as its backend, which is based on wiringPi. You can easily adapt it to work with a different backend by providing suitable alternatives for I2C (i2c_start, i2c_stop, i2c_send_byte and i2c_read_byte) as well as for delay (usleep). I’ve been using it in a PIC16F88 here and with an ESP8266 here.

If you want to use pins 8 and 9, please notice these are hardware enabled I2C pins. So make sure you unloaded i2c kernel modules before using these routines.

Note the initialization changes in two parameters: The I2C address, because PCF8574’s default address is 0x27 while PCF8574A’s is 0x3f. And the number of lines.

In this example you will learn to interact with two separate I2C buses. In one of the buses you will have a HTU21D, a temperature and humidity sensor. Please note that this is just an example, not a proper way to use I2C: it is a bus, so you should have connected both devices to the same I2C lines.

Advanced level: you know how to use LCDs and the different options and commands they have. You want to have plenty of control over all LCD configurations.

It initializes a new I2C bus. Then tries to communicate with PCF8574 driver and reset the LCD to a known state. If it goes all right, it returns a new lcd_t structure with the default configuration.

If you set the wrong pin numbers, I2C bus is busy or not pulled up, your power supply is not ready o LCD driver is defective, or any other error condition, it will return NULL.

There is an integrated character replacement function. If you try to print non-standard 2 byte UTF8 characters like ÁÉÍÓÚÑáéíóúñ they will be replaced by a 1 byte equivalent. You can disable this behaviour unsetting a flag in the LCD structure.

This function may be called at the end of your program. It is optional. If you don’t call it, then the LCD just shows the last message until you disconnect the power supply after your program ends.

Turns LCD ON (default) or OFF. When the LCD is on, the display data can be displayed instantly. When the LCD is off, the display data remains in RAM, and can be displayed instantly by switching it to ON.

Each and every I2C commands sent to PCF8574 must be acknowledged. If PCF8574 fails to ack a command, the current function stops and set the error flag.

Checking the error condition, you can take appropriate measures like showing a message by another mean or resetting the LCD; but the last will not always be possible.

Most of PCF8574 LCD drivers have a jumper to switch on and off the back light. This library provides the function lcd_backlight_dim to control the led through PWM.