I2C Protocol
Overview and How I2C Works
I2C communication protocol which is also known as inter-integrated communication protocol was developed by Philips Semiconductors in 1982.This can transfer data between a central processor and multiple ICs on the same circuit board using just two common wires and it is widely adopted for short distance synchronous serial communication between microcontrollers, sensor arrays, displays, IoT devices, EEPROMs & etc. The I2C multi master bus consists of two active wires (Serial Data/SDA and Serial Clock/SCL) and data is transferred bit by bit along a single wire.
I2C design has an address space of 7 bit and it is mainly used in bus speed modes such as standard mode(100kbit/s) and fast mode(400kbit/s). There is a low speed mode(10kbit/s), but arbitrary low clock frequencies are accepted, and the latest upgrades have speeds up to 5mbit/s. Address space and the total bus capacitance limits the number of nodes which can exist on a given I2C bus and also restricts the communication distance to few meters.
I2C Hardware Principles
The data that needs to be transferred is sent to the Serial Data Line (SDA) wire and is synchronized with the clock signal from Serial Clock Line (CL). The devices on the I2C network are connected to the above lines as shown in the above figure.
The two I2C bus lines are operated as open drain drivers. This means that any device on the I2C network can drive SDA and SCL low, but they cannot drive them high. Therefore, a pull up resistor is used for each bus line, to keep them positive(high) by default. By using an open drain system, the chances of shorting will be reduced.
The devices which are connected to the I2C bus are called masters or slaves. Only a single master stays active on the I2C bus at any instant. Master controls the SCL clock line and the SDA lines and communicates with the slave devices.
When transferring data to or from a slave device, the master specifies a 7-bit address which is assigned to the slave device on the SDA line and then proceeds the data transfer. Therefore, communication is done effectively between the master device and the slave device.
I2C Data Transmission
The data transferring is done in messages, which are broken up into frames of data. The message includes address frame that contains the binary address of the slave, start/stop conditions, read/write bits and ACK/NACK between each data frame.
- Start Condition: In start condition, the SDA line switches from a high voltage (‘1’ bit) level to a low voltage level (‘0’ bit) before the SCL line switches from high to low.
- Address Frame/Slave Address: A 7-bit sequence unique to each slave that identifies the slave when the master device needs to send/receive data.
- Read/Write Bit: A single bit specifying whether the master is sending data to the slave (the bit is set to ‘0’) or requesting data from the slave (the bit is set to ‘1’)
- ACK/NACK Bit: Each frame in a message is followed by an acknowledge/not-acknowledge bit. If the physical address of any slave device matches with the address sent by the master device, an ACK bit is returned to the sender from the receiving device. (if matches the bit is set to ‘0’ otherwise it remains ‘1’)
- Stop Condition: When the required data blocks are transferred through the SDA line, the SDA line switches from a low voltage (‘0’ bit) level to a high voltage level (‘1’ bit) before the SCL line switches from high to low.
- Data Frame:
When the master device detects the ACK bit from the slave device, the first data frame is ready to be sent. The data frame comprises of 8 bits and is sent by the sender with the most significant bit first. Each data frame is immediately followed by an ACK/NACK bit and is set to ‘0’ by the receiver to verify that the frame has been received successfully. Otherwise, it remains at its default value ‘1’. Depending on who is sending the data, the ACK/NACK bit must be received by either the master device or the slave device before the next data frame can be sent.
Transmission is stopped when the master sends a stop condition to the slave after all the data frames have been sent. The stop condition is a voltage switching from low (‘0’ bit) to high (‘1’ bit) on the SDA line after a low (‘0’ bit) to high(‘1’ bit) voltage switching on the SCL line, with the SCL line remaining high.
How Addressing works
Addressing is done to let the slave know that data is being sent to it, and not another slave. The address frame comes after the first frame after the start bit in a new message.
First, the master device will issue a START condition. This acts as an ‘Attention’ signal to all of the connected devices to the bus. Then the slave devices will listen to the bus for incoming data.
Then the master device sends the ADDRESS of the device it wants to access, along with an indication of whether the access is a Read or Write operation. Having received the address, all slave devices will compare it with their own address. If the address doesn’t match, they simply wait until the bus is released by the stop condition. If the address matches, however, the device will produce the ACKNOWLEDGE signal.
Once the master receives the acknowledge, it can start transmitting or receiving DATA. When all is done, the master will issue the STOP condition. This signal is released by the bus and that the connected ICs may expect another transmission to start any moment.
Reading and Writing
The address frame includes a single bit at the end which informs the slave whether the master wants to write data (send data) to it or read data (receive data) from it. If the master wants to write data to the slave, the bit is set to ‘0’. If the master is reading data from the slave, the bit is set to’1’.
I2C Buffer
I2C buffer is used to reduce bus capacitance, control rise times, add additional devices to the bus, interface devices operating at different voltages, or even to implement a redundant bus. It divides the I2C bus into two separate buses, while allowing devices to communication across it and reducing the total bus capacitances. This means that for the same pull-up resistors, we get a lower RC time constant and thus a shorter rise time by implementing an I2C buffer to the network.
Single Master with Multiple Slaves
I2C protocol uses addressing therefore multiple slaves can be controlled from a single master. When we use 7-bit address, 128 unique addresses are available for the communication. To connect multiple slaves to a single master, follow the below diagram, connect pull-up resistors with the SDA and SCL lines to Vcc.
Multiple Masters with Multiple Slaves
I2C protocol supports having multiple master devices on the same bus. Therefore, multiple masters can be connected to single or multiple slaves. The problem with multiple masters is signal collision takes place, when several masters try to send or receive data at the same time over the SDA line. To eliminate this error, each master needs to detect if the SDA line is low or high before transmitting a message. If the SDA line is low, it means that another master is controlling the bus, and the master should wait to send the message. When the SDA line is high, then master can safely transmit the message.
I2C clock stretching
There may be instances when the master’s data rate exceeds the slave’s ability to provide data. This happens when the data which must be transmitted by the slave is not ready yet or the previous operation is not yet finished. Therefore, the slave adopts clock stretching. Slave normally do this in situations when it needs more time to process incoming data, or data needed to be sent. When the clock stretching takes place, the master must wait for the clock to go high again. This can also be done by the master device, but the term usually applies to the scenario in which the slave drives the SCL, since usually only the master is driving the SCL.
Advantages and Disadvantages of I2C protocol
Advantages | Disadvantages |
Uses only two wires. | Slower data transfer rate.
|
Supports multi-master and multi-slave modes. | Limited size of the data frame (8 bits).
|
Confirmation that each frame is transferred successfully with ACK/NACK bit.
|
I2C is a half-duplex protocol which is complex when compared to other protocols. |
Widely used protocol. | Complex hardware and firmware when compared other protocols. |
Good error handling functionality. |
I2C vs SPI Communication
I2C | SPI |
Supports multi-master and multi-slave mode | Only supports multi-slave mode |
Half duplex communication protocol | Full duplex communication protocol |
Has clock stretching. | No clock stretching. |
Needs only 2 wires to communicate. | Needs three or four wires to communicate. |
Slower than SPI. | Faster than I2C. |
Draws more power than SPI. | Draws less power than I2C. |
Less susceptible to noise than SPI. | More susceptible to noise than I2C. |
Implementation is cheaper than SPI. | Implementation is costly than I2C. |
Works on wire and logic and it has a pull-up resistor. | No need of a pull-up resistor in the case of the SPI. |
Address based bus protocol, need to send the address of the slave for the communication. | Need to select the slave using the slave select pin for the communication. |
Long distance communication when compared to SPI. | Short distance communication when compared to I2C. |
Developed by NXP in 1982. | Developed by Motorola in 1979. |
I2C Connection with Devices
Arduino has built in dedicated pins for I2C communication, which have built in pull up resistors as required by the I2C protocol. By going through the Arduino board details we can find the SDA and SCL pins (normally A4 and A5 ins in Arduino UNO). Raspberry pi supports both single and multi-master configurations. It also has 3 types of interface on the GPIO header. Raspberry pi I2C interfaces aren’t enabled by default and need some extra configuration before you can use them. After connecting the wires accordingly to the devices, you can can start the communication with the devices with the help of related libraries.
Following is an example of using a I2C module to control and LCD display using Arduino. With help of this module we can control the LCD display by using only two pins of the Arduino. Connect the pins of the I2C module and the Arduino board as follows: GND – GND, Vcc – 5v, SDA – A4, SCL – A5. After connecting the wires properly, you can start programming to control the module. For the below example we used “LiquidCrystal” library and common commands like lcd.begin (), lcd.print () and lcd.setCursor () for display control.