I2C (Inter-Integrated Circuit) is a multi-master, multi-slave, packet-switched, single-ended, serial communication bus. It is widely used in embedded systems to connect low-speed devices such as sensors, EEPROMs, real-time clocks, and microcontrollers. I2C is known for its simplicity and efficiency, requiring only two communication lines: a data line (SDA) and a clock line (SCL). This makes it ideal for applications where minimizing pin usage is critical.
I2C does not refer to a specific physical component but rather a protocol. However, devices using I2C typically have the following pins:
Pin Name | Description |
---|---|
SDA | Serial Data Line: Used for transmitting and receiving data |
SCL | Serial Clock Line: Carries the clock signal for synchronization |
GND | Ground: Common ground for the communication |
VCC | Power Supply: Provides power to the device (e.g., 3.3V or 5V, depending on the system) |
Below is an example of interfacing an I2C temperature sensor (e.g., TMP102) with an Arduino UNO:
#include <Wire.h> // Include the Wire library for I2C communication
#define TMP102_ADDRESS 0x48 // I2C address of the TMP102 sensor
void setup() {
Wire.begin(); // Initialize I2C communication
Serial.begin(9600); // Start serial communication for debugging
}
void loop() {
Wire.beginTransmission(TMP102_ADDRESS); // Start communication with TMP102
Wire.write(0x00); // Point to the temperature register
Wire.endTransmission(); // End transmission
Wire.requestFrom(TMP102_ADDRESS, 2); // Request 2 bytes of data from TMP102
if (Wire.available() == 2) { // Check if 2 bytes are available
int msb = Wire.read(); // Read the most significant byte
int lsb = Wire.read(); // Read the least significant byte
// Combine the bytes and calculate the temperature
int temperature = ((msb << 8) | lsb) >> 4;
float celsius = temperature * 0.0625; // TMP102 resolution is 0.0625°C
Serial.print("Temperature: ");
Serial.print(celsius);
Serial.println(" °C");
}
delay(1000); // Wait 1 second before the next reading
}
No Communication on the Bus:
Devices Not Responding:
Data Corruption:
Clock Stretching Issues:
Q: Can I connect multiple masters on the same I2C bus?
Q: What happens if two devices have the same I2C address?
Q: How do I calculate the pull-up resistor value?
Q: Can I use I2C with 3.3V and 5V devices on the same bus?
This documentation provides a comprehensive guide to understanding and using the I2C protocol effectively in your projects.