For an embedded system if the microcontroller is the brain then the input and output devices form the sensory parts of the embedded system. In other words, the system consists of two microcontrollers one which overlooks everything and the other is inside the input or output device. Both microcontrollers interact with each other to exchange data. This exchange of data follows a standard set of rules called the communication protocol. In this article, we would be exploring one such protocol called I2C.
Introduction
I2C stands for Inter-Integrated Circuit serial communication protocol. In other words data is sent bit by bit serially to the receiver. It uses two wires(channels) for communication. The two channels respectively are SCL (Serial Clock) and SDA(Serial Data). It compares with other serial communication protocols like UART, SPI with I2C being the hybrid of the two.I2C is a synchronous communication protocol. The master and slave both use the same clock to synchronise the bits and for sampling the data.
The Message
Whenever a system uses I2C protocol the message has a certain specific structure. A standard I2C message consists of the following:
Start Condition: This is the bit which signals to the slave that communication is about to start. The SDA line switches from HIGH to LOW before the SCL line switches HIGH to LOW.
Stop Condition: This is the bit which signals the completion of communication. It is opposite of the Start condition in other words the SDA switches from LOW to HIGH after SCL switches from LOW to HIGH.
Address Frame: This part of the message consists of the address of the slave. The address is used to locate and send or retrieve the data from the slave. It is generally 7-10 bits long. Hence it can identify 2^N slaves uniquely.
Read/Write: This bit defines the operation to be performed. It tells whether we are sending data to the slave or are requesting the data. Generally, 0 indicates write operation and 1 indicates read operation.
ACK/NACK: It is the acknowledge bit sent in after every message to verify whether the communication was successful or not.
The Data Frame: It is the 8 bits long part of the message which is the actual data that we are sending or have requested from a slave. It is always sandwiched between the ACK/NACK bits for verification. After getting the acknowledgement this 8 bits are sent serially with MSB first.
How it works?
The master first sends the start bit followed by the address of the slave it wants to communicate with. To do so the master pulls the SDA line from HIGH to LOW before SCL goes from HIGH to LOW. Each slave compares the address with its own. If the address matches with that of the slave it sends and acknowledgement by pulling the SDA line low for one bit. The address is followed by read or write bit, after acknowledgement is received the data is transferred from master to slave or vice versa. The communication is terminated after the master pulls SDA HIGH before SCL goes HIGH.
I2C Vs other protocols
As mentioned above I2C is a hybrid version of UART and SPI communication. Unlike SPI communication it gives you the choice of having multiple masters and multiple slaves in the system while using lesser hardware resources. The UART has a pre fixed data transfer rate and hence there is a possibility of data loss. I2C overcomes this by making the data transfer synchronous and hence reducing the possibility of data loss. As, for each data transfer cycle you also have to send the extra bits in form of Start/Stop, ACK it makes system a bit inefficient and also make it slow as compared to SPI. Also, because it doesn’t have a separate bus for address the hardware implementation of a system with multiple masters is a bit more complex. To conclude I2C is a cheaper solution for a system which requires the protocol to be reliable, efficient and fast.