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.
Activate the I2C bus from the preferences menu of Raspberry Pi OS (former Raspbian)
If you have chosen to install Raspbian Buster with a graphical interface, open the menu Preferences
then launch Pi Configuration
.
The I2C bus is activated by clicking on Enable. Click OK to save the change. Restart Raspberry Pi OS to activate the I2C bus.
Activate the i2c bus with the raspi-config tool
For projects that do not require a graphical interface (robotics projects based on Raspberry Pi Zero for example), the I2C bus is managed using the raspi-config utility on Raspbian.
Log in as SSH read this tutorial to learn how to do it or open a Terminal and run the Raspberry Pi OS configuration tool.
sudo raspi-config
Choose option (5) Interfacing Options
Then in the list the option (P5) I2C. Activate by selecting YES. No to disable.
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.
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).
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.
Plug and test an i2c device with Raspberry Py
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.
- Waveshare launches two Raspberry Pi 400 + 7″ or 13.3″ touchscreen bundles
- Motorized microscope with HQ camera for Raspberry Pi and HTML interface (Python)
- Open an HTML page when starting Raspberry Pi OS with Chromium Browser in full screen (kiosk mode)
- Pi Imager and the configuration wizard simplify the installation of Raspberry Pi OS (old Raspbian)
- New Raspberry Pi 3 Model B+ 2018 @1.4GHz, Gigabit Ethernet, Ethernet POE Daughter Card, Flash Sale at $49.99 on Gearbest
- #Test LCD 3.5” HDMI touch screen (via GPIO) with acrylic case for Raspberry Pi 3 under Raspbian (480×320 to 1920×1080 pixels)