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.
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:
| Symbol | What happens | Standard-mode limit |
|---|---|---|
| START (S) | SDA falls while SCL is high | tHD;STA ≥ 4.0 µs before SCL falls |
| data bit | SDA set while SCL is low, read while SCL is high | tLOW ≥ 4.7 µs, tHIGH ≥ 4.0 µs |
| repeated START (Sr) | SDA released, SCL rises, then SDA falls with SCL high | tSU;STA ≥ 4.7 µs |
| STOP (P) | SDA rises while SCL is high | tSU;STO ≥ 4.0 µs |
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:
| Step | On the bus | Who drives SDA |
|---|---|---|
| 1 | S, 0x48 W (0x90), ACK | master, then the sensor for the ACK |
| 2 | register number 0x00, ACK | master, then the sensor |
| 3 | Sr, 0x48 R (0x91), ACK | master, then the sensor |
| 4 | data 0x19, ACK | the sensor, then the master: "send more" |
| 5 | data 0x00, NACK, P | the 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.
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.