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

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 IconJiangbin ZHENG and Riszter István joined GarageLab (arduino, electronics, robotics, hacking)
yesterday
Profile Iconkaan and guner guvenc joined GarageLab (arduino, electronics, robotics, hacking)
Monday
Devang Rameshbhai Zalavadiya is now a member of GarageLab (arduino, electronics, robotics, hacking)
May 18
Profile Iconraj kumar, Raffaello Palandri and Jigga Boo joined GarageLab (arduino, electronics, robotics, hacking)
May 7
Profile IconGabriele D'Angelo, dale wolver, Ibrahim Aykut and 2 more joined GarageLab (arduino, electronics, robotics, hacking)
May 5
Profile IconJaschaSaurus Rex and Christian Mock joined GarageLab (arduino, electronics, robotics, hacking)
May 1
Igor Golov is now a member of GarageLab (arduino, electronics, robotics, hacking)
Apr 27
Marco is now a member of GarageLab (arduino, electronics, robotics, hacking)
Apr 24
Muhammad Ali commented on Marcelo Rodrigues's blog post Arduino generated VGA (color) signal - Complete!
"Thanks for the advice. I will persist with the code till I get it done. thanks once again."
Apr 18
Marcelo Rodrigues commented on Marcelo Rodrigues's blog post Arduino generated VGA (color) signal - Complete!
""The output waveforms on oscilloscope for Hsync and Vsync and for that matter RGB pins are not…"
Apr 17
Muhammad Ali commented on Marcelo Rodrigues's blog post Arduino generated VGA (color) signal - Complete!
"Hi  Marcelo I am trying to make this code work with Attiny 44a operating @ 20 MHz. I have…"
Apr 17
Profile IconReal NOiseux, Muhammad Ali, bob v and 1 more joined GarageLab (arduino, electronics, robotics, hacking)
Apr 17
baiqian is now a member of GarageLab (arduino, electronics, robotics, hacking)
Apr 10
Profile IconDavide and Almas Amandossov joined GarageLab (arduino, electronics, robotics, hacking)
Apr 6
Profile Iconvamoalrenga and John Ferrell joined GarageLab (arduino, electronics, robotics, hacking)
Apr 3
Marcelo Rodrigues replied to FiniousFingers's discussion New to group
"Yes, please share your progress here. Thanks and good"
Mar 27
FiniousFingers replied to FiniousFingers's discussion New to group
"Thanks, good to know someone is out there. I will be…"
Mar 27
Edward Evans replied to GarageLab's discussion Serious claims against BeagleBone's manufacturer CircuitCo
"Have you moved forward with any action?  I would be interested in discussing your…"
Mar 27
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."
Mar 23
Profile IconWesenachin Asfaw and Muhammad Basil joined GarageLab (arduino, electronics, robotics, hacking)
Mar 23

© 2017   Created by Marcelo Rodrigues.   Powered by

Badges  |  Report an Issue  |  Terms of Service