How I2C Works: START, STOP, ACK and Clock Stretching
I2C lets one master talk to many devices over two wires, SCL (clock) and SDA (data), using nothing but open drain outputs and two pull-up resistors. Everything in the protocol follows from one rule: SDA may only change while SCL is low. A change while SCL is high is not data; it is a START or a STOP. This page builds an I2C slave in Verilog, drives it from a bus-functional master, and measures the timing against the Standard-mode limits in the I2C specification, NXP UM10204.
Learning objectives
- Recognize START, STOP, a data bit and an ACK on a waveform
- Build the address byte for a read or a write, and use a repeated START to read a register
- Explain open drain lines, and size a pull-up resistor for a rise time
- Explain clock stretching and see it in a simulation
- Check bus timing against the Standard-mode limits
Prerequisites
- Part 1: what the I2C wires do
- Verilog: state machines, and the two-flop synchronizer
Two open drain wires
Neither line is ever driven high. A device either pulls a line low or lets go, and a pull-up resistor brings it high when nobody pulls. That is open drain. It allows any number of devices on one wire with no short circuit when two disagree: low simply wins. It lets a slave answer on SDA and hold SCL. It also sets the speed limit, because the rising edge is the resistor charging the bus capacitance.
// Open drain with pull-ups: a line is low if anyone pulls it low.
reg m_scl_low = 0, m_sda_low = 0;
wire s_scl_low, s_sda_low;
wire scl = !(m_scl_low || s_scl_low);
wire sda = !(m_sda_low || s_sda_low);
i2c_slave #(.ADDR(7'h50), .STRETCH(`STRETCH)) slave (.clk(clk), .rst(rst), .scl_in(scl), .sda_in(sda),
.scl_low(s_scl_low), .sda_low(s_sda_low));START, bits, ACK and STOP
A transfer starts with START: SDA falls while SCL is high. Then come bytes, most significant bit first. The sender sets each bit while SCL is low, and the receiver reads it while SCL is high. After eight bits the sender releases SDA, and the receiver pulls it low for a ninth clock: that is the ACK. If SDA stays high, it is a NACK. The transfer ends with STOP: SDA rises while SCL is high.
The slave detects START and STOP from the synchronized lines exactly as defined: an SDA edge while SCL is high.
reg [2:0] scl_s, sda_s; // two-flop synchronizers plus the previous value
always @(posedge clk) begin scl_s <= {scl_s[1:0], scl_in}; sda_s <= {sda_s[1:0], sda_in}; end
wire scl = scl_s[1], sda = sda_s[1];
wire scl_rise = scl_s[2:1] == 2'b01, scl_fall = scl_s[2:1] == 2'b10;
wire start = scl && sda_s[2:1] == 2'b10; // SDA falls while SCL is high
wire stop = scl && sda_s[2:1] == 2'b01; // SDA rises while SCL is highAddressing and repeated START
The first byte after START is the 7-bit address followed by a read or write bit: 0 to write, 1 to read. A device at address 0x50 is therefore 0xA0 for a write and 0xA1 for a read. Most devices hold registers, so reading one takes two parts: write the register number, then, without a STOP, send a repeated START and the address again with the read bit. The repeated START keeps the bus, so no other master can take it in between. The master acknowledges every byte it reads except the last, which it NACKs to tell the slave to stop sending.
Clock stretching
A slave that needs more time, for example to write a byte into memory, can hold SCL low after the ACK. The master must release SCL and wait until it actually goes high before it times the next high period. That is clock stretching. The master's bus model does exactly that in one task:
task automatic scl_release_and_wait();
m_scl_low = 0;
wait (scl === 1'b1); // a stretching slave keeps it low: wait for it
endtaskWorked example: a write, a read and a missing device
The test writes 0x5A and 0xC3 to registers 2 and 3, reads them back with a repeated START, and addresses 0x51, where there is no device. A monitor measures the shortest high and low times, the START and STOP setup and hold times, and the bus-free time between a STOP and the next START, and compares each with the Standard-mode minimum.
// 1. Write 0x5A and 0xC3 to registers 2 and 3: address, register pointer, data, data.
start_cond();
write_byte({7'h50, 1'b0}, ack); say("write address 0x50 + W", ack);
write_byte(8'h02, ack); say("write pointer 2", ack);
write_byte(8'h5A, ack); say("write data 0x5A", ack);
write_byte(8'hC3, ack); say("write data 0xC3", ack);
stop_cond();
// 2. Read them back: set the pointer, repeated START, address + R, two bytes (ACK, then NACK).
start_cond();
write_byte({7'h50, 1'b0}, ack); write_byte(8'h02, ack);
repeated_start();
write_byte({7'h50, 1'b1}, ack); say("read address 0x50 + R", ack);
read_byte(d0, 1); read_byte(d1, 0);
$display("read: 0x%02h 0x%02h", d0, d1);
stop_cond();
// 3. A device that is not on the bus.
start_cond();
write_byte({7'h51, 1'b0}, ack); say("write address 0x51 + W", ack);
stop_cond();write address 0x50 + W: ACK
write pointer 2: ACK
write data 0x5A: ACK
write data 0xC3: ACK
read address 0x50 + R: ACK
read: 0x5a 0xc3
write address 0x51 + W: NACK
timing, STRETCH = 0:
t_HIGH 5000 ns (Standard-mode minimum 4000 ns) ok
t_LOW 5000 ns (Standard-mode minimum 4700 ns) ok
t_HD;STA 5000 ns (Standard-mode minimum 4000 ns) ok
t_SU;STA 5000 ns (Standard-mode minimum 4700 ns) ok
t_SU;STO 5000 ns (Standard-mode minimum 4000 ns) ok
t_BUF 10000 ns (Standard-mode minimum 4700 ns) ok
longest SCL low: 5000 nsEvery byte to 0x50 is acknowledged, the read returns the two values written, and 0x51 is NACKed. All six timing values meet the Standard-mode minimums: for example the clock low time is 5000 ns against a minimum of 4700 ns, and the bus-free time is 10000 ns against 4700 ns. Built with STRETCH = 500, the slave holds SCL low for 500 of its 20 ns clocks after each data byte:
timing, STRETCH = 500:
t_HIGH 5000 ns (Standard-mode minimum 4000 ns) ok
t_LOW 5000 ns (Standard-mode minimum 4700 ns) ok
t_HD;STA 5000 ns (Standard-mode minimum 4000 ns) ok
t_SU;STA 5000 ns (Standard-mode minimum 4700 ns) ok
t_SU;STO 5000 ns (Standard-mode minimum 4000 ns) ok
t_BUF 10000 ns (Standard-mode minimum 4700 ns) ok
longest SCL low: 10050 nsThe longest SCL low time grows from 5000 ns to 10050 ns, and nothing breaks: the master waited, and every other time is unchanged.
Standard-mode timing
| Symbol | Meaning | Standard-mode minimum | Measured |
|---|---|---|---|
| t_HIGH | SCL high time | 4.0 us | 5.0 us |
| t_LOW | SCL low time | 4.7 us | 5.0 us |
| t_HD;STA | hold time after a START, before SCL falls | 4.0 us | 5.0 us |
| t_SU;STA | setup time before a repeated START | 4.7 us | 5.0 us |
| t_SU;STO | setup time before a STOP | 4.0 us | 5.0 us |
| t_BUF | bus free time between a STOP and a START | 4.7 us | 10.0 us |
Common mistakes
- Shifting the address without the R/W bit, or forgetting to shift it: 0x50 becomes 0x50 on the wire instead of 0xA0, and nobody answers.
- Driving SDA or SCL high from a GPIO instead of releasing it. Two devices then fight, and clock stretching cannot work.
- Changing SDA while SCL is high in a bit-banged master. The slave sees a START or a STOP in the middle of a byte.
- ACKing the last byte of a read. The slave keeps driving SDA and the master's STOP can fail.
- Pull-ups that are too weak for the bus capacitance. Edges become slow, and the bus fails at 400 kHz although it works at 100 kHz.
Quick check
1. SDA rises while SCL is high. What is it?
A STOP condition. Data never changes while SCL is high.
2. Who drives SDA low during the ACK clock of an address byte?
The slave whose address matched. The master releases SDA after the eighth bit.
3. Why use a repeated START rather than a STOP and a new START to read a register?
The repeated START keeps the bus, so no other master can start a transfer between the pointer write and the read, and it saves the STOP and bus-free time.
Exercises
E1 (easy). A sensor has the 7-bit address 0x68. What are the first bytes the master sends for a write and for a read?
Shift the address left once and put R/W in bit 0: 0xD0 to write, 0xD1 to read.
E2 (easy). How long does it take to read 2 bytes from register 0x10 at 400 kHz?
START, address + W, the pointer, repeated START, address + R, two data bytes and STOP: 1 + 9 + 9 + 1 + 9 + 18 + 1 = 48 bit times, 120 us.
E3 (medium). With STRETCH = 500, the longest SCL low is 10050 ns, not 10000. Where do the extra 50 ns come from?
The slave sees SCL through a two-flop synchronizer clocked at 50 MHz, so it notices SCL fall two to three of its 20 ns clocks after the master pulled it low. Only then does it pull SCL low and start counting 500 clocks. The master's 5000 ns of low time are long over before the hold ends, so the low time is set by the slave: 50 ns of detection delay plus 500 times 20 ns.
longest SCL low: 10050 nsE4 (hard). The bus has 200 pF of capacitance. What is the largest pull-up resistor for Standard-mode and for Fast-mode?
The rise time is measured from 30 to 70 percent of VDD. Charging through Rp, that takes Rp * Cb * ln(0.7 / 0.3) = 0.8473 Rp Cb. UM10204 limits it to 1000 ns in Standard-mode and 300 ns in Fast-mode, so Rp is at most 5.90 kohm and 1.77 kohm. The smallest value is set separately, by the 3 mA a device must be able to sink at a low output.
Interview questions
- Q1. Why is I2C open drain? Answer: so that many devices can share a line without a short circuit, a slave can acknowledge and stretch the clock, and two masters can detect each other in arbitration.
- Q2. How does a master know a device is missing? Answer: nobody pulls SDA low in the ACK clock after the address byte, so the master reads a NACK.
- Q3. The bus works at 100 kHz but fails at 400 kHz. What do you check? Answer: the rise time on a scope against the Fast-mode limit, then the pull-up value for the bus capacitance, and whether every device supports Fast-mode.
Summary
- SDA changes only while SCL is low; START and STOP are the SDA edges while SCL is high.
- The first byte is the 7-bit address and R/W; every byte is followed by ACK or NACK.
- A register read is a pointer write, a repeated START and a read; the master NACKs the last byte.
- A slow slave stretches the clock by holding SCL low; the master must wait for SCL to rise.
- The pull-ups and the bus capacitance set the rise time, and so the speed.
Related
Last verified
Run with Icarus Verilog 12.0 and linted with Verilator 5.036 on 27 November 2026; the timing limits and the rise-time rule are from the Standard-mode and Fast-mode columns of UM10204.