I2C to pcf8574a

So i am trying to write a I2C(pcf8574a) device. I am running into a issue. What i send to write does not seem to be written. Using the example code does pretty much the same thing i write out to the device and then read from it and well the read does not change its value. this is the example code i am using it should look familiar except for the address and freq.

            //create I2C object
            I2CDevice.Configuration con = new I2CDevice.Configuration(0x38, 100);
            I2CDevice MyI2C = new I2CDevice(con);
            //create transactions (we need 2 in this example)
            I2CDevice.I2CTransaction[] xActions = new I2CDevice.I2CTransaction[2];
            // create write buffer (we need one byte)
            byte[] RegisterNum = new byte[1] { 2 };
            xActions[0] = MyI2C.CreateWriteTransaction(RegisterNum);
            // create read buffer to read the register
            byte[] RegisterValue = new byte[1];
            xActions[1] = MyI2C.CreateReadTransaction(RegisterValue);
            // Now we access the I2C bus and timeout in one second if no responce
            MyI2C.Execute(xActions, 1000);
            Debug.Print("Register value: " + RegisterValue[0].ToString());

I have also tried code i found for this device and the .net MF

// We want to read/write one byte
byte[] data = new byte[1];
 
// Create new I2CDevice for an PCF8574A (8 Bit I/O Expander)
I2CDevice PCF8574A = new I2CDevice(new I2CDevice.Configuration(0x70, 100));
 
// We want to read the current pin levels from the PCF8574A
I2CDevice.I2CTransaction[] ReadGPIO = new I2CDevice.I2CTransaction[] {
    PCF8574A.CreateReadTransaction(data)
};
 
// Execute the ReadGPIO transaction, check if byte was successfully read
int bytesRead = PCF8574A.Execute(ReadGPIO, 100);
if (bytesRead != 1) { return; }
 
// Toggle high/low
data[0] = (byte) ~data[0];
 
// Now write the toggled byte
I2CDevice.I2CTransaction[] WriteGPIO = new I2CDevice.I2CTransaction[] {
    PCF8574A.CreateWriteTransaction(data)
};
 
// Execute the WriteGPIO transaction, check if byte successfully send
int bytesSend = PCF8574A.Execute(WriteGPIO, 100);
if (bytesSend != 1) { return; }
 
// Dispose I2CDevice 
PCF8574A.Dispose();
 
Debug.Print("Successfully toggled PCF8574A IO pins");

It doesn’t make it past the byte read part.
I honestly don’t believe its the code. I have tried the various addressing with the same result. It maybe the wiring. Can some one show me a wiring example of the I2C connected to the domino. I feel i am just missing something stupid. I connected pin 15 to Di2 and pin 14 to Di3 and of course power and ground. I am leaving A0,1,2 to low to keep base address. Any help would be appreciated.

Hi bstag,

could you exchange these line of code with the code below?


if (bytesSend != 1) { return; }

//make sure the data was received
if (bytesSend != 1)
throw new Exception("Could not read from device.");

I have also some problems with I2C , but my error was the adress.

I didn’t noticed that the printed adress on my dev board was 8-bit.

So I have to shift 1-bit right ( In your case device address was 0x70 but you have to use 0x38)

The address is 32. Did not go that low before. So i wrote a for loop and tried up to 1000 :>

Amazing what sleep can do for you.

thanks cypher,
thank you very much for your advice, that the address of the PCF8574A has to be shift 1-bit right from 0x70 to 0x38. I spend many hours to find the mistake in a Gadgeteer Spider application with I2C bus.

@ RoSchmi - Welcome to the community!

i’ve an error there " MyI2C.CreateWriteTransaction(RegisterNum);" i don’t know why