Sub topic 5
Anatomy of a Transaction
This chapter assumes you know what SDA and SCL are, why the bus has pull-up resistors, and how 7-bit addresses work. It also assumes you can read binary and hexadecimal. Here we follow one complete transaction, from the first START to the final STOP. We then map each step onto the Wire library calls that produce it.
The rules that frame every transaction
Four rules explain nearly everything on the wire:
- Data may change only while SCL is low. The receiver samples SDA while SCL is high, so SDA must be stable then.
- Bytes go out most significant bit first. Each byte takes eight clock pulses.
- A ninth clock pulse carries the acknowledge bit. The receiver of the byte answers on this pulse.
- START and STOP are the only times SDA changes while SCL is high. They are deliberate rule violations that the whole bus recognizes as markers.
Because the lines are open-drain, "pulling SDA low" means a device turns on its output transistor. "Releasing" SDA means it lets the pull-up resistor raise the line. A device never drives the line high.
The four bus events
| Event | What happens on the lines | Who produces it |
|---|---|---|
| START | SDA falls while SCL is high | Controller |
| ACK | Receiver holds SDA low during the ninth clock | Whoever receives the byte |
| NACK | Nobody pulls SDA low during the ninth clock, so it stays high | Whoever receives the byte, or nobody |
| STOP | SDA rises while SCL is high | Controller |
A NACK is not always a deliberate refusal. If no device exists at an address, nothing pulls SDA low, and the pull-up makes that look like a NACK.
A write transaction step by step
Suppose you want to send one byte, 0x2A, to register 0x10 of a device at address 0x48.
- The controller issues START.
- The controller sends the address byte: the 7-bit address in the upper bits and the read/write bit in bit 0. For address
0x48and a write (bit = 0), the byte is0x48 << 1 | 0, which is0x90. - The addressed target pulls SDA low on the ninth clock (ACK). Targets with other addresses ignore the rest of the transaction.
- The controller sends the register number
0x10. The target ACKs. - The controller sends the data byte
0x2A. The target ACKs. - The controller issues STOP and releases the bus.
The first byte after the address is not special to the I2C protocol. Most register-based chips treat it as a register pointer, but that is a convention of the device, not of the bus. Check the datasheet for each chip.
A read transaction and the repeated START
Reading a register takes two phases, because the controller must first tell the target which register to read. That is a write, followed by a read. If the controller issued a STOP between the phases, another controller could take the bus and the target's internal pointer might change. A repeated START avoids that. The controller issues a new START without a STOP, so it never gives up the bus.
Note the direction reversal in the read phase. After the address byte, the target drives SDA for the data bits, and the controller drives it only for the ninth clock, the ACK bit. The controller ACKs every byte it wants to continue after. It NACKs the last byte it wants, which tells the target to stop driving SDA so the controller can issue STOP. This is the one case where a NACK is normal and healthy.
How Wire calls map to bus events
The Wire library hides the bit-level work, but the calls line up closely with the events above.
| Wire call | Bus effect |
|---|---|
| `Wire.beginTransmission(addr)` | Nothing on the wire yet. It records the address and clears the transmit buffer. |
| `Wire.write(byte)` | Nothing on the wire yet. It appends to the transmit buffer. |
| `Wire.endTransmission()` | START, address with write bit, buffered bytes, then STOP. |
| `Wire.endTransmission(false)` | The same, but without STOP, so the next call can begin with a repeated START. |
| `Wire.requestFrom(addr, n)` | START or repeated START, address with read bit, `n` bytes (ACK all but the last), then STOP. |
| `Wire.available()` and `Wire.read()` | Read from the receive buffer. These do not touch the bus. |
The most common beginner mistake follows from this table. beginTransmission and write send nothing. If you forget endTransmission, the bytes never leave the Arduino.
The return value of endTransmission
endTransmission blocks until the transaction finishes and returns a status code. It is your only view of the ACK and NACK bits.
| Code | Meaning | Typical cause |
|---|---|---|
| 0 | Success. Every byte was ACKed. | Working device |
| 1 | Data too long for the transmit buffer | More than the buffer holds (32 bytes on AVR boards) |
| 2 | NACK on the address byte | Wrong address, missing device, or no pull-ups |
| 3 | NACK on a data byte | Device rejected a register or value |
| 4 | Other error | Often a bus fault, such as arbitration lost or SDA stuck |
| 5 | Timeout | Not reported on every board's core |
Codes 2 and 3 point at different problems. Code 2 means no one answered at that address. Code 3 means someone answered, so wiring and address are probably fine, but the device did not accept your data. That distinction is the first diagnostic step in most debugging sessions.
The scanner you have already seen relies on this behavior. It starts a transaction with zero data bytes and counts an address as present only if endTransmission returns 0.
A complete example with error reporting
This sketch performs the write-pointer, repeated START, read sequence and reports where it fails. It assumes some device answers at 0x48 and has a 16-bit register at 0x00. Change both values to match your part.
#include <Wire.h>
const uint8_t DEVICE = 0x48;
const uint8_t REGISTER_POINTER = 0x00;
const char *describe(uint8_t code) {
switch (code) {
case 0: return "success";
case 1: return "data too long for buffer";
case 2: return "NACK on address";
case 3: return "NACK on data";
case 4: return "other error";
case 5: return "timeout";
default: return "unknown";
}
}
void setup() {
Serial.begin(9600);
Wire.begin();
}
void loop() {
Wire.beginTransmission(DEVICE);
Wire.write(REGISTER_POINTER);
uint8_t err = Wire.endTransmission(false); // repeated START follows
if (err != 0) {
Serial.print("Pointer write failed: ");
Serial.println(describe(err));
delay(1000);
return;
}
uint8_t got = Wire.requestFrom(DEVICE, (uint8_t)2);
if (got != 2) {
Serial.print("Short read, bytes received: ");
Serial.println(got);
delay(1000);
return;
}
uint16_t value = (uint16_t)Wire.read() << 8;
value |= Wire.read();
Serial.print("Register value: 0x");
Serial.println(value, HEX);
delay(1000);
}Edge cases and gotchas
- 7-bit versus 8-bit addresses. Some datasheets list the full address byte, such as
0x90for write and0x91for read. Wire expects the 7-bit address, so you pass0x48. The library shifts it and adds the read/write bit for you. - Missing STOP. After
endTransmission(false), the bus stays claimed. If you never follow it with a read or a normalendTransmission(), other controllers are locked out and some targets keep waiting. - Short reads.
requestFromreturns how many bytes arrived. A short count means the target stopped responding partway. Always compare it with the number you asked for before callingread. - Targets that need a STOP before a read. A few devices do not tolerate a repeated START. If a read works with
endTransmission(true)but not withfalse, the datasheet usually explains why. - Auto-increment. Many chips advance their internal register pointer after each byte. That lets you read several consecutive registers in one transaction by requesting more bytes. Not every chip does this.
Exercise
A sketch calls Wire.beginTransmission(0x40), Wire.write(0x05), and then Wire.endTransmission(false). It then calls Wire.requestFrom(0x40, 3) and gets back 3. List the bus events in order, and say which bus event the controller sends after the last byte it reads.
Show answer
START, address 0x40 with write bit (ACK), byte 0x05 (ACK), repeated START (no STOP in between), address 0x40 with read bit (ACK), three data bytes from the target. The controller ACKs the first two bytes and NACKs the third to say it wants no more, then issues STOP. The final byte is NACKed by design, not as an error.
Exercise
endTransmission returns 2 on a bus that worked yesterday, and returns 3 when you instead address a different, working chip with an unsupported register number. What does each code tell you about where to look first?
Show answer
Code 2 means nobody ACKed the address byte, so check power, wiring, pull-ups, and the address itself. Code 3 means the address was ACKed but a data byte was NACKed, so the device is present and the problem is in what you sent, such as the register number or value.