Tutorial: Arduino I/O Port Expander with PCF8574

The PCF8574 is a 8 bits I/O port expander that uses the I2C protocol. That protocol communicates using a 2-wire serial interface, where one wire is a serial clock (SCL) and the other is for serial data (SDA). With this IC, you can use only 2 ports of your Arduino board to control up to 8 digital I/O ports. In the I2C protocol, each IC has a different address, in the case of PCF8574, you have a 3 bits address, which enable you to connect up to 8 devices, having 64 ports in total.

We can take a look on the PCF8574 pinout in the data datasheet:

Where: A0,A1,A2 are address pins

            P0,P1,P2,P3,P4,P5,P6,P7 are digital I/O ports (each one is 1 bit)

            SDA,SCL are the I2C pins to communication with the microcontroller.

To make the IC work, it is necessary to set and address, again we can find this information on the datasheet:

where: L means LOW or 0V(GND)

           H means HIGH or VCC

To define an address set A0, A1 and A2 to H or L as you desire. Remember that each IC should has a different address.

Checking the data interface, we can see how the I2C interface will work on the PCF8574:

If you send a I/O data byte, it will turn on or off the corresponding pins P1 to P7 according to the byte send. For example, if you send the number 32, it will be 0010000 in binary, which will turn on only the P5 output. For reading, the same is true, if you receive the number 32, means that only the port P5 was on at that moment.
To test the IC, let's wire an Arduino board,2 PCF8574, some LEDs and buttons:
The left PCF8574 is a receiver, and it will control the LEDs. The right one will be a emitter and will transmit the button's status. When you press a button, the Arduino will read it from the I2C interface and will command the left IC to turn the corresponding LED on.
The code for the Arduino is this:

#include<Wire.h>

byte x=0;
byte y=0;

void setup()
{
    Wire.begin();
}

void loop()
{
    Wire.requestFrom(33,1); 
    if(Wire.available())     //If the request is available
    {
        x=Wire.receive();       //Receive the data
    }
    if(x<255)                     //If the data is less than 255
    {
        if (a==254) { y = 0; }; //P0
        if (a==253) { y = 2; }; //P1
        if (a==247) { y = 8; }; //P3
        if (a==251) { y = 4; }; //P2
    }
    Wire.beginTransmission(32);     //Begin the transmission to PCF8574
    Wire.send(y);                                //Send the data to PCF8574
    Wire.endTransmission();            //End the Transmission
}

Now you can start tweaking your code to match your needs. Good luck!

References:

http://arduino.cc/en/Reference/Wire

http://www.datasheetcatalog.org/datasheet2/b/0fjjhr6h643gldhx3o1rgk...

Views: 85871

Comment by Vassilis Fotopoulos on November 7, 2012 at 1:57am

I think that y should be 1 in the line

if (a==254) { y = 0; }; //P0

am I missing something? I don't have the chip to test it yet.

Comment by contractorwolf on November 29, 2012 at 7:09am

i dont get your code, where does the "a" come from:

if (a==254) { y = 0; }; //P0

should that be "x"?

Comment by Vassilis Fotopoulos on November 29, 2012 at 11:39am

contractorwolf is right..

Comment by Kunal Kashalkar on January 18, 2013 at 2:08am

i tried above Tutorial: Arduino I/O Port Expander with PCF8574

but i receive data i got 255 in decimal

trying to write data but writen data & receive data is different.

what i can do to troubleshoot this problem

Comment

You need to be a member of GarageLab (arduino, electronics, robotics, hacking) to add comments!

Join GarageLab (arduino, electronics, robotics, hacking)

Latest Activity

Marcelo Rodrigues replied to GarageLab's discussion Serious claims against BeagleBone's manufacturer CircuitCo
"Ed, No, they didn't deliver yet.  :( Good luck for us all."
22 hours ago
Profile IconWesenachin Asfaw and Muhammad Basil joined GarageLab (arduino, electronics, robotics, hacking)
22 hours ago
Marcelo Rodrigues replied to FiniousFingers's discussion New to group
"Hey man! A good point to start is reading basic tutorials on Arduino coding and wiring. Then, you…"
22 hours ago
FiniousFingers posted a discussion

New to group

Couple quick things,I am just starting to learn, or wanting to learn, how to use the Arduino UNO…See More
yesterday
Profile IconDan Urbauer, Bjorn and Jiaming Zhao joined GarageLab (arduino, electronics, robotics, hacking)
Wednesday
FiniousFingers is now a member of GarageLab (arduino, electronics, robotics, hacking)
Monday
Erva updated their profile
Mar 16
Erva and Mrs. Priscilla Bonneval are now friends
Mar 16
Profile IconSamantha Dorr, Lee Robinson and Celso Eiju Ito joined GarageLab (arduino, electronics, robotics, hacking)
Mar 16
Edward Evans updated their profile
Mar 15
Edward Evans replied to GarageLab's discussion Serious claims against BeagleBone's manufacturer CircuitCo
"Hi Marcelo, I am curious if you have had any resolution on this?  We may be in the same boat…"
Mar 15
Celso Eiju Ito left a comment for Mrs. Priscilla Bonneval
"I just accept your invitation. Thank You for contact me. Sincerelly,   'Eiju"
Mar 15
Mohasin Ahamed Chinnapattan commented on Mauro Assis's blog post How to hack a remote control (and a flying shark) I
"You mentioned collector is set to ground but emitter is set to ground in your diagram "
Mar 14
Profile Iconbadrkhan mijbas khalifa, Bethany Cruz, Mrs. Priscilla Bonneval and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
Mar 13
Edward Evans is now a member of GarageLab (arduino, electronics, robotics, hacking)
Mar 9
Profile IconAllan Zilkowsky, Shubham Dewangan, FAUSTO PEREIRA ROMANO and 4 more joined GarageLab (arduino, electronics, robotics, hacking)
Mar 1
Dave Lewing is now a member of GarageLab (arduino, electronics, robotics, hacking)
Feb 27
Profile Iconjames essien and Chow HP joined GarageLab (arduino, electronics, robotics, hacking)
Feb 17
Profile IconPerry Duvall, Irayya Mathad and wiley milam joined GarageLab (arduino, electronics, robotics, hacking)
Feb 16
Profile IconAlan Powell and Chuck Nolan joined GarageLab (arduino, electronics, robotics, hacking)
Feb 9

© 2017   Created by Marcelo Rodrigues.   Powered by

Badges  |  Report an Issue  |  Terms of Service