Home / Tutorials / Raspberry Pi Tutorial / How to Use I2C Devices with Raspberry Pi
Raspberry Pi I2C pins

How to Use I2C Devices with Raspberry Pi

I2C is a popular device protocol in communicating with microcontrollers. However, this protocol is not limited to Arduino, PIC and STM32; the Raspberry Pi can also use I2C.

If you are a beginner in the protocol, I suggest you read my primer on I2C.

I am using Raspberry 4 Model B in this tutorial running Raspian 10 Buster. Some parts of this tutorial may not apply to your version of Raspberry Pi. To know your Raspbian version, run this on the shell:

Enabling I2C

The first thing to do is to enable I2C for the RPi as it’s disabled by default. To do this, run raspi-config on shell:

Select 5 Interfacing Options:

Enabling Raspberry Pi I2C - 1

Then select P5 I2C:

Enabling Raspberry Pi I2C - 2

Select Yes:

Enabling Raspberry Pi I2C - 3

If this shows then I2C is now usable in your Raspberry Pi:

Raspberry Pi I2C is now enabled

If you’re using the Raspberry Pi in Desktop mode or VNC, you can enable I2C this way:

Click the Raspberry Pi icon to reveal the menu. Under Preferences, select Raspberry Pi Configuration:

Raspi config on desktop

The Raspberry Pi Configuration window appears. Click the I2C Enabled radio button and click OK.

Enabling I2C via desktop

That should enable the I2C for your Raspberry Pi.

I recommend rebooting your Pi after enabling I2C in any of these methods.

or if in Desktop:

Raspberry Pi reboot 1

Raspberry Pi Reboot 2

Raspberry Pi I2C Pins

The I2C pins SDA and SCL are in pins 3 and 5 of the Raspberry Pi’s header:

Raspberry Pi I2C pins

This is I2C port 1 or simply I2C-1. There is another set of I2C pins (I2C-0) at 27 and 28 but they are reserved for communicating with EEPROMs found in Raspberry Pi HATs.

Note that these pins already have a 1.8 kilo-ohm pull-up resistors in them.

I2C Programming Using Python

The main library for I2C programming with Python is SMBUS. However prior to programming, you must check if the device is recognized by the Raspberry Pi. One thing you can do is use:

This will install tools needed to access the I2C bus via shell. One of those tools is:

This will show you a list of connected devices and their address. This is an example output when an MPU6050 attaches to the Raspberry Pi’s I2C bus:

SMBUS already comes with the latest Python and Raspbian. But just in case, you can install SMBUS using command:

To use SMBUS, import and it create an object

The parameter (1) refers to the bus number.

You can then write a byte of data:

Here, DEVICE_ADDRESS is the 7-bit I2C address of the device, DEVICE_REG is the address of the specific register inside the device you want to write to and DEVICE_CMD is the data you want to write to that register.

Conversely, to read a byte, use:

You can also write data without specifying which register using:

Similarly, to read data without specifying the register, use:

I will be building a project where a Raspberry Pi communicates via I2C to a device so I hope you come back for that. Thanks for reading!

Check Also

Send SMS with Raspberry Pi

How to Send SMS with Raspberry Pi

It’s 2020 and sending SMS is now an old technology. But such technology remains to …

Leave a Reply

Your email address will not be published.