Imagine you build a project and then you just run out the pins. You need more pins for your project to drive something. We will learn to add more pins to our Arduino board using the PCF8574 module. I will demonstrate it with Arduino UNO and an EPS8266 board WEMOS. We also test the output by blinking LEDs and a relay then test the input of this expander module by reading input from a button and an infrared sensor. And we also try to use more than one module to a single Arduino board so we got 16 additional pins.
This is the PCF8574T IC module. This module can give you 8 more pins for each module. This is actually the same IC that we used for the I2C LCD backpack. But this module is used as an IO expander. There are two types that are popular on the market. The blue one and the red one. We can also change the I2C address. For the red one, you can change the address by switching the little slide switch.
And For this blue module, you can change the address by moving the jumper, And for other pins, I think it same as the red one.
PCF8574 Wiring Diagram With Arduino or ESP8266 based MCU Board
PCF8574 With Arduino or ESP8266 based MCU Board Code
After wire the module with Arduino, we need to find the address of the module first. To find it you need to upload the sketch called I2C scanner. Here’s the I2C_Scanner sketch.
// -------------------------------------- // i2c_scanner // // Version 1 // This program (or code that looks like it) // can be found in many places. // For example on the Arduino.cc forum. // The original author is not know. // Version 2, Juni 2012, Using Arduino 1.0.1 // Adapted to be as simple as possible by Arduino.cc user Krodal // Version 3, Feb 26 2013 // V3 by louarnold // Version 4, March 3, 2013, Using Arduino 1.0.3 // by Arduino.cc user Krodal. // Changes by louarnold removed. // Scanning addresses changed from 0...127 to 1...119, // according to the i2c scanner by Nick Gammon // http://www.gammon.com.au/forum/?id=10896 // Version 5, March 28, 2013 // As version 4, but address scans now to 127. // A sensor seems to use address 120. // Version 6, November 27, 2015. // Added waiting for the Leonardo serial communication. // // // This sketch tests the standard 7-bit addresses // Devices with higher bit address might not be seen properly. // #include <Wire.h> void setup() { Wire.begin(); Serial.begin(9600); while (!Serial); // Leonardo: wait for serial monitor Serial.println("\nI2C Scanner"); } void loop() { byte error, address; int nDevices; Serial.println("Scanning..."); nDevices = 0; for(address = 1; address < 127; address++ ) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the address. Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address<16) Serial.print("0"); Serial.print(address,HEX); Serial.println(" !"); nDevices++; } else if (error==4) { Serial.print("Unknown error at address 0x"); if (address<16) Serial.print("0"); Serial.println(address,HEX); } } if (nDevices == 0) Serial.println("No I2C devices found\n"); else Serial.println("done\n"); delay(500); // wait 5 seconds for next scan }
You need to open the serial monitor, and now you should see the address on serial monitor. for me it is 0x27.
The PCF8574 Library
In this tutorial, I use library by xreef. you can download the library from here.
Download the libray, then extract on your arduino library folder. If your arduino IDE is currently open. Restart it first.
Test PCF8574 Output
I will make these 3 LEDs blink sequentially. So I will turn on and off every LED by using the digitalWrite command. To use the digitalWrite we just need to write the object name followed by digitalWrite. So the only differences with the original digitalWrite on Arduino is the object name and pins. Then upload the code. So here’s the wiring diagram with the LEDs attached.
#include "Arduino.h" #include "PCF8574.h" // Set i2c address PCF8574 pcf8574(0x27); //this is according to your address found on i2c_scanner void setup() { Serial.begin(115200); delay(1000); // Set pinMode to OUTPUT pcf8574.pinMode(P0, OUTPUT); pcf8574.pinMode(P3, OUTPUT); pcf8574.pinMode(P5, OUTPUT); Serial.print("Init pcf8574..."); if (pcf8574.begin()) { Serial.println("OK"); } else { Serial.println("KO"); } } void loop() { pcf8574.digitalWrite(P0, HIGH); delay(500); pcf8574.digitalWrite(P0, LOW); delay(500); pcf8574.digitalWrite(P3, HIGH); delay(500); pcf8574.digitalWrite(P3, LOW); delay(500); pcf8574.digitalWrite(P5, HIGH); delay(500); pcf8574.digitalWrite(P5, LOW); delay(500); }
Don’t forget to edit the I2C address according to your address found from i2c_scanner
Use More Than One PCF8574 module to a Single Board
Now you should see the LED is blinking sequentially. This means our test for this module is successful. Now Let’s try connect two modules to a single Arduino board. But for this example let’s just use the Wemos, a small board that only has some pins so maybe we will often use additional pins on a small board like this.
So I connect the blue and red module to a wemos here’s the wiring diagram. So I use a breadboard to help connect multiple wiring on I2C pins.
Let’s upload the i2c scanner again and open the serial monitor. Now you should see two addresses that read.
For me, they are 0x27 and 0x20. The 0x27 is definitely our first module, the red one. So the 0x20 should be the blue module.
I also add a relay to the second module to test the output.
/* Exmple Using more than one PCF8574 module to an Wemos board */ #include "Arduino.h" #include "PCF8574.h" // Set i2c address PCF8574 first(0x27); PCF8574 second(0x20); void setup() { Serial.begin(115200); delay(1000); // Set pinMode to OUTPUT first.pinMode(P0, OUTPUT); first.pinMode(P3, OUTPUT); first.pinMode(P5, OUTPUT); second.pinMode(P7, OUTPUT); Serial.print("Init pcf8574..."); if (first.begin()) { Serial.println("OK"); } else { Serial.println("KO"); } if (second.begin()) { Serial.println("OK"); } else { Serial.println("KO"); } } void loop() { first.digitalWrite(P0, HIGH); delay(500); first.digitalWrite(P0, LOW); first.digitalWrite(P3, HIGH); delay(500); first.digitalWrite(P3, LOW); first.digitalWrite(P5, HIGH); delay(500); first.digitalWrite(P5, LOW); second.digitalWrite(P7, HIGH); delay(1000); second.digitalWrite(P7, LOW); delay(1000); }
Now you should see the LEDs blinking then followed by the relay module.
Test The PCF8574 Input (digitalRead)
Now I will connect a button and an Infrared sensor to the WEMOS board, when the button is pressed it will print “Button Pressed” On the serial monitor, and when the infrared sensor senses object in front of it, it will print “Infrared Detected”. Here’s the wiring diagram.
The Code
/* miliohm.com test PCF8574 input */ #include "Arduino.h" #include "PCF8574.h" // Set i2c address PCF8574 first(0x27); void setup() { Serial.begin(115200); delay(1000); // Set pinMode to OUTPUT first.pinMode(P0, INPUT_PULLUP); first.pinMode(P1, INPUT_PULLUP); Serial.print("Init pcf8574..."); if (first.begin()) { Serial.println("OK"); } else { Serial.println("KO"); } } void loop() { if (first.digitalRead(P0) == LOW) { Serial.println("Button Pressed"); } if (first.digitalRead(P1) == LOW) { Serial.println("Infrared Detected"); } }
Here’s the complete tutorial on video.
Hi, Please contact me, I would like to advertise on your website.
troque a ordem, resistor direto em vcc e conectado ao led, e pcf no gnd led acende melhor