# I2C Basics

## Introduction

In this topic page, we will discuss how I2C works in greater detail.

## The Physical Interface

There are only two wires that are used in I2C: SDA and SCL, which are the Serial Data Line and Serial Clock Line, respectively. Both lines are pulled up to Vcc through resistors. This is because the lines are open-drain lines, where the devices connected to these lines “drain” the line by pulling it to ground to transmit data.

Figure 1 below shows a generic I2C system where you can see the pull-up resistors on SDA and SCL, and that all the devices are connected to both lines. You'll also see that devices on an I2C network have one of two roles, master or slave. For most networks, there will be only one master, but there is multi-master support on I2C (we won't go into great detail about how it works, however).

The range of resistors that can be used is based on the total bus capacitance (each device adds some amount of capacitance to both SDA and SCL), and the mode that I2C is running (we will cover standard mode, but there is fast mode, fast mode plus, and high speed mode). The maximum capacitance allowed on a I2C network is 400 pF. The equation for the largest pull-up resistor that can be used is:

$R_{P_{\text{max}}} = \frac{t_r}{0.8473 C_b}$
Where the number 0.8473 comes from charging the bus from 0.3 Vcc to 0.7 Vcc (a similar process is discussed in this project, which covers how to debounce a button with a NE555 timer IC).

Where $R_{P_{\text{max}}}$ is the maximum pull-up resistance, $t_r$ is the rise time, which is 1000 ns for standard mode (these values can be found in the official specification), and $C_b$ is the total bus capacitance. Plugging in the maximum rise time on a full bus (400 pF) gives us 2.95 kΩ. The maximum pull-up resistance for a typical bus (around 100 pF) is 11 kΩ.

We now have a ballpark figure for the maximum resistance, but what about the minimum resistance? In that case, the limiting factor is current (too much current can damage components). We have the maximum current for standard mode at 3 mA. We are using a transistor to pull the line low, thus we must take into account the voltage drop from the transistor in the equation. That leaves a voltage drop of $V_{DD} - V_{OL}$, where $V_{OL}$ is the voltage drop across the transistor. So now we have the voltage and the current, but need to find the resistance. We can use Ohm's law to solve for the resistance from those values. The equation we get is:

$R_{P_{\text{min}}} = \frac{V_{DD} - V_{OL_{\text{max}}}}{I_{OL}}$
Where $V_{OL_{\text{max}}}$ is the maximum voltage drop across the transistor (usually about 0.7 V, but depends on the transistor). We'll use 5V as our VDD since it is a very common voltage. This gives the resistor value of about 1.5 kΩ.

So the general range of acceptable pull-up resistors is from 1.5 kΩ to about 11 kΩ for common I2C networks. However, these can be pushed if necessary, but it may result in a unreliable network where messages get corrupted due to noise.

## The Protocol

I2C, like UART, has a start and stop condition for a transmission. Unlike UART, I2C allows transmissions to restart, change modes (since only one device can transmit at a time), and send as much data as desired before the stop condition. The start condition is when SDA is pulled LOW while SCL is HIGH. The stop condition is when SDA goes from LOW to HIGH while SCL is HIGH.

Every transmission must start with the address of the slave the master wants to communicate with and whether the master wants to write to or read from the slave. This is the first byte of the transmission, since slave addresses are 7 bits (although 10 bit address are possible, but it depends on the device), and the eighth bit is the read/write bit which tells the slave whether to read data from the master, or to send data to the master.

What the master needs to have sent or what is received depends on the slave device. Every byte of data sent, including the slave address and R/W bit, has a ninth acknowledgment bit, which the receiver of that data pulls low to acknowledge the receipt of that data. Figure 2 displays a generalized packet for I2C, showing the start condition, the address with R/W byte and the ACK bit, data and its ACK bit, and the stop condition. It should be noted that data is sent MSB (most significant bit) first; it sends data from the left most bit to the right most bit.

The master devices added to a I2C network must have support for multi-mastering. Since a single-master device won't “listen” the network before transmission, or stop transmission if another master “wins” arbitration if they send at the same time.

Each multi-master device detects when there is communication on the bus, and waits until a stop condition before starting its transmission. Arbitration is done when two or more multi-masters start transmission. The multi-master writes more zeros than the others and wins the right to transmit and the other masters discontinue their transmissions and wait until a stop condition before trying again.

#### Expanding I2C Networks

There are several ways to expand an I2C network, as well as deal with slave address conflicts without requiring a completely separate network. The two main network components that expand an I2C network are buffers and multiplexers.

Buffers simply pass signals through them, while isolating each local network. This allows the total bus capacitance on each local network to stay below 400 pF, and allow for more devices than the capacitance would allow for normally.

Multiplexers are more complex, as they have a control register to select which downstream network to communicate upstream. This allows multiple slaves to exist on a single network since they can be separated via multiplexer and thus no conflicts or collisions will occur.