The idea: two shared wires that anyone may pull low

I2C (inter-integrated circuit, also written I²C) connects a microcontroller to sensors, EEPROMs, displays and clocks on the same board with only two wires: SDA (serial data) and SCL (serial clock). Every device sits on the same two wires. Each one has its own 7-bit address, so no chip-select wire is needed.

The outputs are open-drain: a device can pull a line to 0 V or let go of it. Nobody ever drives a 1. A pull-up resistor (here Rp = 4.7 kΩ to VDD = 3.3 V) pulls the line up when everyone has let go. So a line is 1 only if every device releases it: the line is the wired-AND of all outputs. Two devices can never short each other out, and this one fact gives I2C its ACK, its clock stretching and its arbitration.

VDD 3.3 V feeds two 4.7 kilo-ohm pull-up resistors, one on SDA and one on SCL. Master A, master B, the temperature sensor at 0x48 and the EEPROM at 0x50 each connect to both lines through an open-drain transistor. A table: if every device releases the line it reads 1; if any device pulls low it reads 0
Every device can only pull a line low; the pull-up makes the line 1 when nobody does, so the line is the AND of all outputs.

The page models the standard mode of the NXP I2C-bus specification (UM10204): 100 kHz, so one clock is 10 µs, with SCL low for 5 µs (the spec asks tLOW ≥ 4.7 µs) and high for 5 µs (tHIGH ≥ 4.0 µs). The canvas draws each transaction on a timeline as it happens. The colour of a low line tells who pulls it: blue for master A, purple for master B, green for a slave. Grey means released.

START, STOP and data bits

The master drives the clock. A slave only answers. The one rule for data: SDA may change only while SCL is low. While SCL is high the receiver samples SDA, so SDA must stay still. The two exceptions mark where a transaction begins and ends:

SymbolWhat happensStandard-mode limit
START (S)SDA falls while SCL is hightHD;STA ≥ 4.0 µs before SCL falls
data bitSDA set while SCL is low, read while SCL is hightLOW ≥ 4.7 µs, tHIGH ≥ 4.0 µs
repeated START (Sr)SDA released, SCL rises, then SDA falls with SCL hightSU;STA ≥ 4.7 µs
STOP (P)SDA rises while SCL is hightSU;STO ≥ 4.0 µs
Timing diagram of SCL and SDA: at START SDA falls while SCL is high; for a data bit SDA changes while SCL is low and is sampled while SCL is high, one clock is 10 microseconds, 5 low and 5 high; at STOP SDA rises while SCL is high
Data changes only while SCL is low; START and STOP are the only SDA edges while SCL is high.

Address, R/W and ACK

After START the master sends one byte, most significant bit first: the 7-bit address and the R/W bit (0 = write, 1 = read). Address 0x48 = 1001000 with W gives the byte 0x90; with R it gives 0x91. Every slave reads the byte and compares it with its own address.

Every byte is followed by a 9th clock. In it the sender releases SDA and the receiver pulls it low: that 0 is the acknowledge (ACK). If SDA stays high it is a NACK. In Demo: write a register the sensor ACKs its address and the EEPROM, which has 0x50, stays silent until the STOP. In Demo: nobody at 0x3C no device pulls SDA low in the 9th clock. The pull-up keeps it at 1, so the master reads a NACK and sends STOP after 9 clocks (105 µs from S to P). The NACK costs no extra wire. It is simply what an empty bus looks like.

Writing and reading a register

Most I2C chips hold a few byte-wide registers and a register pointer. The first byte after a write address sets the pointer. Each data byte after it is written at the pointer, and the pointer moves on by one. Demo: write a register sends S · 0x48 W · ACK · 0x02 · ACK · 0x61 · ACK · P: 27 clocks, 285 µs, and the sensor's register 0x02 now holds 0x61.

To read, the master must first write the register number and then turn the bus around. It does this with a repeated START instead of a STOP, so it never lets go of the bus and no other master can slip in between:

StepOn the busWho drives SDA
1S, 0x48 W (0x90), ACKmaster, then the sensor for the ACK
2register number 0x00, ACKmaster, then the sensor
3Sr, 0x48 R (0x91), ACKmaster, then the sensor
4data 0x19, ACKthe sensor, then the master: "send more"
5data 0x00, NACK, Pthe sensor, then nobody: "that was the last"

