You are viewing an older revision! See the latest version

PCF8574 I2C IO Expander

This is an I2C 8 bit IO expander, which you can add up to 8 of to a single I2C bus

Hello World!

main.cpp

#include "mbed.h"
#include "PCF8574.h"

PCF8574 io(p9,p10,0x20);

int main(){
    while(1) {
        io.write(0x0);
        wait(0.2);
        io.write(0xF);
        wait(0.2);
    }
 }

PCF8574 Hello World! circuit

Library

The mbed library for the PCF8574 IO Expander

Reference





14 comments:

07 Aug 2010

I find I need an address of 0x40 for the PCF857. I suspect the code example above was written before your change to 8-bit I2C addressing.

07 Aug 2010

Encouraged by Simon, I've made the appropriate change to the code above.

04 Dec 2010

Does this code also work for the 8574AP variant?

04 Dec 2010

There is only a difference in the adressing between a normal pcf8574 en the a version. /media/uploads/mepper13/pcf8574.jpg

13 Mar 2012

I bought this and all is working fine. I have tried the write command and it works. But read command is not working is this correct ?

#include "mbed.h"
#include "PCF8574.h"

PCF8574 io(p9,p10,0x40);

int main() {
int a;
  while (1){
    
       if ( io.read() == 0x65)
        {
                
       io.write(0x64);
    }}
    
}

Basically if a button is pressed at 5th pin a led should glow at 4th pin.. I have tried connecting the other pin of the button to +3.3v and ground, but nothing seem to work.

13 Mar 2012

Do you have pull-ups or pull-downs connected to the inputs of the PCF8574?

Lerche

13 Mar 2012

user Christian Lerche wrote:

Do you have pull-ups or pull-downs connected to the inputs of the PCF8574?

Lerche

You mean at the 5th pin ? Then no I have not connected any pull up or pull down. Do I have to connect ?

P.S. I have connected 2.5k pull up to SDA and SCL.

13 Mar 2012

What do you mean by 5th pin?

The 5th pin of the chip, or the 5th bi-directional port-pin?

Please draw a simple schematic. That would be much easier to understand.

13 Mar 2012

user Christian Lerche wrote:

What do you mean by 5th pin?

The 5th pin of the chip, or the 5th bi-directional port-pin?

Please draw a simple schematic. That would be much easier to understand.

Yes its the 5th bi-directional port-pin

http://i.imgur.com/spT3Q.png

So if i press the button at P5, the led should glow at P4. I need to know what should be connected at A. Thanks

13 Mar 2012

/media/uploads/Lerche/pcf_1.gif

Here's the schematic I'd use.

Have you put pull-down/pull-up on the other pins as well?

This is probably necessary, as when reaqding the input, those pins must not hang. Put 100k pull-down on all of the inputs, then this should take care of it.

Lerche

13 Mar 2012

You dont really need a pullup on input pins. The PCF8574 has internal weak pullups. However, you do need to write a '1' to every bit that you want to read. The weak pullup will then allow you to read a '1' when the switch is open or read a '0' when the switch is closed. When you have previously written a '0' to a port pin you will always read back a '0' no matter what the switch state is or whether or not you have an external pull up installed. I would also mask and test the specific bit on P5 rather that just compare with a hardcoded value. That way you can later add more switches or LEDs without changing the test.

if ( (io.read() & 0x20) == 0x00) {
// P5 switch pressed
}

Christian is right about the series R for the LED, you need that to limit the max current.

13 Mar 2012

I've posted sample code and a video of a working circuit at http://romillys-robots.blogspot.com/2012/03/pcf8574-i2c-and-mbed.html

The breadboarded circuit is as given by Christian Lerche, with a resistor added in series with the LED but without the 100k resistors on the unused inputs.

13 Mar 2012

Instant demo. Great!

14 Mar 2012

user Romilly Cocking wrote:

I've posted sample code and a video of a working circuit at http://romillys-robots.blogspot.com/2012/03/pcf8574-i2c-and-mbed.html

The breadboarded circuit is as given by Christian Lerche, with a resistor added in series with the LED but without the 100k resistors ion the unused inputs.

Thanks it worked :)