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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-184983-1.png" data-lazy- height="188" src="data:image/svg xml,” width=”809″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-184983-2.png" data-lazy- height="399" src="data:image/svg xml,” width=”807″>

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:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-184983-3.png" data-lazy- height="381" src="data:image/svg xml,” width=”811″>

Select the “Interface Options”.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-184983-4.png" data-lazy- height="366" src="data:image/svg xml,” width=”805″>

Select the “I2C” option.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-184983-5.png" data-lazy- height="364" src="data:image/svg xml,” width=”804″>

Choose the “Yes” option to enable it.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-184983-6.png" data-lazy- height="408" src="data:image/svg xml,” width=”603″>

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

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-184983-7.png" data-lazy- height="402" src="data:image/svg xml,” width=”600″>

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:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-184983-8.png" data-lazy- height="226" src="data:image/svg xml,” width=”809″>

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.

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/word-image-184983-9.png" data-lazy- height="204" src="data:image/svg xml,” width=”807″>

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

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/06/echo/unnamesadadsasdd-150×150.jpg62a7e9d1b25a3.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.