# I2C Write Operation on TCA9539

I am using TI's TCA9539 for some read and write I2C operation to control some signals on my test board (school project). Per TI, the write operation comprises of the following,

"To write on the I2C bus, the master sends a START condition on the bus with the address of the slave, as well as the last bit (the R/W bit) set to 0, which signifies a write. After the slave sends the acknowledge bit, the master then sends the register address of the register to which it wishes to write. The slave acknowledges again, letting the master know it is ready. After this, the master starts sending the register data to the slave until the master has sent all the data necessary (which is sometimes only a single byte), and the master terminates the transmission with a STOP condition."

However i am always getting a Nack from slave when i send the register address information to the slave.

Here is my sample code (very basic)

int Address = 0x74;
#define control_register 0x02 // command byte for Output Port 0
#define data_register_1 0xFF

void setup()
{
byte ack;
Wire.begin();
Serial.begin(9600);
while (!Serial);
ack = Wire.endTransmission();
if (ack == 0)
{
Wire.beginTransmission(control_register);
Serial.print("Control Register is : ");
byte bck = Wire.endTransmission();
if (bck == 0) // since i dont get a nack, this portion of code doesnt execute
{
Wire.write(control_register);
Wire.write(data_register_1);

}

}
}

• Are you using the right address? 7 bit or 8 bit? AFAIK the arduino library needs the 7 bit address, not including the read/write bit. – Passerby Jul 23 '17 at 4:09
• A 7 bit 0x74 requires A0 and A1 to be tied to ground. Did you do that? – Passerby Jul 23 '17 at 4:13
• What value does wire end transmission return? A 1 means transmission error. 2 is address error. 3 is data/register error. 4 is other. – Passerby Jul 23 '17 at 4:21
• You are confusing the slave address and the register address. At least one of your beginTransmission calls is wrong. – CL. Jul 23 '17 at 7:11
• Hello, does this mean i only need to start one transmission and then end it after i have send the complete write cycle based on the data sheet? – JYasir Jul 24 '17 at 1:08

Oh. Simple enough. You begin trasmission to 0x74. Then you end it.

Then you begin transmission to 0x02. That means you really sent a transmission to device address 0x02. Instead of device 0x74 register 0x02.

For that you would do

Begin transmission 0x74.
Write 0x02. Register
Write value.
End transmission.
If back = 0, you successfully
wrote value to register 0x02 on device 0x74


By ending the transmission you send a i2c stop condition, ruining the write or read from the device you began transmission to.

• Hello Passerby, yep the address is 7-bit right justified and i can run a successful scan of i2c device and see 0x74. I was getting 2 which means the slave address was nacking when i was trying to write to command register. – JYasir Jul 23 '17 at 20:33
• @jyasir yes. I realized that after CL posted his comment. You need to use wire.write to write the control register. – Passerby Jul 23 '17 at 20:38
• @jyasir don't forget to accept the answer if it helped – Passerby Jul 24 '17 at 18:01