Knowledge Base

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

I2C, SMBus and Raspbian Linux

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

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

This page details how to setup I2C support on the Raspbian Jessie or Raspbian Stretch image from http://www.raspberrypi.org/downloads

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

Step 2: Next you need to update your Raspberry Pi to ensure all the latest packages are installed:

sudo apt-get update
sudo apt-get upgrade

Step 3:  Once you have logged into your Raspberry Pi from the command line, run the following command to check that the nessesary software is installed:

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

Step 4:

For recent versions of the Raspberry Pi (3.18 kernel or later) you will need to update the /boot/config.txt file.  Open the file with nano using the command:

sudo nano /boot/config.txt

Add the following text to the bottom of the file:

dtparam=i2c1=on

Save your changes and exit the nano editor.

All versions:

Step 5: To avoid having to run the I2C tools at root you will need to add the ‘pi’ user to the I2C group:

sudo adduser pi i2c

Step 6: Next reboot the Raspberry Pi:

sudo reboot

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

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:

dtparam=i2c_baudrate=400000

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.

core_freq=250

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