I2C - Temperature and Pressure Sensor
This example will use the Raspberry Pi's I2C interface to read the current temperature and barometric pressure from the BMP180 sensor
What is I2C?
I2C (eye-squared-cee) is a communication protocol that the Raspberry Pi can use to speak to other embedded devices (temperature sensors, displays, accelerometers, etc). In this example, we'll be connecting a BMP180 Temperature/Pressure Sensor to our Raspberry Pi.
I2C is a two wire bus, the connections are called SDA (Serial Data) and SCL (Serial Clock). Each I2C bus has one or more masters (the Raspberry Pi) and one or more slave devices, like the I/O Expander. As the same data and clock lines are shared between multiple slaves, we need some way to choose which device to communicate with. With I2C, every device has an address that each communication must be prefaced with. The temperature sensor defaults to an address of 0x77. Remember this number as we'll see it shortly when we try to detect the chip using
The Rasperry Pi has two I2C buses. One is available on the GPIO (P1) header, the other is only available from the P5 header. To access the P5 header, you'll need to solder on your own header pins. This is generally unnecessary as you typically only need a single I2C bus.
Connecting the BMP180
Preparing RPi for I2C
Activate I2C Drivers
On your Raspberry Pi, you must first enable the I2C drivers.
2. At the menu, choose option
8. Advanced Options
A7 I2C and then say "Yes" to enable the I2C driver and "Yes" again to load the driver by default
4. Reboot your Raspberry Pi by running
sudo reboot back at the command line
Add i2c-dev to /etc/modules
Then from the prompt run:
Confirm that the i2c modules are loaded and active:
Install some i2c utilities:
i2c-tools includes some cool utilities, like
i2cdetect, which will enumerate the addresses of all slave devices on a single bus. Try it out by running
sudo i2cdetect -y 1 with the sensor connected. Another utility,
i2cdump lets you query the state of individual settings (registers) on a specific I2C device. This is the output you should see with the sensor connected as below, and configured for the default address of 0x77:
Reading values from the BMP180
First we'll need to install some utilities for the Raspberry Pi to communicate over I2C inside of Python.
Next, place the following into a Python script and run it.
- Q. When I run the script, I get the following error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/lib/python2.7/dist-packages/Adafruit_BMP-1.0.0-py2.7.egg/Adafruit_BMP/BMP085.py", line 66, in __init__ self._device = I2C.Device(address, busnum) File "/usr/local/lib/python2.7/dist-packages/Adafruit_GPIO-0.5.5-py2.7.egg/Adafruit_GPIO/I2C.py", line 68, in __init__ self._bus = smbus.SMBus(busnum) IOError: [Errno 2] No such file or directory
- A: Ensure you have loaded the i2c_bcm2708 and i2c-dev modules using
lsmod. If they are not present, follow the instructions at the top of the page to modify the files in