The NACK after the last byte is required. While a slave is sending, it holds SDA. The master must tell it to stop, so that SDA is free for the STOP condition. In Demo: read a register this takes 45 clocks and 480 µs. The sensor's 0x19 0x00 is a 12-bit value: 0x1900 >> 4 = 400, and 400 × 0.0625 °C = 25.0 °C. Demo: read the EEPROM runs the same sequence on address 0x50 and gets 0x48 0x69, "Hi". The sensor ignores both address bytes.

So I2C moves 8 data bits in every 9 clocks. At 100 kHz that is at most about 11 kB/s, less once you count the address and register bytes. Fast mode (400 kHz) and Fast-mode Plus (1 MHz) use the same protocol with shorter times. For a fast, full-duplex link with one chip select per device, see SPI on the Embedded I/O page.

Clock stretching

A slave may need time, for example to finish a measurement. Because SCL is open-drain too, the slave can simply hold SCL low after the master has let go of it. The master sees that SCL did not rise and waits. This is clock stretching, and the master needs no extra logic for it beyond "wait until SCL is really high". In Demo: a slow sensor stretches the clock the sensor holds SCL low for 30 µs before its first data bit (green on the SCL trace), and the same read takes 510 µs instead of 480 µs. Nothing is lost; the transfer is only slower.

Two masters: arbitration

I2C allows several masters. Two of them may see a free bus and send START at the same moment. Both then put their address bits on SDA, and each one checks every bit it sends against what it reads back. A master that sends a 1 (releases SDA) but reads a 0 knows someone else is pulling low. It has lost arbitration: it stops driving SDA and waits for the STOP.

Master A sends address 0x48 (1001000) and master B sends 0x50 (1010000), bit by bit. Bits A6 and A5 are equal, 1 and 0. At A4 master A sends 0 and master B sends 1; the wire is 0 AND 1 = 0, so B reads 0, loses and releases SDA, while A goes on and the wire carries exactly A's byte 0x90
The wire is the AND of both masters: the one that sends 1 where the other sends 0 loses, and the winner's bits are never damaged.

In Demo: two masters A addresses the sensor (0x48 = 1001000) and B the EEPROM (0x50 = 1010000). Bits A6 and A5 are equal. At A4, the third bit (sampled at 35 µs), A sends 0 and B sends 1. The wire is 0 AND 1 = 0, so B loses and lets go. A never notices: every bit it sent is exactly what is on the wire, so its write of 0x61 to the sensor finishes as usual in 285 µs. This is why the arbitration costs no time and loses no data. The lower address wins when two masters address different devices. The masters' clocks are also combined by wired-AND (clock synchronization). On the page both masters clock in step, so only the data line decides.

Choosing the pull-up

The pull-up and the capacitance of the wires (Cb) set how fast a released line rises. Standard mode allows a rise time up to tr = 1000 ns, and the rise time is about 0.8473 × Rp × Cb. So Rp(max) = 1000 ns / (0.8473 × Cb): with Cb = 200 pF that is about 5.9 kΩ, and 4.7 kΩ gives tr ≈ 0.80 µs. The resistor must not be too small either. A device must be able to pull the line below VOL = 0.4 V while sinking at most 3 mA, so Rp(min) = (3.3 V − 0.4 V) / 3 mA ≈ 0.97 kΩ. A smaller Rp makes faster edges but more current every time a line is pulled low. The gates inside an open-drain output are the subject of Logic Gates.

See also ADC and PWM, how a microcontroller reads a voltage with its own ADC and drives an LED or a servo with PWM.

See also ADC and PWM, how a microcontroller reads a voltage with its own ADC and drives an LED or a servo with PWM.

See also AMBA Buses, the on-chip buses (AXI, AHB, APB) that connect the CPU to memory and to peripherals like an I2C controller.

See also the CAN bus, where the same wired-AND decides arbitration on a differential pair and the lowest message ID wins without damage.

See also the watchdog timer, which resets a microcontroller whose code is stuck waiting on an I2C bus that never answers.

See also Modbus RTU and Modbus TCP, frame by frame, where a PLC addresses slaves on an RS-485 pair and a CRC-16 guards every frame.

See also USB enumeration, another bus where every packet is answered with ACK or NAK and a new device starts at a default address.

What the page leaves out

  • Rise and fall times are drawn as vertical edges; analog levels (VIL, VIH), noise and the spike filter are not shown. Reading a sensor's analog value is the job of an ADC, shown on another page.
  • Hold and setup times other than those in the tables (tHD;DAT, tSU;DAT, tBUF between STOP and the next START), and the faster modes (400 kHz, 1 MHz, 3.4 MHz High-speed mode, Ultra Fast-mode).
  • 10-bit addresses, the general call address 0x00, reserved addresses, the Device ID, and SMBus and I3C extensions such as timeouts and packet error checking.
  • Clock synchronization between two masters with different clock speeds; a master that loses arbitration and is itself being addressed; bus recovery when a slave holds SDA low after a reset.
  • Real chip details: the sensor is TMP102-like but simplified to three byte registers; an EEPROM's internal write cycle (it NACKs for a few ms after a write) and page writes.

