PCF8574 GPIO Extender - With Arduino and NodeMCU _ 15 Steps - Instructables
PCF8574 GPIO Extender - With Arduino and NodeMCU _ 15 Steps - Instructables
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 t
he 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-chained you can connect upto 8 of these devices in a project. Each device will giv
e us an additional 8-bits 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 an
d just want to keep your project really "simple", then you can buy these fully assembled breakout boards. You just need to hook th
em up to the I2C bus and you are all ready to go.
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 1/14
7/13/24, 7:08 PM PCF8574 GPIO Extender - With Arduino and NodeMCU : 15 Steps - Instructables
Breadboard
PCF8574 GPIO Extender IC
PCF8574 GPIO Extender Breakout Board
Arduino UNO/Nano whatever is handy
NodeMCU
Few LEDs and equal amount of 220 Ohms current limiting resistors
2 x 10K Resistors
Jumper Cables, and
USB Cable to upload the code
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 2/14
7/13/24, 7:08 PM PCF8574 GPIO Extender - With Arduino and NodeMCU : 15 Steps - Instructables
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 3/14
7/13/24, 7:08 PM PCF8574 GPIO Extender - With Arduino and NodeMCU : 15 Steps - Instructables
Inter-integrated Circuit pronounced I-squared-C (I²C) or I2C is a 'short distance', 'two wire bus technology' (well actually 4 wires b
ecause 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 09'. For now we will just cover the basi
cs of I2C and we will jump to our main topic. The two wires are:
Both of 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 aswell. Both masters and slaves can transmit or receive data.
PCF8574 GPIO Extender run on the I2C bus, extending the 'IO Pins' which can be controlled by a single host I2C bus. The Addre
ss range of PCF8574 is from 0x20 to 0x27 (7 bit address mode). at the max 8 of these devices can be connected in a project to th
e I2C bus.
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 4/14
7/13/24, 7:08 PM PCF8574 GPIO Extender - With Arduino and NodeMCU : 15 Steps - Instructables
****************************
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 5/14
7/13/24, 7:08 PM PCF8574 GPIO Extender - With Arduino and NodeMCU : 15 Steps - Instructables
Step 5:
*******************************
When you are using multiple of these you can switch the address by adjusting these jumperAddress bars.
Step 6: Addressing
By connecting the three address bits A0, A1 and A2 to VIN or HIGH you can get different combination of the addresses.
This is how an address byte of the PCF8574 looks like. First 7-bits combine to form the slave address. The last bit of the slave ad
dress defines the operation (read or write) to be performed. When it is high (1), a read is selected, while a low (0) selects a write o
peration.
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 6/14
7/13/24, 7:08 PM PCF8574 GPIO Extender - With Arduino and NodeMCU : 15 Steps - Instructables
The included 'Wire Library' allows to communication over the I2C/TWI bus of Arduino, however if you want you can also download
and use the 'PCF8574_library' from github: https://github.com/xreef/PCF8574_library
The link is in the description below. Click the DOWNLOADS button in the top right corner of the page, once downloaded rename t
he uncompressed folder to PCF8574.
Make sure that the PCF8574 folder contains PCF8574.cpp and PCF8574.h.
You may need to create the libraries sub-folder if this is your first library. Restart the IDE and then you should be able to use the li
brary in your code. This library also comes with very good examples which will help you in getting your hands on the IO Expande
r.
#include "Arduino.h"
#include "PCF8574.h"
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 7/14
7/13/24, 7:08 PM PCF8574 GPIO Extender - With Arduino and NodeMCU : 15 Steps - Instructables
VCC to 3.3v
GND to GND
We also need to connect the three address selection pins A0, A1 and A2 to GND to use the default 0x20 address Then connect th
e SCL pin to A5 of Arduino and
SDA to A4
Finally we need to pull-up both the SCL and SDA buses with 10K resistors respectively.
VCC to 3.3v
GND to GND
SCL to D1
SDA to D2
And finally pull-up both the SDA and SCL buses using 10K resistors respectively
Once the IC is hooked up to the micro-controller, you just need to connect the sensors to the Pins 4~7/9~12 or in other words to t
he pins P0 to P7 of the IC.
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 8/14
7/13/24, 7:08 PM PCF8574 GPIO Extender - With Arduino and NodeMCU : 15 Steps - Instructables
As we know, by connecting the three address bits A0, A1 and A2 to VIN or HIGH we can get different combination of addresses. T
herefore, 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 other one.
By running this 'I2C Scanner' you can easily find the hex address of your device. When loaded to an Arduino, the sketch scans th
e I2C network, showing the addresses that respond.
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 9/14
7/13/24, 7:08 PM PCF8574 GPIO Extender - With Arduino and NodeMCU : 15 Steps - Instructables
Once you know the address of the IO Expander you can easily use it in your code.
PCF8574(uint8_t address);
for esp8266 if you want specify SDA and SCL pin use this:
pcf8574.pinMode(P0, OUTPUT);
pcf8574.pinMode(P1, INPUT);
and finally "begin" the transmission before looping through the rest of the code.
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 10/14
7/13/24, 7:08 PM PCF8574 GPIO Extender - With Arduino and NodeMCU : 15 Steps - Instructables
Now, to write a value, you just need to call the "digitalWrite" function and pass the pin-number followed by the mode:
PCF8574.digitalWrite(P1, HIGH);
or:
PCF8574.digitalWrite(P1, LOW);
Alright, so lets 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 thats exactly whats happening here, bingo.
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 11/14
7/13/24, 7:08 PM PCF8574 GPIO Extender - With Arduino and NodeMCU : 15 Steps - Instructables
Now, to read values from the expander we are going to use either one of the two functions the "digitalReadAll()" or "digitalRead(Pi
n_Number)".
PCF8574::DigitalInput di = PCF8574.digitalReadAll();
Serial.println(di.p3);
If you want read a single input you can use the "digitalRead(Pin_Number)" function:
Now, lets load this code to an Arduino. The code is very simple and it just reads the value of the pin P1 in a loop. If the value is HI
GH it will turn on the LED connected to the pin P0. I am using this pushbutton to change the state of the pin P1. When the button i
s pressed, value of P1 goes HIGH and the LED lights up, and when the button is released the LED goes off, thats it simple.
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 12/14
7/13/24, 7:08 PM PCF8574 GPIO Extender - With Arduino and NodeMCU : 15 Steps - Instructables
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 on packages, most microprocessors have only one or two interrupt lines.
Consequently, several devices usually are connected to the same interrupt line. The drawback to this configuration is that the ove
rhead processing time to identify the device that requested the interrupt service may be too long (in the order of microseconds).
The remote I/O expander can inform the microprocessor if there is incoming data on the port or if there is a change of port data, w
ithout having to communicate via the I2C bus.
The PCF8574 provides an open-drain interrupt (INT) output that can be fed to a corresponding input of the microprocessor. To lea
rn more about interrupts, please read the attached datasheet.
To use interrupts 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.
// Function interrupt
void keyPressedOnPCF8574();
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 13/14
7/13/24, 7:08 PM PCF8574 GPIO Extender - With Arduino and NodeMCU : 15 Steps - Instructables
I am actually designing a breakout board with 8 of these ICs pre-fitted on it to extend my Arduino's GPIO pins. In another attempt,
I am going to couple these ICs with the TCA9548A I2C MUX to see if I can get 512 GPIO pins in total. The TCA9548A Breakout e
nables communication with multiple I2C devices that have the same address making it simple to interface with them. Obviously, N
ANO will cry controlling that many pins but may be MEGA will be able to handle it. However, I haven't actually tried it yet.
Maximum 8 MUX can be added to an Arduino each providing additional 8 I2C ports. So:
8 x 8 = 64 I2C Ports
64 x 8 = 512 IO Pins
So, what are you waiting for? Go ahead use your imagination and attach 64 sensors to your Arduino to make an amazing automa
tion project to amaze your friends.
If you want to support me you can subscribe to my channel and watch my other videos. Thanks ca again in my next video, bye no
w.
https://www.instructables.com/PCF8574-GPIO-Extender-With-Arduino-and-NodeMCU/ 14/14