Arduino Leonardo and PCF8574

Hi all !

I am trying to use one pcf8574 with my Leonardo to read 6 push-pull buttons. Diagram is below :

Code is :

#include <Wire.h>

uint8_t address;

uint8_t ReadIo()
{  
  WriteIo(B11111111); // PCF8574 require us to set all outputs to 1 before doing a read.
  
  Wire.beginTransmission(address);
  Wire.requestFrom((int)address, 1); // Ask for 1 byte from slave
  uint8_t bits = Wire.read(); // read that one byte
  Wire.endTransmission();
  
  return bits;
}

void WriteIo(uint8_t bits)
{
  Wire.beginTransmission(address);
  Wire.write(bits);
  Wire.endTransmission();
}

void setup() {
  Wire.begin(); // Arduino needs (SDA=A4,SCL=A5)
  //Wire.begin(0,2); // ESP8266 needs (SDA=GPIO0,SCL=GPIO2)
  address = 0x20;
  Serial.begin(9600);
  
}

void loop() {
  
  
Serial.println("hello you");
delay(1000);
uint8_t bits = ReadIo(); // Read all switches
Serial.println(bits);
}

For the moment, I just try to have the bits, but nothing work... If you have any idea...

If all bits are inputs you don't need to write for every read. Just once in setup is enough. If there is a mix of inputs and outputs, then something else is required.

I believe you need to wait or check for a response

if (Wire.requestFrom(address,1) == 1)
{}

First: Always use code tags to post code!

  Wire.beginTransmission(address);
  Wire.requestFrom((int)address, 1); // Ask for 1 byte from slave
  uint8_t bits = Wire.read(); // read that one byte
  Wire.endTransmission();

You must not put Wire.beginTransmission() and Wire.endTransmission() calls around Wire.requestFrom(). The *Transmission calls are used only for writing to the bus.

uint8_t address;

That code won't work as no address is defined.

And post schematics and not Fritzing's breadboard view!

pylon:

uint8_t address;

That code won't work as no address is defined.

Psst. I noticed that too. It's in setup() (set to 0x20).

adwsystems:
Psst. I noticed that too. It's in setup() (set to 0x20).

Is there a problem with the way address is set ?

No. Since the address can't change, most people would declare and assign the value at the same time in the same line.

Replacing

uint8_t address;

with

uint8_t address = 0x20; (and removing the line address = 0x20;)

Psst. I noticed that too. It's in setup() (set to 0x20).

I apologize, haven't seen that.