So far we’ve talked about wireless technologies and UART communications. In this tutorial, we will talk about Inter-Integrated Circuits or I2C.
I2C
I2C (Inter-Integrated Circuits) is a serial communication protocol that connects multiple slaves to a single master and vice versa. This feature is particularly useful for peripheral devices that log data into a single microcontroller. Moreover, just like UART communications, it only needs two wires to communicate. These two wires are:
SDA (Serial Data Line) – This line is where the master and slave devices communicate.
SCL (Serial Clock Line) – This line synchronizes the devices. It carries the clock signal.
I2C is synchronous so it needs a clock signal. To know more about synchronous and asynchronous communication, check the previous article.
Anatomy of an I2C message
Start Condition: A start bit is a LOW added to the beginning of a data packet to initiate data transfer. In I2C, to initiate the message, the SDA line drops from a HIGH to LOW before the SCL line switches from HIGH to LOW.
Stop Condition: A stop bit pulls the digital signal to HIGH for at least two-bit durations to indicate the end of the transmission. The SDA line rises from LOW to HIGH after the SCL line switches from LOW to HIGH.
Address Frame: A series of 7 or 10 bits distinct to every master or slave device.
Read/Write Bit: This bit specifies if the master is sending data (LOW) or requesting (HIGH).
ACK/NACK Bit: ACK/NACK is short for Acknowledge and No-Acknowledge bits. A receiving device returns an ACK if the data is received without errors. Alternatively, if the message is corrupted or incomplete, the receiving device sends a NACK.
How I2C Works
1. The master initiates start condition by dropping SDA signal to LOW followed by the SCL.
2. The master sends every slave a 7 or 10 address bit to identify the target slave for communication. Then, it sends a read or write bit.
3. Each slave compares the address bit sent by the master to its own address. If the address matches, it returns an ACK by dropping the SDA signal LOW for a moment.
4. The master communicates with the target slave.
5. After the entire data has been sent, the slave returns an ACK to let the master know that it received the message successfully.
6. Finally, to stop the communication between the master and the target slave, the master initiates stop condition. It returns both SDA and SCL lines to HIGH. It does this to SCL first then the SDA.
Advantages
- Requires only 2 lines of communication
- It can support multiple masters and slaves
- Chip addressing
- Simpler than UART
Disadvantages
- Slow data rate
- Requires more space
- Conflicts may arise because of chip addressing
In conclusion, I2C is a robust wireless communication protocol for controlling peripheral devices because it supports multiple master and slave devices and just like UART, it only requires two lines to communicate.
Here are the default I2C lines in different Arduino boards:
Uno, Ethernet?: ?A4 (SDA), A5 (SCL)
Leonardo?: ?2 (SDA), 3 (SCL)
Due?: ?20 (SDA), 21 (SCL), SDA1, SCL1
Mega2560?: ?20 (SDA), 21 (SCL)
That’s it for I2C! Hope to see you at the final one!