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: 84415

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

Profile Iconjames essien and Chow HP joined GarageLab (arduino, electronics, robotics, hacking)
Friday
Profile IconPerry Duvall, Irayya Mathad and wiley milam joined GarageLab (arduino, electronics, robotics, hacking)
Thursday
Profile IconAlan Powell and Chuck Nolan joined GarageLab (arduino, electronics, robotics, hacking)
Feb 9
Profile Iconpavan lakhe and Olive Liao joined GarageLab (arduino, electronics, robotics, hacking)
Feb 6
pin is now a member of GarageLab (arduino, electronics, robotics, hacking)
Feb 3
Valcke Marc updated their profile
Feb 2
Bede Domokos is now a member of GarageLab (arduino, electronics, robotics, hacking)
Feb 1
Profile IconToshiyuki Akashi and Valcke Marc joined GarageLab (arduino, electronics, robotics, hacking)
Jan 31
korte is now a member of GarageLab (arduino, electronics, robotics, hacking)
Jan 30
Profile IconRobert Broedel, Al calaguin and Allan joined GarageLab (arduino, electronics, robotics, hacking)
Jan 26
HUZEFA ESSAJI is now a member of GarageLab (arduino, electronics, robotics, hacking)
Jan 20
karthikeyan is now a member of GarageLab (arduino, electronics, robotics, hacking)
Jan 19
Lou Rabinski is now a member of GarageLab (arduino, electronics, robotics, hacking)
Jan 18
iftekhar Mobin is now a member of GarageLab (arduino, electronics, robotics, hacking)
Jan 16
Profile IconLeicester Hibbert and Michael Young joined GarageLab (arduino, electronics, robotics, hacking)
Jan 9
Profile IconThorsten Schwarz and Dana Custer joined GarageLab (arduino, electronics, robotics, hacking)
Jan 2
Aqeel munir updated their profile
Dec 31, 2016
Todd Enger updated their profile
Dec 29, 2016
FRANK KWABENA is now a member of GarageLab (arduino, electronics, robotics, hacking)
Dec 28, 2016
Ayhan Chaplan commented on GarageLab's blog post How to use a Peltier with Arduino
"Hi! Will this setup work with a 3,8V / 2,5A Peltier element? Or do I have to reduce the resistance…"
Dec 26, 2016

© 2017   Created by Marcelo Rodrigues.   Powered by

Badges  |  Report an Issue  |  Terms of Service