Home | Difficulty level | Beginner | Activate the i2c bus of Raspberry Pi 3 or Pi Zero W

Activate the i2c bus of Raspberry Pi 3 or Pi Zero W

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.

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.

Installing i2c Tools

First, make an update to the system.

Please note that the operation may take a long time
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).
Once the installation is complete, you can already test whether the modules are fully loaded
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

For older models, run

If an error occurs, you will get the following message

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

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