Reading from I2c PCF8574

Hello

I'm trying to find a simple code exemple to read if a key (button) attached to a PCF8574 I2C pin is pressed.

Once decoded, I will need to debounce it, but, I need to read it first.

Any help appreciated.

Martin

P.S. Excuse my English, I'm more fluent un French.

I'm trying to find a simple code exemple to read if a key (button) attached to a PCF8574 I2C pin is pressed.

You can find one there.

1. Make the following setup between UNO and 8574.
pcf8574.png
Figure-1:

2. Upload the following sketch and check that LED1 remains ON as long as you keep K1 closed (untested).

#include<Wire.h>

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  Wire.beginTransmission(0x20);
  Wire.write(0x00);  //LED1 is OFF
  Wire.endTransmission();
}

void loop()
{
  Wire.requestFrom(0x20, 1);
  byte x = Wire.read();
  if (bitRead(x, 1) == LOW)
  {
    Wire.beginTransmission(0x20);
    Wire.write(0x01);  //LED1 is ON
    Wire.endTransmission();
  }
  else
  {
    Wire.beginTransmission(0x20);
    Wire.write(0x00);  //LED1 is OFF
    Wire.endTransmission();
  }
  delay(1000);
}

pcf8574.png

Thank you Pylon and GolamMostafa

I will report progress

Hello

Half way there.

I could communicate with the PCF8574, but not with a proper result. I just started with Arduino, so I'm lost...

To put in context: The key switch status that I want to read are in a multifonction module. This one do include 7 key switch, lcd, buzzser and led. The pins on the PCF8574 are maintained high with a resistor. Pressing on a switch, will put the attached pin à 0v.

Now the interogation???

On the PCF8574 Once selected, all pins goes to a low state. Pressing a switch has no effect since the pin is already put at a "0" digital state.

So I suppose that I need to put all pin to a high state (wire.write) then read back (wire.read) individual pin to decode witch one is in us.

Did not succeed to do this simple task....

Set to 1 all pin
Read back all pin
serial.println (value read from the I2C (0X20)

Martin

The key switch status that I want to read are in a multifonction module.

I'm missing a link to the schematics of that board.

On the PCF8574 Once selected, all pins goes to a low state.

Hmm, that is done by your circuit? How do you get the state? Depending on your circuit a simple read() may be enough to get the state of the pin.