In this tutorial, I am going to talk about the PCF8574 8-bit GPIO Port Extender. It is one of the many GPIO extenders available in the market.
This tiny little board becomes a life saver, When you run out of pins on your Arduino. This "GPIO (General Purpose Input Output) pin extender" provides an additional 8 pins (P0 ~ P7) which can be used to 'output a signal' or 'read a signal as an input'. These modules run on the I2C bus, and if daisy-chined you can connect upto 8 of these devices in a project. Each device will give us an additional 8-bits of GPIO enabling 64 GPIOs in total.
Topics Covered -------------- - Overview of the I2C Technology ........................................ 01:57 - Closer look at the PCF8574 GPIO Extender IC and the Breakout Board .... 02:57 - GPIO Addressing ....................................................... 05:25 - Libraries To Use ...................................................... 05:48 - Connecting PCF8574 to Arduino and NodeMCU ............................. 06:32 - Finding address using I2C Scanner ..................................... 07:24 - How to program and use PCF8574 in your project ........................ 07:56 - How to use the Interrupt Pin .......................................... 09:15 - Areas of Application .................................................. 10:15
In my last tutorial I talked about the TCA9548A multiplexer which can be used to add at the max
of 64 I2C or I2C sensors to your Arduino, ESP8266 or ESP32. In this tutorial I'm going to talk about
the PCF8574 8-bit GPIO port extender. It is one of the many GPIO extenders available in the market.
These modules run on I2C bus and if days change you can connect up to eight of these devices
in a project. Each device will give you an additional 8-bit of GPIO enabling 64 GPIOs
in total. These ICs are ridiculously cheap and can be bought easily from eBay or AliExpress.
If you don't want to worry about the wiring and want to keep your project really simple
then you can buy these fully assembled breakout boards. You just need to hook them up to your
I2C bus and you are all ready to go. For this tutorial we need a breadboard, PCF8574 GPIO
extender IC, PCF8574 GPIO extender breakout board, Arduino UNO Nano whatever is handy,
NodeMCU, few LEDs and equal amount of 220 ohm current limiting resistors, two 10k resistors,
jumper cables and USB cable to upload the code. We'll start our discussion by talking about the
I2C technology then we'll have a closer look at the PCF8574 IC and the breakout board then we'll
check out how addressing works for these ICs. Next we'll download and install the libraries
that are required for the tutorial. After that we'll connect the IC to an Arduino and NodeMCU.
Next we'll check out the I2C scanner that we'll use to discover the expander over the I2C network.
After that we'll program and use the expander in our project. Next we'll check out how to use the
interrupt pin and finally we'll conclude the tutorial by discussing the areas of application
of the I2C expander. Interintegrated circuit pronounced I2C or I2C is a short distance
two-wire bus technology well actually four wires because you also need the VCC and ground that is
used for serial communication between multiple processors and sensors. If you want to know more
about the I2C technology please check out my tutorial number 9. For now we'll just cover the
basics of I2C and we'll jump to our main topic. The two wires are SDA or the serial data and SCL
or the serial clock. Both these lines are open drain and are pulled up with resistors. Usually
there is one master and one or multiple slaves on the line. However there can be multiple masters
as well. Both master and slave can transmit data. PCF8574 GPIO extender runs on the I2C bus
extending the IO pins which can be controlled by a single host I2C bus. The address range of PCF8574
is from 0x20 to 0x27. At the max eight of these devices can be connected in a project to the I2C
bus. Let's first check out the pinouts of the PCF8574 IC. Pin number 16 of this IC is VCC which
can operate on 2.5 to 6 volts. Pin number 8 is ground. Pin number 4 to 7 and 9 to 12 are the P0
to P7 8 parallel IO pins. Each of these bi-directional IO pins can be used as an input
or output without the use of a data direction control signal. At power on all these IO pins
are at high state. Pin number 15 is for the I2C serial data input or output and pin number 14
is for the I2C clock input. Pin number 1, 2 and 3 or the A0, A1 and A2 allows us to specify the
address that the IC appears as on the I2C bus by turning them high. Default address is 0x20.
By default all these pins are grounded to low. Pull-up resistors are not needed for these pins.
Pin number 13 is for the interrupt output connected to the VCC using a pull-up resistor.
These ICs have very low standby current consumption of only 10 microampere. The SDA,
SLC and the interrupt pins all need to be pulled up using pull-up resistors. There is a second
variant of this IC available in the market called the PCF8574A. The major difference is the
addressing scheme. The first 4 bits of the 8574 7-bit address are 0 1 0 0 and those for the PCF8574A
are 0 1 1 1. The lower 3 bits are the setting on the device pins A2, A1 and A0. The PCF8574 and
the PCF8574A have a maximum syncing current of 25 milliampere. In applications requiring
additional drive, two port pins may be connected together to sync up to 50 milliampere current.
Now let's have a look at the PCF8574 breakout port. Looking from the left to right we can see
that this module has 4 pins VCC, GND, SDA and SCL. Then we have the two 10 kilo ohm pull-up
resistors for the SDA and SLC buses. Next we have the PCF8574SMD IC followed by the 3 jumpers for
A0, A1 and A2 with high and low positions. Then we have this port which can be used to piggyback
another one of these ports. Then we have the 8 IO pins plus 1 interrupt pin. If you look at the
back of the board the pins are all labeled starting from P0 to P7 and then ending with the interrupt
pin. When you're using multiple of these you can switch the address by adjusting the jumper bars.
By connecting the three address bits A0, A1 and A2 to VIN or HI you can get different
combination of addresses. This is how an address byte of a PCF8574 looks like. First 7 bits combine
to form the slave address. The last bit of the slave address defines the operation read or write
to be performed. When it's high a read is selected while a low selects a write operation.
The included wire library allows communication over the I2C bus of Arduino. However if you want
you can also download and use the PCF8574 library from github. The link is in the description below.
Click the downloads button on the top right corner of the page. Once downloaded rename the uncompressed
folder to PCF8574. Make sure that the folder contains PCF8574.cpp and PCF8574.h file. Place
the folder in your Arduino's libraries folder. You may need to create the libraries folder if this is
your very first library. Restart the IDE and then you should be able to use the library in your code.
This library also comes with very good examples which will help you in getting your hands on over
the IO expander. Let's start by connecting the IC to an Arduino. Connect VCC to 3.3 volts.
Ground to ground. We also need to connect the three address selection pins A0, A1 and A2 to ground
to use the default hex 20 address. Then connect the SCL pin to A5 and SDA to A4. Finally we need
to pull up both the SCL and SDA buses with 10k resistors respectively. For node MCU connect VCC
to 3.3 volts. Ground to ground. Pins A0, A1 and A2 to ground. SCL to D1 and SDA to D2 and finally
pull up both the SDA and the SCL buses with 10k resistors respectively. Once the IC is hooked up to
the microcontroller you just need to connect the sensors to the pins 4 to 7 or 9 to 12 or in other
words to the pins P0 to P7 of the IC. As we know by connecting the three address bits A0, A1 and A2
to VIN or HI we can get different combination of addresses. Therefore it sometimes becomes
hard to figure out what address has been allocated to the expander. Moreover if there are two or more
devices on the I2C bus it is always a good idea to check if any one of them is conflicting with
the other one. By running this I2C scanner you can easily find out the hex address of your device.
When loaded to an Arduino the sketch scans the I2C network showing the addresses that respond.
Once you know the address of your I2C expander you can easily use it in your code. First you
need to include the PCF8574.h library to your code. Then you need to pass the address of the
I2C expander to the constructor. For ESP8266 if you want to specify the SDA and SLC pins
use this constructor. Then you need to specify the modes of the I2C pins and finally begin the
transmission before looping through the rest of the code. Now to write the value you just need
to call the digital write function and pass the pin number followed by the mode. Alright so let's
upload the code to an Arduino. As per the code the LEDs attached to P0 and P1 of the expander
should alternate and blink. And that's exactly what's happening here. Bingo. Now to read values
from the expander we are going to use either one of these two functions. Digital read all or digital
read with a pin number. The digital read all function reads all the input pins in one transmission.
If you want to read a single input you can use the digital read with a pin number function.
Now let's load the code to an Arduino. The code is very simple and it just reads the value of the
P1 in a loop. If the value is high it will turn on the LED connected to the pin P0. I'm using this
push button to change the state of the pin P1. When the button is pressed the value of P1 goes
high and the LED lights up. And when the button is released the LED goes off. That's it. Simple.
In today's complex embedded computing and data communication systems interrupts are used
extensively to service peripheral devices. However because of the pin number limitation of packages
most microprocessors have only one or two interrupt lines. Consequently several devices usually are
connected to the same interrupt line. The drawback of this configuration is that the overhead
processing time to identify the device that requested the interrupt service may be too long.
Remote IO expanders like the PCF8574 can inform the microprocessor if there is an incoming data
on the port or if there is a change on the port data without having to communicate via the I2C bus.
The PCF8574 provides an open drain interrupt output that can be fed to a corresponding input
of the microprocessor. To learn more about the interrupts please read the attached data sheet.
To use interrupts in your program you must initialize the pin and the function to call
when PCF8574 raises an interrupt. Once done you can use the interrupt routine in your program.
PCF8574 GPIO extenders are used in processors with limited GPIO pins. You can create an I2C
based keypad using them. They can be used when working with multiple relays. They can be used
for making LED chaser circuits. These ICs are also used to drive the LCD shields and many more.
I'm actually designing a breakout board with eight of these ICs pre-fitted on it to extend my
Arduino's GPIO pins. In another attempt I'm going to couple these ICs with the TCA9548A multiplexer
to see if I can get 512 GPIO pins in total. The TCA9548A breakout enables communication with
multiple I2C devices that have the same address making it simple to interface with them. Obviously
Nano will try controlling that many pins but maybe Mega will be able to handle it. However,
I haven't actually tried it myself. So what are you waiting for? Go ahead and use your imagination
and attach 64 sensors to your Arduino to make an amazing automation project to amaze your friends.
Thanks again for watching this video. I hope it helps you. If you want to support me,
you can subscribe to my channel and watch my other videos. Thanks. See you again in my next video.
Bye now.