The i2c bus is used to exchange digital data between a sensor (or an actuator) and a mini-PC such as Raspberry Pi. The i2c bus is also widely used on microcontrollers (Arduino, ESP8266, ESP32). By default, the i2c bus is not enabled on the Raspbian distribution. We will activate it and install the necessary tools.

This tutorial works on all versions of Raspbian and all Raspberry Pi models (including the new Pi Zero W).

Activate the i2c bus with the raspi-config tool

Log in as SSH (read this tutorial to learn how to do it) or open a Terminal and run the Raspbian configuration tool.

sudo raspi-config

Choose option (5) Interfacing Options

 raspberry pi i2c raspi-config

Then in the list the option (P5) I2C. Activate by selecting YES. No to disable.

raspberry pi i2c activate raspi-config

It is necessary to restart the Raspberry Pi to take the change into account.

sudo reboot

Installing i2c Tools

First, make an update to the system.

Please note that the operation may take a long time
sudo apt update 
sudo apt upgrade -y

Once the system is up-to-date, start the installation with the tools to communicate with i2c devices in Python (python-smbus) and diagnostic tools (i2c-tools).

sudo apt-get install -y python-smbus i2c-tools

Once the installation is complete, you can already test whether the modules are fully loaded

pi@raspberrypi:~ $ lsmod | grep i2c_
i2c_bcm2835             6433  0 
i2c_dev                 6642  0

You need the module i2c_bcm2708 (or i2c_bcm2835 on the Raspberry Pi Zero W), is that the modules were loaded correctly.

I2c pins on the Raspberry GPIO

The i2c pins are located on pins 3 (SDA) and 5 (SCL).

Raspberry pi reperage broches Pinout

If you are starting with the Raspberry Pi, I recommend that you obtain a breadboard adapter on which the pins are already located. Pin 1 of the GPIO is symbolized by a square.

adaptateur wedge gpio raspberry pi

Plug and test an i2c device with Raspberry Py

To prevent deterioration of your Raspberry Pi, it is essential to turn off the system with the sudo halt  control and disconnect the power supply.

To illustrate this tutorial, I have BH1750 brightness sensor (GY-302 from CJMCU) presented in this article. Any other i2c sensor will do the trick. By default, the BH1750 has the address 0x23.

Check the device address with i2cdetect

The i2cdetect command lets you know the device address on the i2c bus. The i2c bus connects up to 255 devices. Depending on the version of the Raspberry Pi, the control is different. For Pi 3 and Pi 2 (models A, B, B +) and Pi Zero, run

sudo i2cdetect -y 1

For older models, run

sudo i2cdetect -y 0

If an error occurs, you will get the following message

Error: Could not open file `/dev/i2c-0' or `/dev/i2c/0': No such file or directory

Here’s what you should get. The BH1750 is at the expected address.

pi@raspberrypi:~ $ sudo i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- 23 -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --

So, everything is ready to use i2c sensors in your python, C ++ or Node-RED programs.

Subscribe to the weekly newsletter

No spam and no other use will be made of your email. You can unsubscribe anytime.

DIY Projects