
“If you are working on a project with an OLED Display, a barometric pressure sensor or a gyroscope/accelerometer module, you may find that the I2C bus is being used.
“
If you are working on a project with an OLED Display, a barometric pressure sensor or a gyroscope/accelerometer module, you may find that the I2C bus is being used.
Introduction to the I2C bus
I2C combines the advantages of SPI and UART. Using I2C, you can connect multiple slave devices to a single master device (like SPI) and have multiple master devices control single or multiple slave devices. This is useful when you want to have multiple microcontrollers logging data to a single memory card or displaying text to a single LCD.
Like UART communication, I2C uses only two wires to transfer data between devices:
SDA (Serial Data) – The line on which the master and slave send and receive data.
SCL (Serial Clock) – The line that carries the clock signal.
I2C is a serial communication protocol, so data is transferred bit by bit along a single wire (SDA line).
Like SPI, I2C is synchronous, so the bit output is synchronized to the bit sampling by a clock signal shared between the master and slave. The clock signal is always controlled by the host.
How I2C Works
When using I2C, data is converted into messages, and messages are decomposed into data frames. Each message has an address frame containing the binary address of the slave, and one or more data frames containing the data being transferred. The message also includes start and stop conditions, read/write bits and ACK/NACK bits between each data frame:
picture.png
Start Condition: The SDA line transitions from high to low before the SCL line transitions from high to low.
Stop condition: After the SCL line switches from low to high, the SDA line switches from low to high.
Address Frame: A sequence of 7 or 10 bits unique to each slave that is used to identify the slave when the master wants to communicate with it.
Read/Write Bit: A single bit that specifies whether the master sends data to the slave (low voltage level) or the slave requests data (high voltage level).
ACK/NACK bit: Each frame in the message is followed by an Acknowledge/Non-Acknowledge bit. If the address frame or data frame is successfully received, an ACK bit is returned from the receiving device to the sender.
address
I2C doesn’t have a slave select line like SPI, so it needs another way to let the slave know that data is being sent to it, not another slave. It does this by address. The address frame is always the first frame after the start bit in a new message.
The master device sends the address of the slave device with which it communicates to each slave device connected to it. Each slave device then compares the address sent from the master device with its own address. If the addresses match, a low voltage ACK bit is sent back to the host. If the addresses do not match, the slave device does nothing and the SDA line remains high.
read/write bit
The address frame includes a bit at the end that informs the slave whether the master wants to write data to it or receive data from the master. The read/write bit is low if the master wants to send data to the slave. This bit is high if the master is requesting data from the slave.
data frame
After the master detects the ACK bit from the slave, it is ready to send the first data frame.
Data frames are always 8 bits long and sent MSB first. Immediately following the ACK/NACK bit of each data frame to verify that the frame was successfully received. Before sending the next data frame, either the master or the slave must receive the ACK bit (depending on who sent the data).
After all data frames have been sent, the master can send a stop condition to the slave to stop the transmission. The stop condition is that after a low-to-high transition on the SCL line, the SDA line goes from low to high and the SCL line remains high.
I2C data transfer steps
1. The host sends data to each connected slave device, then switches the SDA signal from high to low, and then switches SCL from high to low.
2. The master sends each slave the 7 or 10-bit address of the slave it wants to communicate with, along with the read/write bits:
3. Each slave device compares the address sent by the master with its own address. If the addresses match, the slave returns the ACK bit by pulling the SDA line low by one bit. If the address of the master device does not match the address of the slave device, the slave device holds the SDA line high.
4. The master device sends or receives data frames:
5. After each data frame has been transmitted, the receiving device returns another ACK bit to the sender to confirm successful receipt of the frame:
picture.png
6. To stop the data transfer, the master sends a stop condition to the slave by toggling SCL high before toggling SDA high:
Single master with multiple slaves
Since I2C uses addressing, multiple slave devices can be controlled from a single master device. With 7-bit addresses, 128 (27) unique addresses can be used. Using 10-bit addresses is not common, but provides 1,024 (210) unique addresses. To connect multiple slaves to a single master, connect them like this, using 4.7K ohm pull-up resistors to connect the SDA and SCL lines to Vcc:
There are multiple slave devices and multiple master devices
Multiple masters can be connected to a single slave or to multiple slaves. The problem of multiple masters in the same system occurs when two masters try to send or receive data simultaneously over the SDA line. To solve this problem, each master needs to detect whether the SDA line is low or high before sending a message. If the SDA line is low, it means that another master has control of the bus and the master should wait to send a message. If the SDA line is high, then it is safe to transmit the information. To connect multiple masters to multiple slaves, use the diagram below to connect the SDA and SCL lines to Vcc using 4.7K ohm pull-up resistors:
picture.png
Advantages and disadvantages of I2C
Compared to other protocols, I2C sounds complicated and is not easy to implement in a program causing problems such as data loss, no response, “dead, etc.”. But there are many advantages:
advantage
Use only two wires
Supports multiple masters and multiple slaves
ACK/NACK bits confirm that each frame was successfully transmitted
Hardware is not as complex as UART
Well-known and widely used protocols
shortcoming
Data transfer rate is slower than SPI
The size of the data frame is limited to 8 bits
Implement more complex hardware than SPI
The Links: 7D50D-050EHR SKIIP26AC126V1