Professional Documents
Culture Documents
Introduction
I2C (Inter Integrated Circuit) is a synchronous serial protocol that communicates data
between two devices.
It is a master-slave protocol which may have one master or many master and many slaves
whereas SPI has only one master.
It is generally used for communication over short distance.
The I2C device has 7-bit or 10-bit unique address. So, to access these devices, master
should address them by the 7-bit or 10-bit unique address.
I2C is used in many applications like reading RTC (Real time clock), accessing external
EEPROM memory. It is also used in sensor modules like gyro, magnetometer etc.
It is also called as Two Wire Interface (TWI) protocol.
Raspberry Pi I2C
Raspberry Pi has Broadcom processor having Broadcom Serial Controller (BSC) which is a
master, fast-mode (400Kb/s) BSC controller. The BSC bus is compliant with the Philips I2C
bus.
It supports both 7-bit and 10-bit addressing.
It also has BSC2 master which is dedicatedly used with HDMI interface and should not be
accessed by user.
I2C bus/interface is used to communicate with the external devices like RTC, MPU6050,
Magnetometer, etc with only 2 lines. We can connect more devices using I2C interface if
their addresses are different.
To access I2C bus in Raspberry Pi, we should make some extra configuration. Raspberry Pi has
I2C pins which are given as follows.
First, we should enable I2C in Raspberry Pi. We can enable it through terminal which is given
below:
sudo raspi-config
Select Interfacing Configurations
Now, after booting raspberry Pi, we can check user-mode I2C port by entering following
command.
ls /dev/*i2c*
Now connect any I2C based device to the user-mode port and scan that port using following
command,
sudo i2cdetect -y 1
e.g.
Here, we connected MPU6050 I2C based device to the Raspberry Pi and try to detect that device
which is shown in below image,
i2cdetect command will scan the I2C port to get the address of device if connected.
If no device is connected to I2C port then it will return field with (- -).
We can also get or set data of I2C device using following command
e.g.
sudo i2cget -y I2C_user_mode_Port address_of_device Register_addresssudo i2cget 1 0x68
0x01
and it will respond with data present in the register having address 0x01.
While developing program for Raspberry Pi I2C communication in python, use SMBus library
package which has great support to access I2C devices. So, we should add SMBus support for
Python by using apt packet manager,
sudo apt-get install python-smbus