Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 37 ยท Communication Buses

The I2C Bus

Learn how I2C works with only two wires: START and STOP conditions, 7-bit addresses, ACK and NACK, repeated start, pull-up resistor sizing and a bus scanner in C.

I2C (Inter-Integrated Circuit, pronounced "I squared C") connects many chips with just two wires. A temperature sensor, a real-time clock, an EEPROM, an OLED display and an accelerometer can all sit on the same pair of lines, each answering to its own address. You pay for the savings with lower speed (usually 100 kHz or 400 kHz) and a more complicated protocol than SPI, but for slow peripherals it is hard to beat.

This lesson covers the two signals, the shape of a transaction, how addressing and acknowledge work, how to size the pull-up resistors, and the practical problems you will meet: address clashes, stuck buses and clock stretching.

Two wires, open-drain

  • SDA: serial data, bidirectional.
  • SCL: serial clock, driven by the master (a slave may hold it low to slow the master down).

Both lines are open-drain (or open-collector): a device can only pull a line to ground, never drive it high. External pull-up resistors return the line to high when nobody is pulling it down. This arrangement lets several devices share a wire safely: if two disagree, the low wins and nothing is damaged, which is also how arbitration between multiple masters works.

Two consequences follow immediately. First, the bus needs pull-up resistors (usually 2.2 kฮฉ to 10 kฮฉ), and many breakout boards already include them. Second, the speed is limited by how quickly the resistor can charge the wire's capacitance, which is why I2C is slower than a push-pull bus like SPI.

Anatomy of a transaction

Data on SDA may change only while SCL is low and must be stable while SCL is high. The only exceptions are two special conditions the master creates while SCL is high:

  • START: SDA falls while SCL is high. It begins a transaction.
  • STOP: SDA rises while SCL is high. It ends it.

After START the master sends the address byte: seven address bits followed by a read/write bit (0 = write to the slave, 1 = read from the slave). Every byte is followed by a ninth clock pulse in which the receiver pulls SDA low to acknowledge (ACK) or leaves it high to signal NACK. A NACK after the address means "nobody home" (or the device is busy). A NACK after the master reads the last data byte is how the master says "that's enough".

sequenceDiagram
    participant M as Master
    participant S as Slave 0x48
    M->>S: START
    M->>S: address 0x48 + W (0x90)
    S-->>M: ACK
    M->>S: register pointer 0x00
    S-->>M: ACK
    M->>S: REPEATED START
    M->>S: address 0x48 + R (0x91)
    S-->>M: ACK
    S-->>M: data byte
    M->>S: NACK (last byte)
    M->>S: STOP

Reading a register: write the register pointer, then restart and read.

A repeated START is a START without a STOP in between. It lets the master switch from writing the register pointer to reading the data without letting go of the bus, so another master cannot sneak in between.

The simulated transaction below prints each step, with a fake bus where three devices answer.

C Trace an I2C register read and write

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The address 0x48 becomes the byte 0x90 for a write and 0x91 for a read: shift left by one and add the R/W bit. This is the number-one confusion with I2C. Some datasheets quote the 7-bit address (0x48), others the 8-bit write address (0x90). Libraries such as Arduino's Wire expect the 7-bit form, while some register-level code expects the 8-bit form, so check which one yours wants.

Addresses and scanning

A 7-bit address allows 128 values, but a few are reserved (0x00 to 0x07 and 0x78 to 0x7F: general call, 10-bit addressing and so on), leaving 112 usable addresses. Many chips choose some low bits of their address from pins (A0, A1, A2), so you can put several identical devices on one bus. If two devices share an address, neither works reliably: change an address pin, use a different part, or put one on a second bus (an I2C multiplexer such as a TCA9548A is the usual cure).

A bus scanner is the first program to run on any new I2C setup: try every address, and print those that ACK.

C An I2C bus scanner

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

If a scan finds nothing, suspect wiring (swapped SDA and SCL), missing pull-ups, a wrong supply voltage or the wrong bus number. If it finds a device at an unexpected address, read the datasheet's address table.

Sizing the pull-up resistors

The pull-up must be small enough to charge the bus capacitance within the allowed rise time, and large enough that a device can still pull the line low with its limited sink current (3 mA is the standard-mode figure). Two rules of thumb give the window:

TEXT
R_min = (Vdd - 0.4 V) / 3 mA
R_max = t_rise / (0.8473 * C_bus)

where t_rise is 1000 ns for 100 kHz, 300 ns for 400 kHz, and C_bus is the total wire and pin capacitance (the specification allows up to 400 pF).

C Pull-up resistor window

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

With a long, heavy bus (400 pF) at 400 kHz there may be no valid resistor at all. The solution is a lower speed, a shorter bus, or an active pull-up/bus buffer chip.

Problems you will meet

  • Clock stretching. A slave may hold SCL low while it prepares data. A master that ignores this reads garbage. Always enable stretching support, and add a timeout so a dead slave cannot hang the master forever.
  • Stuck bus. If a transaction is aborted mid-byte (a reset, say), a slave may keep SDA held low. Cure: toggle SCL up to nine times until SDA releases, then send a STOP.
  • Wrong address form. 7-bit versus 8-bit (see above).
  • Voltage mismatch. A 3.3 V and a 5 V device on one bus need a level shifter designed for open-drain lines.
  • Too many devices. Total capacitance is limited to 400 pF; each device adds around 10 pF plus the wire.
  • Blocking forever. Always use timeouts in your I2C driver, or one dead sensor freezes the system. Interrupt or DMA-driven I2C avoids stalling the CPU.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Why do I2C lines need pull-up resistors?

    Open-drain outputs can only sink current to ground, so a resistor to Vdd supplies the high level.
  2. 2. A device's 7-bit address is 0x50. What byte does the master send to write to it?

    Shift the address left by one and append R/W = 0: (0x50 << 1) | 0 = 0xA0. A read would be 0xA1.
  3. 3. What does a NACK after the address byte usually mean?

    An ACK pulls SDA low on the ninth clock. If nobody does, SDA stays high and the master sees a NACK.
  4. 4. What does a repeated START allow?

    A repeated START begins a new phase without a STOP, so no other master can take the bus in between.
  5. 5. Two identical sensors share the same fixed I2C address. What is a good fix?

    Each device on one bus must have a unique address. A multiplexer gives each device its own branch.

Summary

  • I2C uses SDA and SCL, both open-drain with pull-up resistors; many devices share the bus by address.
  • A transaction is START, address plus R/W bit, ACK per byte, data, and STOP; a repeated START changes direction.
  • 7-bit and 8-bit address notation differ: the 8-bit byte is the address shifted left plus the R/W bit.
  • Pull-ups must balance rise time (bus capacitance) against sink current; add timeouts for stretching and stuck buses.

Next: CAN, USB and Other Buses