14.3.2 Use I2C sensor aware environment ( Pressure, Temperature and Altitude with the BMP180 )

In this section we use GPIO to build an application that measures altitude and temperature. Use the I2C interface of RPi-Spark to connect the BMP180 air pressure sensor module and then read the altitude and temperature data (requires a little soldering technique, refer to the method described in the previous chapter 14.3.1).

The BMP180 module uses the I2C (pronounced 'eye two see' or 'eye squared see') communication protocol and the Raspberry Pi communication. The I2C protocol uses only two pins for data communication, one pin for data ( SDA ) and other for clock signal ( SCL ) ( for more detailed I2C information please visit: I2C ).

Connect the RPi-Spark and BMP180 modules together as shown below.

I2C BMP180

First we need to make sure that the Raspberry Pi configured to enabled the I2C bus ( configuration Raspberry Pi ). Next we check if the system can discover the BMP180 module.

$> sudo i2cdetect -y 1

If the command is executed successfully, you should be able to see the following information:

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: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- 77                         

There 0x68 is the attitude sensor channel of RPi-Spark. 0x77 is the channel of the BMP180 module, so we can found BMP180 module, next we need to install python driver of BMP180 module:

$> git clone https://github.com/adafruit/Adafruit_Python_BMP
$> cd Adafruit_Python_BMP
$> sudo python setup.py install

We can use the BMP180 module when then driver has installed done.

Create a file named: 14_3_1_bmp180.py, and type the content build the temperature, altitude and air pressure application:

from JMRPiFoundations.Skeleton.RPiSparkModule import RPiSparkModule
import Adafruit_BMP.BMP085 as BMP085 # Imports the BMP library

class BMP180(RPiSparkModule):
    BMPSensor = None

    def setup(self):
        # Create an 'object' containing the BMP180 data
        self.BMPSensor = BMP085.BMP085()

    def run(self):
        self.RPiSpark.Screen.write('BMP-180 sensor', xy=(0,4), screenCenter=True)
        dataTxt = "Temp: {0:0.2f} C\nBaro: {1:0.2f} Pa\n Alt: {2:0.2f} m".format(
            self.BMPSensor.read_temperature(),
            self.BMPSensor.read_pressure(),
            self.BMPSensor.read_altitude())

        self.RPiSpark.Screen.write(dataTxt, xy=(12,26), spacing=1)

        # Draw a 2 pixels width line
        self.RPiSpark.Screen.Canvas.line( (12, 20, 116, 20), 1, 3 )        
        self.RPiSpark.Screen.refresh()

Save the file and enter the command:

$> rspk 14_3_1_bmp180 -f

At this time, you should able to see the current temperature, altitude and barometric pressure data on the OLED screen:

Pressure, Temperature and Altitude with the BMP180


 

Was this article helpful?

YES     |     NO

 

Enjoying the project? Spotted a mistake? Any opinions on the website? Let us know!