Before we are going to Understand the I2C protocol, first we understand what is communication protocol?
what is communication protocol?
A communication protocol is a set of rules that defines how data is transmitted and received between devices
Communication
- Communication is the exchange of data between two or more devices.
- In electronics, this typically involves a transmitter (TX) sending data and a receiver (RX) receiving it.
Protocol
- A protocol is a set of rules that define how data should be transmitted between devices.
- These rules include data format, speed (baud rate), error checking, timing, and transmission method (wired/wireless).
Introduction to I2C (Inter-Integrated Circuit)
- I2C is a two-wire communication protocol used to connect multiple devices like sensors, displays, and memory chips with microcontrollers. It is widely used in embedded systems due to its simple wiring and efficient data transfer.
- Unlike UART (which is asynchronous), I2C is synchronous, meaning all devices share a common clock signal for precise communication. This allows multiple masters and slaves to communicate on the same bus without conflicts.
- I2C is ideal for short-distance, low-speed communication, making it perfect for applications like sensor interfacing (temperature, pressure, motion) with microcontrollers like Arduino, STM32, and Raspberry Pi.
Synchronization
Serial communication
Basics of I2C Communication
I2C uses only two bidirectional lines to communicate:
- SDA (Serial Data) Line – Transfers data between devices.
- SCL (Serial Clock) Line – Synchronizes the communication between the master and slave devices.
- The master device generates the clock signal and initiates communication, while the slave devices respond based on their unique addresses.
Key Features of I2C Communication
✅ Synchronous Communication
Unlike UART, I2C uses a shared clock (SCL) to keep all devices synchronized, ensuring reliable data transfer.
✅ Multi-Master and Multi-Slave Support
I2C allows multiple master devices to communicate with multiple slave devices on the same bus. Each slave is identified by a unique 7-bit or 10-bit address.
✅ Two-Wire Interface
I2C operates with only two lines (SDA & SCL), reducing the number of connections needed for communication, making it ideal for microcontroller-based systems.
✅ Address-Based Communication
Each slave device has a unique address that allows the master to select and communicate with a specific device without interfering with others.
✅ Clock Stretching for Synchronization
If a slave device is not ready to send data, it can hold the clock line low (clock stretching) to delay communication until it is ready.
✅ Adjustable Data Rate
I2C supports multiple speed modes:
Standard Mode → 100 kbps
Fast Mode → 400 kbps
Fast Mode Plus → 1 Mbps
High-Speed Mode → 3.4 Mbps
How I2C Works: Step-by-Step Communication Process
1️⃣ Start Condition (S)
What is the Start Condition?
- The master initiates communication with a Start Condition (S).
- It pulls the SDA line LOW while the SCL line is HIGH.
- This signals all slave devices that the master is about to send data.
Why is the Start Condition Important?
- It alerts all slaves on the bus that a new transmission is beginning.
- It acts as a synchronization signal for all devices.
Example in Embedded Systems
🔹 If an Arduino (Master) wants to communicate with an EEPROM (Slave), it first sends the Start Condition before transmitting the address of the EEPROM.
2️⃣ Slave Address & Read/Write Bit
What is the Slave Address?
- The master sends a 7-bit (or 10-bit) address to specify which slave it wants to communicate with.
- The 8th bit (LSB) is the Read/Write (R/W) bit:
- 0 (Write) → Master is sending data to the slave.
- 1 (Read) → Master is requesting data from the slave.
Why is the Address Important?
- I2C allows multiple slaves on the same bus.
- The correct slave must be selected for proper communication.
Example in Embedded Systems
🔹 The master sends 10100000 (EEPROM address 0x50, Write mode) when writing data to an EEPROM memory module.
3️⃣ Acknowledge Bit (ACK/NACK)
What is Acknowledge (ACK) and Not Acknowledge (NACK)?
- After receiving the address or data byte, the receiving device must send an ACK (Acknowledge).
- The receiver pulls the SDA line LOW during the ACK bit to confirm it received the data.
- If the receiver does not acknowledge, it sends a NACK (Not Acknowledge) by leaving SDA HIGH.
Why is Acknowledgment Important?
- Ensures that data is correctly received.
- If the slave doesn’t send an ACK, the master knows there is a communication issue.
Example in Embedded Systems
🔹 If the master sends an address to an I2C LCD display, and the LCD sends ACK (LOW), it means the LCD is ready to receive data.
4️⃣ Data Transfer (Byte-Wise)
How is Data Transferred in I2C?
- Data is transferred byte-by-byte (8 bits at a time).
- The MSB (Most Significant Bit) is sent first.
- The receiver must ACK each byte for successful transmission.
- Communication continues until the master issues a Stop Condition (P).
Why is Byte-Wise Transfer Used?
- Ensures compatibility across different devices.
- Allows efficient and synchronous data transfer.
Example in Embedded Systems
🔹 If an Arduino (Master) is sending the value 0x3A to a temperature sensor (Slave), it sends 8 bits (00111010) one by one.
5️⃣ Stop Condition (P)
What is the Stop Condition?
- The master terminates the communication with a Stop Condition (P).
- The SDA line is pulled HIGH while SCL remains HIGH.
Why is the Stop Condition Important?
- Marks the end of communication.
- Frees the I2C bus for the next communication.
Example in Embedded Systems
🔹 After reading sensor data from a BMP180 barometric sensor, the master issues a Stop Condition to end the transmission.
6️⃣ Clock Stretching
What is Clock Stretching?
- Sometimes, a slave device is not ready to send or receive data.
- The slave can hold the SCL line LOW to pause the master.
- The master must wait until the slave releases SCL (pulls it HIGH) before continuing.
Why is Clock Stretching Important?
- Prevents data loss if the slave is too slow.
- Allows reliable communication with slower sensors or peripherals.
Example in Embedded Systems
🔹 A slow EEPROM module may use clock stretching while processing a write operation to prevent the master from sending more data before it’s ready.
🔍 Summary Table of I2C Communication Steps
Advanced Concepts in I2C
🔹 Clock Stretching
Some slave devices may need more time to process data. Instead of missing data, they hold SCL low, making the master wait until they are ready to send/receive data.
🔹 Repeated Start Condition
Instead of stopping and restarting communication, the master can send a Repeated Start Condition to continue communication with another slave without releasing the bus.
🔹 I2C Bus Arbitration
When multiple masters are present, two masters may attempt to communicate simultaneously. Bus arbitration ensures that only one master can take control based on priority.
🔹 Pull-Up Resistors in I2C
I2C lines require pull-up resistors (typically 4.7kΩ – 10kΩ) to maintain proper signal levels and prevent floating states.
Key Applications of I2C
✅ Sensor Communication – Connecting temperature, pressure, motion, and other sensors to microcontrollers.
✅ EEPROM & Memory Devices – Reading and writing data in EEPROMs and FRAMs using I2C.
✅ Display Modules – Communicating with LCDs and OLED displays.
✅ Battery Monitoring – Monitoring battery charge levels in embedded systems.
✅ Real-Time Clocks (RTC) – Synchronizing time with RTC modules like DS1307.
✅ Communication Between Microcontrollers – Interfacing multiple microcontrollers in embedded systems.
Comparison: I2C vs. UART vs. SPI
I2C Bus Speed Modes
I2C (Inter-Integrated Circuit) has multiple speed modes that define how fast data is transmitted between devices. The main speed modes of the I2C bus are:
- Standard Mode (SM)
Speed: Up to 100 kbit/s
Most commonly used in simple applications.
2. Fast Mode (FM)
Speed: Up to 400 kbit/s
Used in applications requiring moderate speed.
3. Fast Mode Plus (FM+)
Speed: Up to 1 Mbit/s
Requires stronger pull-up resistors for better performance.
4. High-Speed Mode (HS Mode)
Speed: Up to 3.4 Mbit/s
Requires a special high-speed master, while slaves operate at lower speeds.
5. Ultra-Fast Mode (UF Mode)
Speed: Up to 5 Mbit/s
Uses push-pull drivers instead of open-drain.
Understanding I2C Read/Write Operations
I2C (Inter-Integrated Circuit) is a widely used communication protocol in embedded systems, enabling multiple devices to communicate using just two wires: SDA (Serial Data Line) and SCL (Serial Clock Line). In this blog, we will discuss the process of reading from and writing to a slave device using the I2C protocol.
Writing to a Slave Device
When a master device writes data to a slave device, the following sequence of operations takes place:
START Condition: The master initiates communication by sending a START condition on the I2C bus.
Send Slave Address with Write Bit (0): The master sends the 7-bit slave address followed by a write bit (0).
ACK from Slave: The slave acknowledges (ACK) the received address.
Send Register Address (Optional): The master may send the register address within the slave device where data needs to be written.
ACK from Slave: The slave acknowledges the received register address.
Send Data Byte(s): The master sends one or more data bytes to be written into the slave device.
ACK from Slave: The slave acknowledges each byte received.
STOP Condition: The master terminates the communication by sending a STOP condition.
Reading from a Slave Device
To read data from a slave device, a slightly different sequence is followed:
START Condition: The master initiates communication with a START condition.
Send Slave Address with Write Bit (0): The master first sends the 7-bit slave address with a write bit (0) to indicate that it will send a register address.
ACK from Slave: The slave acknowledges the address.
Send Register Address (Optional): The master sends the address of the register from which data needs to be read.
ACK from Slave: The slave acknowledges the register address.
Repeated START Condition: The master sends a repeated START condition to switch to read mode.
Send Slave Address with Read Bit (1): The master sends the 7-bit slave address again, but this time with the read bit (1).
ACK from Slave: The slave acknowledges and starts sending data.
Master Receives Data: The slave transmits data to the master.
ACK from Master (for multiple bytes): If multiple bytes are to be read, the master sends an ACK after each received byte.
NACK and STOP Condition: After receiving the final byte, the master sends a NACK (Not Acknowledge) followed by a STOP condition to terminate communication.