About Us Knowledge Base Community Forum Electronics Toolbox FAQ Shipping & Delivery

Knowledge Base

The AB Electronics UK Knowledge Base provides support solutions, tutorials and troubleshooting guides.

Knowledge Base

I2C, SMBus and Raspbian Linux

Find out how to set up I2C communication on the Raspberry Pi
Created: 02/10/2015 | Last Updated: 16/10/2019

This tutorial explains how to install and configure I2C on the Raspbian Buster and later distributions of Linux for the Raspberry Pi from http://www.raspberrypi.org/downloads.  For earlier versions of Rasbian Linux please use our tutorials for Raspbian Jessie and Stretch and Raspbian Wheezy.

I2C, pronounced I-squared-C or I-2-C, is a popular serial bus used by a wide range of manufacturers for low speed serial communication.  

I2C is designed to be a multi-slave, half-duplex bus meaning you can have a master device and multiple slave devices connected to the same bus, but communication can only occur in one direction at a time.  While I2C 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 I2C port on the Raspberry Pi uses the pins GPIO2 (SDA) and GPIO3 (SCL) as shown below.

I2C Pins on the Raspberry Pi

Enabling I2C on Raspbian Linux

Step 1: Download the latest Raspbian linux image and burn to your SD Card following the instructions on http://elinux.org/RPi_Easy_SD_Card_Setup

If you are using Raspian Linux 3.18 or later you need to go into the raspberry pi config utility and enable I2C.

sudo raspi-config

Select 5 Interfacing Options and then  P5 I2C. A prompt will appear asking Would you like the ARM I2C interface to be enabled?, select Yes, exit the utility and reboot your raspberry pi.

sudo reboot

When your Raspberry Pi has rebooted you will now have I2C and SMBus Python 2 or 3 support.

Installing I2C Tools and Python Libraries

If you are using the Raspbian Lite version of Raspbian Linux you may need to install the i2ctools package as well as several python libraries.  You can do this using the following commands.

sudo apt-get update
sudo apt-get install python-smbus python3-smbus python-dev python3-dev i2c-tools

You can test if i2ctools is working by listing all of the I2C devices connected to your Raspberry Pi.

sudo i2cdetect -y 1

i2cdetect will display a grid of numbers with the addresses of any i2c devices shown within the grid.  In the following example two I2C devices are show on addresses 0x68 and 0x69.

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- 68 69 -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Setting the I2C Bus Speed

The I2C bus supports several bus speeds, typically 100KHz, 400KHz, 1MHz, 3.4MHz and 5.0MHz.  You can set the bus speed by editing /boot/config.txt.  Open the file with nano using the command:

sudo nano /boot/config.txt

Add the following text to the bottom of the file; The number is the frequency of the I2C bus in hertz:


Save your changes, exit the nano editor and reboot

sudo reboot

Notes for the Raspberry Pi 3B, 3B+ and Zero W

The I2C controller on the Raspberry Pi 3B, 3B+ and Zero W has its clock linked to the VPU core so as the VPU frequency changes depending on processor load so does the clock rate of the I2C bus.  This can cause problems with any devices that expect a constant clock rate during communication.

To solve this issue the VPU core frequency must be set to a fixed frequency by editing /boot/config.txt.  Open the file with nano using the command:

sudo nano /boot/config.txt

Add the following text to the bottom of the file.


Save your changes, exit the nano editor and reboot

sudo reboot

Choosing the correct I2C bus speed

I2C devices are designed with a maximum speed for the I2C bus.  This speed varies from device to device and can normally be found in the manufacturer’s datasheet for that device.  Devices will run at speeds below the maximum but not faster so when choosing the correct bus speed for your Raspberry Pi you will need to find the maximum speed for each device and then set the I2C bus to be the speed of the slowest device.

Other factors can also limit the speed of the I2C bus including the number of devices connected to the bus, cable length and cable capacitance so you may find that if I2C communication is not working correctly you will have to set the clock frequency to a slower speed.

The maximum recommended speed for the expansion boards made by AB Electronics UK is listed in the table below.

Expansion Board Maximum Clock Frequency
1 Wire Pi Plus 400 KHz (400000)
1 Wire Pi Zero 400 KHz (400000)
ADC Pi 1 MHz (1000000)
ADC Differential Pi 1 MHz (1000000)
Expander Pi 100 KHz (100000)
IO Pi Plus 1 MHz (1000000)
IO Pi Zero 1 MHz (1000000)
RTC Pi 100 KHz (100000)
Servo PWM Pi 1 MHz (1000000)

I2C Device Addresses

Before the Raspberry Pi can communicate with an I2C device it needs to know the address assigned to that device.  I2C devices typically use a 7-bit address which is sent at the start of each communication from the master to the slave device.  7 bits gives a total of 128 possible device addresses.

Some devices will have their address pre-set by the manufacturer while others will have a small range of addresses that can be selected by the user.  Each device on the bus must have a unique address, if two devices share the same address then there can be a conflict that stops the devices from functioning correctly.

To find a list of the devices connected to the I2C bus on the Raspberry Pi you can use the following command:

sudo i2cdetect -y 1

On the original Raspberry Pi with the 26 pin GPIO header you may need to change the “-y 1” to “-y 0” for the command to work.

I2cdetect returns a grid of possible addresses with each device displayed as shown in the screenshot below.  

i2c detect screen output

If no devices are detected check that you have followed all the steps at the start of this tutorial for enabling the I2C bus and the devices connected to the Raspberry Pi are powered and do not share the same I2C address. 

Was this article helpful? Yes / No