I2C
Pros: Only requires 2 pins, multiple devices can be included on a single bus, and allows two-way communication. Multiple master devices are possible. Multiple slave devices are possible–without the cost of extra pins.
Cons: This approach is not as fast as SPI, devices can have address conflicts, and software setup is required
General Information: I2C
I2C stands for Inter-Integrated Circuit and is pronounced "I squared C", "I two C" or "I-I-C". I2C is a protocol that allows one device to exchange data with one or more connected devices through the use of a single data line and clock signal.
I2C is a Master/Slave protocol, which means that any number of master nodes can be present. Additionally, master and slave roles may be changed between messages.
I2C only requires two communication connections:
I2C Communication Connections
SCL
This is the clock signal that keeps the device and the module synchronized while transferring data. The two devices need to have a mutual understanding of how fast data is to be transferred between them. This is sometimes referred to as the baud or bitrate. The clock signal provides that reference signal for the devices to use when exchanging data. Without a clock signal to synchronize the devices, they would have no way to interpret the signal on the data lines.
SDA
This is the data line used for exchanging data between the master and slaves. Instead of having separate communication lines for the master and slave devices, they both share a single data connection. The master coordinates the use of that connection so that only one device is "talking" at a time.
Since multiple slave devices can use the same SDA line, the master needs a way to distinguish between them and talk to a single device at a time. The I2C protocol uses the concept of device addressing to coordinate traffic on the data line.
Every I2C device connected to the microcontroller of the device will have an internal address that cannot be the same as any other module connected to the microcontroller. This address is usually determined by the device manufacturer and listed in the datasheet. Sometimes you can configure the address through device-specific tweaks defined by the manufacturer.
I2C Device Addresses
The following diagram illustrates how the SDA and SCL pins are toggled when transferring data with the I2C protocol.
Flow of data between multiple I2C devices
To begin a data transaction, the master creates what is called a start condition by pulling the SDA pin low before the SCL pin.
The master then broadcasts the address of the device it wishes to communicate with by sending each bit of the 7-bit address. Notice that the clock signal (SCL) is toggled for each bit. This toggling is how the slaves know when to read each bit of the address, so they can determine with which device the master wants to communicate.
Right after the address, the master sends a read/write bit, which signals whether it will be sending data to the slave or reading data from the slave.
After broadcasting the address, the master either transmits data to the slave or sends the address of a register (internal storage) on the slave from which it wishes to retrieve data.
Finally, the master will issue a stop condition on the bus by pulling the SCL high, followed by pulling the SDA high.