The I2C bus is a widely used device communication protocol, allowing the comunitaction between the devices sharing the same bus channel. This is a great advantage because it is designed to allow your Raspberry Pi may be connected to a multitude of complex devices on the same bus and through I2C can communicate independently with each element.
In this tutorial we will learn how to use the Raspberry Pi I2C bus and we will implement a pressure display using a I2C BMP180 barometric sensor plus a I2C 7 segments display to create a digital barometer.
NOTE: thanks to Adafruit because a lot of information and the device drivers come from their great website!!
First of all we need to setup the I2C on our Raspberry Pi:
To enable the I2C bus in our Raspberry Pi, the first thing we must do is install theI2C tools packages:
sudo apt-get install python-smbus
sudo apt-get install i2c-tools
Execute the sudo raspi-config command and go to Advanced Options , activate the I2C bus interface and kernel module and reboot the Raspberry Pi.
Run the command sudo nano /boot/config.txt and add or uncomment the following lines:
dtparam = i2c1 = on
dtparam = i2c_arm = on
Edit the /etc/modules file and add next lines:
After making these changes, reboot again and our Raspberry Pi.
When the Raspberry Pi start, execute the command sudo i2cdetect -y 1 or sudo i2cdetect -y 0 (depending on Raspberry Pi models) to check that everything is working properly.
The barometric sensor has 4 wires: ground, 3.3V, SC and SD. Connect to GND of our Raspberry Pi to the ground pin of the barometric sensor, the 3.3V to 3.3V pin, the pin I2C_SCL to SC and SD to I2C_SDA pin.
After connecting the sensor, run sudo i2cdetect -and 1 to verify if the Raspberry Pi recognizes the sensor
Execute the command sudo apt-get install build-essential git python-dev python-smbus to ensure we have all the necessary packages to compile the library.
Clone the Adafruit repo for barometric sensor BMP085 / BMP180 with: git clone https://github.com/adafruit/Adafruit_Python_BMP.git
Go to the Adafruit_Python_BMP directory and install the library using the command sudo python setup.py install.
To check if the sensor and the software is work correctly, run the sample script simpletest.py
Now, it’s the turn of the7-segments display. Like the barometric sensor it has 4 wires: ground, 3.3V, SC and SD. Connect to GND of our Raspberry Pi to the ground pin of the display, the 3.3V to 3.3V pin, the pin I2C_SCL to SC and SD to I2C_SDA pin.
We need to clone the Adafruit 7-segment display repository with : git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
Access to the directory Adafruit_LEDBackpack inside Adafruit-Raspberry-Pi-Python-Code and try to run the sample codes to test the library.
Putting all together
On the hardware side, you must attach the barometric sensor and the 7 segments display to the Raspberry Pi I2C bus all together. To avoid a protoboard ‘cable mess’ you can build a very simple I2C bus hub connecting every line (2 data / GND and 3.3V) in parallel as picture shows.
Now, It’s all setup, so we can run the code: