Write an I2C Master State Machine in Verilog
Build an I2C master in Verilog that writes one byte to a 7-bit address with quarter-bit timing, open-drain lines and ACK checking, tested against a slave model.

I2C looks simple on the datasheet and surprising in hardware. The clock and data lines are open drain, so nothing ever drives them high; a device either pulls a line low or lets go and a resistor does the rest. Data may change only while the clock is low, with exactly two exceptions that define the start and stop of a transfer. A master that gets those rules right is a state machine with a quarter-bit timer, and this post builds one in Verilog: it writes a single data byte to a 7-bit address, checks the acknowledge after each byte and reports an error when no device answers. It is tested in Icarus Verilog 12.0 against a behavioural slave model, and the code uses only constructs that synthesise. You should already be comfortable with synchronous state machines and have read an I2C waveform once.
Why four phases per bit
Each I2C bit has four moments that matter: the data line is set up while the clock is low, the clock rises, the data is sampled by the receiver while the clock is high, and the clock falls. If the master moves SDA and SCL on a single clock edge at the same instant, the receiver sees a race. Dividing every bit into four phases of equal length gives each event its own slot:
| Phase | SCL | SDA | What happens |
|---|---|---|---|
| 0 | low | set to the next bit | data setup while the clock is low |
| 1 | rises to high | held | clock rises |
| 2 | high | held, sampled for ACK | the receiver samples |
| 3 | falls to low | held | clock falls |
A start condition is SDA falling while SCL is high. A stop condition is SDA rising while SCL is high. Both fit the same four-phase scheme by moving SDA in phase 1 or phase 2 while SCL is already high.
A free-running counter produces the phase ticks. Its modulus, DIV, is the number of system clocks per quarter bit, so the SCL period is four times DIV system clocks. At 100 kHz I2C and a 50 MHz system clock, DIV would be 125.
The open-drain outputs
An I2C pin is never driven high. The design keeps a one-bit register per line where 1 means release and 0 means pull low, and turns it into a tri-state:
assign scl = scl_o ? 1'bz : 1'b0;
assign sda = sda_o ? 1'bz : 1'b0;On an FPGA, the synthesis tool maps this to an open-drain or tri-state pad, and the board supplies a pull-up. Reading the line back is done on the same net: sda is an input as well when the design has released it. In simulation, a pullup primitive on each net models the resistor.
The master
The state machine has five states: idle, start, bits, acknowledge and stop. byte_num selects whether the address byte or the data byte is being sent.
module i2c_write_master #(
parameter DIV = 25
) (
input wire clk,
input wire rst,
input wire start,
input wire [6:0] addr,
input wire [7:0] data,
output reg busy,
output reg done,
output reg ack_err,
inout wire scl,
inout wire sda
);
localparam S_IDLE = 3'd0, S_START = 3'd1, S_BITS = 3'd2,
S_ACK = 3'd3, S_STOP = 3'd4;
reg [2:0] state;
reg [1:0] phase;
reg [15:0] div_cnt;
reg [2:0] bit_idx;
reg byte_num;
reg [7:0] shreg;
reg [7:0] data_q;
reg scl_o;
reg sda_o;
assign scl = scl_o ? 1'bz : 1'b0;
assign sda = sda_o ? 1'bz : 1'b0;
wire tick = (div_cnt == DIV - 1);
always @(posedge clk) begin
if (rst || tick)
div_cnt <= 16'd0;
else
div_cnt <= div_cnt + 16'd1;
end
always @(posedge clk) begin
done <= 1'b0;
if (rst) begin
state <= S_IDLE;
phase <= 2'd0;
scl_o <= 1'b1;
sda_o <= 1'b1;
busy <= 1'b0;
ack_err <= 1'b0;
end else begin
case (state)
S_IDLE: begin
scl_o <= 1'b1;
sda_o <= 1'b1;
if (start) begin
shreg <= {addr, 1'b0};
data_q <= data;
byte_num <= 1'b0;
ack_err <= 1'b0;
busy <= 1'b1;
phase <= 2'd0;
state <= S_START;
end
end
S_START: if (tick) begin
case (phase)
2'd0: phase <= 2'd1;
2'd1: begin sda_o <= 1'b0; phase <= 2'd2; end
2'd2: phase <= 2'd3;
2'd3: begin
scl_o <= 1'b0;
bit_idx <= 3'd7;
phase <= 2'd0;
state <= S_BITS;
end
endcase
end
S_BITS: if (tick) begin
case (phase)
2'd0: begin sda_o <= shreg[bit_idx]; phase <= 2'd1; end
2'd1: begin scl_o <= 1'b1; phase <= 2'd2; end
2'd2: phase <= 2'd3;
2'd3: begin
scl_o <= 1'b0;
phase <= 2'd0;
if (bit_idx == 3'd0)
state <= S_ACK;
else
bit_idx <= bit_idx - 3'd1;
end
endcase
end
S_ACK: if (tick) begin
case (phase)
2'd0: begin sda_o <= 1'b1; phase <= 2'd1; end
2'd1: begin scl_o <= 1'b1; phase <= 2'd2; end
2'd2: begin
if (sda) ack_err <= 1'b1;
phase <= 2'd3;
end
2'd3: begin
scl_o <= 1'b0;
phase <= 2'd0;
if (ack_err || byte_num) begin
state <= S_STOP;
end else begin
shreg <= data_q;
byte_num <= 1'b1;
bit_idx <= 3'd7;
state <= S_BITS;
end
end
endcase
end
S_STOP: if (tick) begin
case (phase)
2'd0: begin sda_o <= 1'b0; phase <= 2'd1; end
2'd1: begin scl_o <= 1'b1; phase <= 2'd2; end
2'd2: begin sda_o <= 1'b1; phase <= 2'd3; end
2'd3: begin
busy <= 1'b0;
done <= 1'b1;
phase <= 2'd0;
state <= S_IDLE;
end
endcase
end
default: state <= S_IDLE;
endcase
end
end
endmoduleRead the acknowledge state, because it carries the I2C idea. In phase 0, SDA is released so the slave can drive it. SCL rises in phase 1, and in phase 2, while SCL is high, the master reads the line: low means the slave acknowledged, high means nothing pulled it down, and ack_err is set. After the fall in phase 3 the machine either goes to stop because of an error or after the second byte, or loads the data byte and returns to the bits state. A master that carried on after a missing acknowledge would clock a byte into the void.
A slave model and a testbench
The behavioural slave watches for a start, collects two bytes on rising SCK edges, and pulls SDA low during the ninth clock of each byte whose address matches. It is simulation-only code and is not meant to synthesise.
module i2c_slave_model #(
parameter [6:0] ADDR = 7'h48
) (
inout wire sda,
input wire scl
);
reg drive_low = 1'b0;
reg [7:0] sh = 8'h00;
reg [7:0] got_addr = 8'h00;
integer n, k;
assign sda = drive_low ? 1'b0 : 1'bz;
always @(negedge sda) begin
if (scl === 1'b1) begin : txn
for (k = 0; k < 2; k = k + 1) begin
for (n = 7; n >= 0; n = n - 1) begin
@(posedge scl);
sh[n] = sda;
end
@(negedge scl);
if (k == 0 && sh[7:1] != ADDR)
disable txn;
drive_low = 1'b1;
@(posedge scl);
@(negedge scl);
drive_low = 1'b0;
if (k == 0)
got_addr = sh;
end
$display("slave saw address byte 0x%02h, data byte 0x%02h", got_addr, sh);
end
end
endmoduleThe testbench instantiates the master with DIV = 2 so the simulation stays short, adds pull-ups and runs two transactions: one to the right address and one to an address nobody answers.
`timescale 1ns/1ps
module i2c_tb;
reg clk = 1'b0;
reg rst = 1'b1;
reg start = 1'b0;
reg [6:0] addr = 7'h00;
reg [7:0] data = 8'h00;
wire busy, done, ack_err;
wire scl, sda;
pullup (scl);
pullup (sda);
i2c_write_master #(.DIV(2)) master (
.clk(clk), .rst(rst), .start(start), .addr(addr), .data(data),
.busy(busy), .done(done), .ack_err(ack_err), .scl(scl), .sda(sda)
);
i2c_slave_model #(.ADDR(7'h48)) slave (.sda(sda), .scl(scl));
always #5 clk = ~clk;
task write_txn(input [6:0] a, input [7:0] d);
begin
@(posedge clk);
addr <= a;
data <= d;
start <= 1'b1;
@(posedge clk);
start <= 1'b0;
wait (done);
@(posedge clk);
$display("master done, ack_err=%b", ack_err);
#100;
end
endtask
initial begin
#30 rst = 1'b0;
#30;
write_txn(7'h48, 8'h1F);
write_txn(7'h50, 8'h1F);
$finish;
end
endmoduleiverilog -g2012 -o i2c_sim i2c_write_master.v i2c_slave_model.v i2c_tb.v
vvp i2c_simslave saw address byte 0x90, data byte 0x1f
master done, ack_err=0
master done, ack_err=1The first transaction shows the 7-bit address 0x48 as the byte 0x90 on the wire, the data byte arriving intact and the master seeing both acknowledges. The second addresses 0x50, which sends 0xA0, nobody answers, SDA stays high on the ninth clock, ack_err goes high and the master stops after the first byte without sending the data. That second case is the one that matters in real life, because on a board it is exactly what a wrong address, an unpowered device or a missing pull-up looks like.
Choosing DIV for a target bus speed
The SCL frequency is the system clock divided by four times DIV, and the divisor must round toward a slower bus, never a faster one, because I2C modes have maximum frequencies. For a 50 MHz clock:
| Target | Exact DIV | Rounded DIV | Actual SCL | Within limit |
|---|---|---|---|---|
| 100 kHz standard mode | 125.0 | 125 | 100.0 kHz | yes |
| 400 kHz fast mode | 31.25 | 31 | 403.2 kHz | no, too fast |
| 400 kHz fast mode | 31.25 | 32 | 390.6 kHz | yes |
Rounding 31.25 down to 31 breaks the fast-mode limit by a little, which a slave with tight input filters may not tolerate. Round up. The same quarter-bit tick also sets the minimum high and low times of SCL, which the specification lists in microseconds: at 100 kHz each half period is 5 microseconds, comfortably above the 4.0 and 4.7 microsecond minimums of standard mode.
Checking the waveform against the plan
When you open the VCD for the first transaction, the signals should follow the table of phases exactly. Before the first byte, SDA falls while SCL is still high, and only then does SCL fall. Each of the eight address bits changes SDA in the quarter after SCL has gone low, never while SCL is high. The ninth clock shows SDA released by the master and pulled low by the slave. A stop at the end shows SDA rising after SCL has risen. If you find an SDA edge between an SCL rise and the next fall that is not a start or a stop, a state is moving SDA in the wrong phase, and the phase column in the waveform tells you which state.
Adding clock stretching
A slave that needs time holds SCL low after the master released it. The master must notice and wait. Change phase 1 of the bits state so it advances only when the line really is high:
2'd1: begin
scl_o <= 1'b1;
if (scl) phase <= 2'd2;
endOn the first tick of phase 1 the master releases SCL, but scl may still read low for a cycle because of the pull-up's rise time or a stretching slave, and the phase counter stays put until it reads high. The effect is that the quarter-bit timer simply waits at no cost to correctness. The same change belongs in the acknowledge and stop states.
What this master leaves out
It writes one byte only. A register read needs a repeated start, a switch of the read/write bit and a controller that acknowledges received bytes, with a NACK on the last one. It does not handle clock stretching, where a slave holds SCL low to ask for more time; supporting that means checking that SCL actually rose before leaving phase 1. It has no arbitration for multi-master buses. Each of those is an extension of the same four-phase structure, and the most useful first one is clock stretching, because real sensors do it.


