I2C Part 1 - Introducing I2C
An introduction to the I²C protocol
Created: 09/05/2020 | Last Updated: 27/05/2020
This tutorial is the first in a four-part series on the communication protocol I²C; explaining what it is, how it works and how you can use it on your Raspberry Pi.
For the other tutorials in this series click on the links below.
What is I²C?
I²C, pronounced I-squared-C or I-2-C, is a popular serial communication protocol originally developed by Phillips Semiconductor, now known as NXP Semiconductor. Today I²C is used by most major IC manufacturers on a wide range of devices from microcontrollers, ADCs and DACs to EEPROMs, I/O devices and real-time clocks.
I²C is designed to be a multi-slave, half-duplex bus meaning a master device can communicate with multiple slave devices connected to the same bus, but communication can only occur in one direction at a time. While I²C is designed to allow multiple master devices, on the Raspberry Pi the Pi acts as the master and all devices connected to the bus are slaves.
The I²C interface
The I²C bus uses two wires called SDA and SCL. The SDA (serial data) wire carries the data packets sent by the master and slave devices. The SCL (serial clock) wire carries a clock signal generated by the master and is used to synchronise the data packets on the SDA wire between the master and slaves.
SDA and SCL are connected to the +Vdd power wire via a pair of pull-up resistors. This means that the default state of the I²C bus is pulled high to the +Vdd voltage. Typical +Vdd voltages are 3.3V and 5V, although other voltages may be used.
Data is sent over the I²C bus by pulling the SDA wire low to 0V. The clock signals are generated by the master and slave devices send a data packet in response to a request from the master.
As the I²C bus uses the +Vdd voltage of the master device, all slave devices also typically work at the same voltage. If a slave device needs to work at a different voltage from the master, then a I²C level shifter can be used to act as an interface between the different bus voltages.
Each slave device on the I²C bus must have a unique address. The I²C protocol allows the uses a 7-bit or 10-bit address system, although 10-bit addressing is rarely used. 7-bit addressing gives an allowable address range of 0x03 to 0x77. The master device will send the address at the start of any communication and only the slave device on that address will respond.
If a circuit requires the use of several slave devices that share the same I²C address an I²C switch may be used which splits an I²C bus into several separate busses with each slave connected to its own sub-bus. Before communicating with one of slaves the switch can be set to channel with the required slave and only that slave will then be visible to the master.
Some I²C devices include an All-Call address which can be used to send data to several slave devices simultaneously. While those devices will receive data on the All-Call address each device will only send data back to the master on its own unique address.
The graphic above shows a typical I²C data packet with a 7-bit address.
Each byte in an I²C packet is 8 bits in length followed by a 9th acknowledge bit generated by the receiving slave device by pulling the SDA wire low for one clock cycle. An acknowledge bit is typically shown in I2C software as ACK. If the byte is not acknowledged by a slave, the acknowledge bit is shown as a NACK.
At the beginning and end of the data packet are the start and stop conditions. The start and stop conditions are always generated by the master.
- Start: High to Low transition on SDA while SCL is high.
- Stop: Low to High transition on SDA while SCL is high.
After the start condition an address byte is sent by the master.
The address byte consists of 7 address bits followed by a Read/Write bit. The Read/Write bit is set as 1 when reading data from the slave and 0 when writing data to a slave.
In a 10-bit address packet the address is contained within the first two bytes. The first address byte consists of four 1s, one 0, two address bits and the Read/Write bit. The second address byte consists of the eight remaining bits.
After the address has been sent by the master the data bytes will be sent or received. The clock cycles on the SCL wire are generated by the master so in a read condition the slave will wait for each clock cycle before sending its data by pulling the SDA wire high or low.
I²C initially supported a clock speed of 100 Kbit/s. Over time the clock speed was increased to support new devices and today compliant hardware supports the following clock speeds:
- 100 Kbit/s – Standard Mode
- 400 Kbit/s – Full Speed
- 1 Mbit/s – Fast Mode
- 3.2 Mbit/s – High Speed
Not all devices support speeds above 100 Kbit/s so check with the datasheet of the device to find the supported clock speeds.
If a slave device requires more time to perform another function before sending a response it can pull the SCL wire low, forcing the master to wait until it is ready. This action is known as clock stretching.
You can find more information on how I²C works in the I²C Manual from Phillips/NXP.
Part 2: Enabling I²C on the Raspberry Pi
Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view, download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.