On the mind map

Open the Computer Architecture mind mapComputerArchitectureAbstraction and numbers: layers and bit patternsAbstraction and numbersCombinational logic: output depends only on inputsCombinational logicSequential logic: state held by a clockSequential logicMIPS software: how C becomes machine wordsMIPS softwareThe processor: datapath plus controlThe processorPipelines and parallelism: more instructions at oncePipelines and parallelismMemory and I/O: small fast near, big slow farMemory and I/OMemory arraysMemory arraysdecoder picks wordline, bitlines readCPU cacheCPU cachetag, index, offset; hit or missCache address breakdownCache address breakdownsets = lines/ways; tag = rest of addressCache Write PoliciesCache Write PoliciesWrite-back sends dirty lines at evictionLoop tiling and interchangeLoop tiling and interchangeWalk rows; tile so reused data fitsMemory hierarchyMemory hierarchyAMAT = hit time + miss rate × penaltyDRAM timingDRAM timingopen row is fast, row change is slowMESI and false sharingMESI and false sharinga write invalidates other copiesVirtual memoryVirtual memoryTLB caches the page tableEmbedded I/OEmbedded I/Odevices are addresses; poll or interruptMCU startup to mainMCU startup to mainCopy .data, zero .bss, then mainButton debounce FSMButton debounce FSMTwo equal 10 ms samples = one pressI2C BusWired-AND bus: ACK, stretch, arbitrateUSB enumerationUSB enumerationAddress 0, descriptors, SET_CONFIGURATIONCAN bus arbitrationCAN bus arbitrationDominant 0 wins: lowest ID sends unharmedModbus RTU and TCPModbus RTU and TCPone master asks; bad CRC means silenceTDMA time-triggered busTDMA time-triggered busFixed slots; FTA drops extremes, averagesAMBA AXI, AHB, APBAMBA AXI, AHB, APBVALID and READY both 1 on the edgePCIe TLPsPCIe TLPsRequests by address, completions by IDBus vs Crossbar vs NoCBus vs Crossbar vs NoCBus: one at a time; NoC: links in parallelADC and PWMADC and PWMSAR = binary search; duty = OC1RS/(PR2+1)PID ControlPID ControlI removes error; clamp stops windupOpen the Computer Architecture mind mapComputer Architecturesmall fast near, big slow farMemory and I/OMemory arraysMemory arraysdecoder picks wordline, bitlines readCPU cacheCPU cachetag, index, offset; hit or missCache address breakdownCache address breakdownsets = lines/ways; tag = rest of addressCache Write PoliciesCache Write PoliciesWrite-back sends dirty lines at evictionLoop tiling and interchangeLoop tiling and interchangeWalk rows; tile so reused data fitsMemory hierarchyMemory hierarchyAMAT = hit time + miss rate × penaltyDRAM timingDRAM timingopen row is fast, row change is slowMESI and false sharingMESI and false sharinga write invalidates other copiesVirtual memoryVirtual memoryTLB caches the page tableEmbedded I/OEmbedded I/Odevices are addresses; poll or interruptMCU startup to mainMCU startup to mainCopy .data, zero .bss, then mainButton debounce FSMButton debounce FSMTwo equal 10 ms samples = one pressI2C BusWired-AND bus: ACK, stretch, arbitrateUSB enumerationUSB enumerationAddress 0, descriptors, SET_CONFIGURATIONCAN bus arbitrationCAN bus arbitrationDominant 0 wins: lowest ID sends unharmedModbus RTU and TCPModbus RTU and TCPone master asks; bad CRC means silenceTDMA time-triggered busTDMA time-triggered busFixed slots; FTA drops extremes, averagesAMBA AXI, AHB, APBAMBA AXI, AHB, APBVALID and READY both 1 on the edgePCIe TLPsPCIe TLPsRequests by address, completions by IDBus vs Crossbar vs NoCBus vs Crossbar vs NoCBus: one at a time; NoC: links in parallelADC and PWMADC and PWMSAR = binary search; duty = OC1RS/(PR2+1)PID ControlPID ControlI removes error; clamp stops windup
This page on the Computer Architecture mind map · 79 pages in 7 branches · open it to see the whole map and test yourself in recall mode.