TactivergeLite

Lite / Intelligent Robotic Systems / Week 30

I²C: decoding a bus conversation

Twenty sensors share the same two wires, and none of them talk over each other. Who decides who speaks, and how does a sensor say “got it”?

About 40 min · 3 goals · Checked by automated tests · Full interactive version

Why should I care?

Temperature sensors, IMUs, real-time clocks and small displays almost all speak I²C, because it needs only two wires for a whole bus of devices. When a sensor “isn't found”, the answer is in the capture: the address byte, and whether anyone pulled SDA low to acknowledge it. Reading a bus capture bit by bit is the core skill of embedded debugging.

Predict

A datasheet says a sensor's I²C address is 0x48. A logic analyser shows the first byte after START is 0x90. What has gone wrong?

  1. The sensor is broken
  2. Nothing: 0x90 is 0x48 shifted left with the write bit (0) added
  3. The bus is running at the wrong speed
Guess first, then reveal

Nothing is wrong. The 7-bit address fills the top 7 bits of the byte and the last bit says read (1) or write (0): 0x48 × 2 + 0 = 0x90. A read would start with 0x91.

Discover

Leave the address at 0x48 and step through the register read one byte at a time. Watch the 9th bit of each byte: who drives it, and why is the very last one different?

What the interactive shows (text version)

The I²C diagram lists a register read from address 0x48, register 0x00, with sample data 0x1A and 0xC3: byte 1 address+W 0x90 ACK by target; byte 2 register 0x00 ACK by target; byte 3 address+R 0x91 ACK by target; byte 4 data 0x1A ACK by controller; byte 5 data 0xC3 NACK by controller, then STOP. Each step shows the 8 SDA bits most significant first and the 9th acknowledge bit. The readout totals 45 clocks: 450 µs at 100 kHz and 112.5 µs at 400 kHz.

You can also open the interactive on the full site when you have a faster connection.

Watch (optional)

Watch for: Watch for the START and STOP conditions (SDA changing while SCL is high) and the acknowledge bit after every byte.

An instrument maker's clear explanation of I2C addressing, acknowledges and timing.

“Understanding I2C” by Rohde & Schwarz (opens YouTube; streaming video uses a lot of data)

The video is optional. Everything you need is in this lesson's text.

Understand

Two wires, pulled up

I²C has SCL (clock, driven by the controller) and SDA (data). Devices never drive a line high: they can only pull it low or let go (open drain). Pull-up resistors bring released lines back high. So two devices can never fight, one pushing high and one low; at worst both pull low. It also means a 0 is a definite act, which is why the acknowledge is a 0.

Start, address, acknowledge

A transaction begins with START: SDA falls while SCL is high (data normally changes only while SCL is low). Then the controller sends the address byte: 7 address bits, MSB first, plus R/W (0 = write, 1 = read). On the 9th clock it releases SDA; the device with that address pulls it low: ACK. If nobody does, SDA stays high: NACK, meaning no such device (or it is busy).

Reading a register

Most sensors keep values in numbered registers. A register read is: START, address+W, register index (target ACKs both), repeated START, address+R, then the target sends data bytes. Now the controller acknowledges: ACK means “send another”, and a NACK on the last byte means “that's enough”, followed by STOP (SDA rises while SCL is high).

Counting the time

Every byte costs 9 clocks: 8 data bits and the acknowledge. Reading 2 data bytes from a register is 5 bytes on the bus, so 45 clocks: 450 µs at standard mode (100 kHz) or 112.5 µs at fast mode (400 kHz), before counting the START, repeated START and STOP. The addressing overhead is why I²C suits small, occasional reads rather than streaming video.

Explain it another way

Intuitive

It is a classroom with one teacher. The teacher calls a name (the address); only that pupil answers “here” (ACK). The teacher then asks a question (the register) and listens to the answer, nodding after each word until saying “thank you, that's all” (NACK and STOP).

Technical

I²C is synchronous, half duplex and multi-drop, with addressing in band. Clock stretching lets a slow target hold SCL low; multi-controller arbitration works because a controller that tries to send 1 but sees 0 knows it has lost. Pull-up value trades rise time against current: bus capacitance and the resistor form an RC that limits speed.

Mathematical

Address byte = (address << 1) | R/W, so 0x68 write → 0xD0 = 208 and read → 0xD1. Transaction time ≈ 9·N/f for N bytes at clock frequency f: N = 5, f = 400 kHz → 45 / 400 000 s = 112.5 µs.

Applied

An IMU at 0x68 read 1000 times a second for 14 bytes needs (3 + 14) × 9 = 153 clocks per read: 382.5 µs at 400 kHz, so about 38% of the bus. At 100 kHz (1.53 ms per read) it would not fit at all.

Derivation: How long a register read takes
  1. List the bytes: address+W, register index, address+R (after the repeated START), then n data bytes: 3 + n bytes.

  2. Each byte is 8 data clocks plus 1 acknowledge clock: 9 clocks per byte, so 9(3 + n) clocks.

  3. One clock lasts 1/f. At 100 kHz that is 10 µs; at 400 kHz, 2.5 µs.

  4. Time ≈ 9(3 + n)/f. For n = 2: 45 clocks → 450 µs at 100 kHz, 112.5 µs at 400 kHz (START/STOP conditions add a little more).

Worked example 1

A capture shows the first byte after START as SDA bits 1 1 0 1 0 0 0 1 (MSB first), then SDA low on the 9th clock. Decode it.

11010001 = 0xD1. The top 7 bits 1101000 = 0x68 are the address; the last bit 1 means read. SDA low on clock 9 is an ACK: a device at 0x68 is present and ready to send.

Worked example 2

After sending address 0x3C+W, SDA stays high on the 9th clock. What does it mean and what would you check?

NACK: no device acknowledged 0x3C. Check the address in the datasheet (some quote the 8-bit form 0x78), the wiring and the pull-up resistors, and that the device is powered.

Common mix-ups

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

Practice 2. During the acknowledge clock, what does the receiving device do to say ACK?

Practice 4. In a multi-byte read, the controller answers the final data byte with a NACK. Why?

µs

µs

Apply it

Your robot reads a temperature sensor (2 data bytes) and an IMU (14 data bytes) on the same 400 kHz bus, each 200 times a second. Using 9 clocks per byte and the register-read pattern, what fraction of the bus time is used? Would 100 kHz still work?

Reflect

Why does it matter that an I²C device can only pull a line low? Describe what could happen on a shared bus if devices could also drive it high.

Write your answer on paper or in your own notes. Lite never stores what you write.

← UART: one wire, ten bits, one byteSPI: swapping bytes at full speed →