In this example we are going to connect to BMP180 barometer sensor, we will be using brake-out board called GY-68.
This sensor is really small even the full brake-outboard is only size of a fingernail. The board does not come with the header pins soldered on, but with mine the header pins came with it at least so I just had to solder them on.
Disclaimer:
We do not take any responsibility for possible errors in the guide or errors that you might do wiring it up. Incorrect wiring can result in damaged sensor or damaged Raspberry PI.
Pieces we use in this example:
- Raspberry PI 2 (or Raspberry PI 3)
- Cobbler and Cobbler cable
- Bread board
- 15 kΩ resistor (x2).
- BMP180 sensor on GY68 brake-out board. (If you shop around then you can find this one for under $3 on Ali Express)
- WiringPI needs to be installed on the Raspberry PI -> See here.
- I2CTools needs to be installed on the Raspberry PI and I2C needs to be enabled in the Raspberry PI settings.
If you are not familiar with breadboards, how to place a cobbler on the breadboard or need to know how to read the color codings on the resistors then click here bellow
Using breadboard Resistor color codings
To install the I2CTools you use the following command:
sudo apt-get install i2c-tools
You need to enable the i2c bus in the Raspberry PI settings, which you can do in the user interface if your running Jessie or if your on older system then you can do it in console.
Wiring up the board:
First we solder the header pins into the brake-out board as shown bellow:
Once the board has been soldered with header pins or wires then we are ready to wire up the sensor which we do as follows:
The wiring is very simple as can be seen on the diagram above. We use the 15 kΩ to lower the voltage on the I2C bus.
GPIO 2 here above is a special GPIO pin that is also called SDA, you cannot choose any other pin for this.
GPIO 3 here above is a special pin that is also called SCL, you cannot choose any other pin for this.
Sanity tests in console:
After you have connected everything and made sure that wiringPI, i2ctools are installed that I2C is enabled in the Raspberry PI settings, then we are ready to do sanity tests in console.
First thing is getting the address of the I2C device, which is in this case the analog converter board.
To do that then we type:
sudo i2cdetect -y 1
This should show our device at 0x77.
If you can see your sensor at address 0x77 then you are ready to start coding.
The Xojo code:
Coding against this sensor is somewhat complicated but I have made it really simple as I created a Xojo module called BMP180 (see download link at bottom) that does all the hard work. Using the module is as easy as follows:
// Getting chip ID to verify all is ok:
Sub Action()
Dim handle as Integer
Dim value as Integer
handle = BMP180.Setup(&h77)
if handle = -1 then
MsgBox "Failed I2CSetup"
return
end if
if handle <> -1 then
// We expect 55 hex as the chio ID if everything is ok
MsgBox("0x" + Hex(BMP180.GetChipID(handle)))
end if
End Sub
// Read temperature:
Sub Action()
Dim handle as Integer
Dim value as Integer
handle = BMP180.Setup(&h77)
if handle = -1 then
MsgBox "Failed I2CSetup"
return
end if
if handle <> -1 then
MsgBox(Format(BMP180.ReadTemperature(handle),"#.0") + "° C")
end if
End Sub
// Read pressure:
Sub Action()
Dim handle as Integer
Dim value as Integer
handle = BMP180.Setup(&h77)
if handle = -1 then
MsgBox "Failed I2CSetup"
return
end if
if handle <> -1 then
MsgBox(Format(BMP180.ReadPressure(handle,BMP180.Mode.STANDARD),"#.0"))
end if
End Sub
// Read pressure ultra high res
Sub Action()
Dim handle as Integer
Dim value as Integer
handle = BMP180.Setup(&h77)
if handle = -1 then
MsgBox "Failed I2CSetup"
return
end if
if handle <> -1 then
MsgBox(Format(BMP180.ReadPressure(handle,BMP180.Mode.ULTRAHIGHRES),"#.0"))
end if
End Sub
// Read pressure at sea level:
Sub Action()
Dim handle as Integer
Dim value as Integer
Dim heightOverSeaLevel as Single = Val(tbHeightOverSeaLevel.Text)
handle = BMP180.Setup(&h77)
if handle = -1 then
MsgBox "Failed I2CSetup"
return
end if
if handle <> -1 then
MsgBox(Format(BMP180.ReadPressureAtSeaLevel(handle,BMP180.Mode.ULTRALOWPOWER,heightOverSeaLevel),"#.0"))
end if
End Sub
To get your current height above sea level then you can use this page here: GEOPosition.
When testing the example then I found I was 221 meter above sea level which I used to calculate pressure at sea level.
Downloading the code:
Note that a better way to hook up the barometer is to use I2C logic converter than to use the 15kΩ resistors, like for example:
You can see more about that in our Building I2C logic converter guide.
Thats it for now!