</>EmbeddedInterview

What does an I2C transaction look like?

EasyCommunication Protocols

Short answer: START (SDA falls while SCL is high), then an address byte (7-bit address plus a R/W bit: 0 = write, 1 = read), then an ACK from the addressed target (it pulls SDA low on the 9th clock), then data bytes, each followed by ACK or NACK, then STOP (SDA rises while SCL is high). To read a register, write the register address, then send a repeated START with the read bit and read the data, with the controller NACKing the last byte.

Write register 0x10 = 0xAB on device 0x48:
S | 0x48<<1|W | A | 0x10 | A | 0xAB | A | P

Read 2 bytes from register 0x00:
S | 0x48<<1|W | A | 0x00 | A | Sr | 0x48<<1|R | A | data0 | A | data1 | N | P
(S = START, Sr = repeated START, A = ACK, N = NACK, P = STOP)

Rules

  • SDA may only change while SCL is low; a change while SCL is high is a START or STOP.
  • Data is sent MSB first.
  • The receiver ACKs each byte: the target during writes, the controller during reads (and NACKs the last byte to tell the target to stop driving SDA).
  • A NACK after the address means no device answered (wrong address, device missing or busy, e.g. an EEPROM during its internal write cycle).

Common bugs

  • Using 8-bit "address" values from a datasheet that already include the R/W bit (e.g. 0x90/0x91 for device 0x48). The 7-bit address is the 8-bit one shifted right by one.
  • Sending STOP between the register address and the read: many devices then forget the register pointer or start a new transaction differently.
  • Not handling NACK and continuing as if the write succeeded.

Tools: i2cdetect -y 1 (scan the bus on Linux), i2cget/i2cset/i2ctransfer, and a logic analyser with an I2C decoder.