The I2C (Inter-Integrated Circuit) module is a serial communication protocol that enables data exchange between microcontrollers and various peripherals over a two-wire bus. It is widely used in embedded systems for connecting low-speed devices like sensors, EEPROMs, and displays. The I2C protocol supports multiple masters and slaves, allowing for complex communication networks with simple wiring.
Pin Name | Description |
---|---|
SDA | Serial Data Line, bidirectional data transfer |
SCL | Serial Clock Line, clock signal provided by master |
VCC | Power supply (3.3V or 5V depending on module) |
GND | Ground |
#include <Wire.h> // Include the I2C library
void setup() {
Wire.begin(); // Join the bus as a master or a slave
Serial.begin(9600); // Start serial communication at 9600 baud
}
void loop() {
Wire.beginTransmission(0x50); // Begin transmission to a device with address 0x50
Wire.write(byte(0x00)); // Write a byte to the I2C bus
Wire.endTransmission(); // End transmission
Wire.requestFrom(0x50, 1); // Request 1 byte from the device at address 0x50
while(Wire.available()) { // While there is data available to read
char c = Wire.read(); // Read a byte
Serial.println(c); // Print the byte to the Serial monitor
}
delay(1000); // Wait for 1000 milliseconds
}
Q: Can I connect multiple masters to an I2C bus? A: Yes, the I2C protocol supports multi-master configurations, but careful management of bus arbitration and collision detection is necessary.
Q: How do I find the address of my I2C device? A: The address is usually specified in the device's datasheet. Alternatively, you can use an I2C scanner sketch to find connected devices on the bus.
Q: What are pull-up resistors, and why are they necessary? A: Pull-up resistors are used to ensure that the bus lines are at a defined logic level when no device is actively driving them. They are necessary for the proper operation of the I2C bus.