Raspberry Pi

How to configure I2C on Raspberry Pi

Inter Integrated Circuit (I2C) is a communication protocol mostly used in micro controller-based systems. It is particularly used for interfacing the device with sensors or other components. It allows one chip to talk with another chip synchronously. The Raspberry Pi has built-in I2C protocol providing you the opportunity to talk with other I2C circuits. However, to ensure a smooth working of the I2C feature, you will have to configure it on your Raspberry Pi device.

This tutorial will guide you in configuring I2C on Raspberry Pi devices.

Configuring I2C on Raspberry Pi

By default, the I2C Kernel Module feature is disabled on your Raspberry Pi device and you will have to enable it first in order to configure it. You will also need to install I2C prerequisites on your Raspberry Pi device to enable the I2C communication. Follow the below step-by-step instruction to configure I2C on your Raspberry Pi device:

Step 1: Update Raspberry Pi packages

First, update the Raspberry Pi packages using the following command:

$ sudo apt update && sudo apt upgrade -y

Step 2: Install Prerequisites

There are two perquisites required for I2C communication. The one is an I2C tool that enables the I2C protocol service. The other one is the python-smbus package that allows you to interact with other I2C devices using Python.

By default, these perquisites are already installed on your Raspberry Pi device. If you remove them by mistake, you can apply the following command to reinstall it:

$ sudo apt install -y i2c-tools python3-smbus

Step 3: Enable I2C

After completing the above steps, you will need to enable the I2C Kernel module on your device from the “raspi-config” command as shown below:

$ sudo raspi-config

Select the “Interface Options”.

Select the “I2C” option.

Choose the “Yes” option to enable it.

You will get the confirmation that your I2C interface is enabled.

Step 4: Reboot Raspberry Pi

Now, reboot your device to apply the changes.

Testing I2C Connection on Raspberry Pi

Now that I2C is enabled on your Raspberry Pi device, it’s no time to test the connection. Attach any I2C device with your Raspberry Pi and then issue the following command:

$ sudo i2cdetect -y 1

The above output confirms that your I2C device is successfully detected on Raspberry Pi and is using the address 0x20. These values can be different in your case because of different I2C devices. If you won’t be able to find any address like the one shown below then you may have a problem with your device.

If you are using the old model of Raspberry Pi, replace “1” with “0” in the above command because the previous model uses Port 0 for I2C communication.

Conclusion

I2C communication is extremely important for the tech users as it allows them to directly talk with I2C circuit boards. On Raspberry Pi, its setup is pretty simple, which only requires enabling the I2C interface from the device configuration mode. After the setup, you will be able to perform communication by connecting various I2C devices with your Raspberry Pi.

About the author

Awais Khan

I'm an Engineer and an academic researcher by profession. My interest for Raspberry Pi, embedded systems and blogging has brought me here to share my knowledge with others.