BMP180 I2C Digital Barometric Pressure Sensor

The BMP180 device is a digital barometric pressure sensor. This is available on a small module which provides access to the sensor via the I2C interface. This allows us to easily connect it to the Raspberry Pi with a minimum of wiring.

My module is a small pcb measuring 15x13mm with a 5 pin header. The order of the pins may vary on other modules so keep an eye on the labels so you connect up the correct wires from the Pi.

The BMP180 is made by Bosch and the official BMP180 datasheet includes all the technical details.

Configure I2C Interface

In order to use this module you must enable the I2C interface on the Raspberry Pi as it is not enabled by default. This is a fairly easy process and is described in my Enabling The I2C Interface On The Raspberry Pi tutorial.BMP180 I2C Digital Barometric Pressure Sensor

Connecting Hardware

The table below shows how the module is connected to the Raspberyr Pi’s GPIO header (P1). Please refer to my GPIO header guide for a diagram.

Module PCB Desc GPIO Header Pins
VCC 3.3V P1-01
GND Ground P1-06
SCL I2C SCL P1-05
SDA I2C SDA P1-03
3.3V

Here is a diagram of a breadboard setup. If you are connecting the module’s four pins directly to the Pi you only need four female-female wires.

The breadboard diagram uses a custom part I defined in Fritzing. Other modules are available which have different pin arrangements so make sure you are connecting the correct pins to the Pi if yours is different to the one shown in this tutorial.

With the device connected and the Pi powered up the “i2cdetect” command should show the device with address 0x77.

Example Python Script

Here is an example Python script to read pressure and temperature data from the sensor :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/python
import smbus
import time
from ctypes import c_short
 
DEVICE = 0x77 # Default device I2C address
 
#bus = smbus.SMBus(0)  # Rev 1 Pi uses 0
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
 
def convertToString(data):
  # Simple function to convert binary data into
  # a string
  return str((data[1] + (256 * data[0])) / 1.2)
def getShort(data, index):
  # return two bytes from data as a signed 16-bit value
  return c_short((data[index]<< 8) + data[index + 1]).value
def getUshort(data, index):
  # return two bytes from data as an unsigned 16-bit value
  return (data[index]<< 8) + data[index + 1]def readBmp180Id(addr=DEVICE):
  # Register Address
  REG_ID     = 0xD0
  (chip_id, chip_version) = bus.read_i2c_block_data(addr, REG_ID, 2)
  return (chip_id, chip_version)
  
def readBmp180(addr=DEVICE):
  # Register Addresses
  REG_CALIB  = 0xAA
  REG_MEAS   = 0xF4
  REG_MSB    = 0xF6
  REG_LSB    = 0xF7
  # Control Register Address
  CRV_TEMP   = 0x2E
  CRV_PRES   = 0x34
  # Oversample setting
  OVERSAMPLE = 3    # 0 - 3
  
  # Read calibration data
  # Read calibration data from EEPROM
  cal = bus.read_i2c_block_data(addr, REG_CALIB, 22)
  # Convert byte data to word values
  AC1 = getShort(cal, 0)
  AC2 = getShort(cal, 2)
  AC3 = getShort(cal, 4)
  AC4 = getUshort(cal, 6)
  AC5 = getUshort(cal, 8)
  AC6 = getUshort(cal, 10)
  B1  = getShort(cal, 12)
  B2  = getShort(cal, 14)
  MB  = getShort(cal, 16)
  MC  = getShort(cal, 18)
  MD  = getShort(cal, 20)
  # Read temperature
  bus.write_byte_data(addr, REG_MEAS, CRV_TEMP)
  time.sleep(0.005)
  (msb, lsb) = bus.read_i2c_block_data(addr, REG_MSB, 2)
  UT = (msb << 8) + lsb
  # Read pressure
  bus.write_byte_data(addr, REG_MEAS, CRV_PRES + (OVERSAMPLE << 6))
  time.sleep(0.04)
  (msb, lsb, xsb) = bus.read_i2c_block_data(addr, REG_MSB, 3)
  UP = ((msb << 16) + (lsb << 8) + xsb) >> (8 - OVERSAMPLE)
  # Refine temperature
  X1 = ((UT - AC6) * AC5) >> 15
  X2 = (MC << 11) / (X1 + MD)
  B5 = X1 + X2
  temperature = (B5 + 8) >> 4
  # Refine pressure
  B6  = B5 - 4000
  B62 = B6 * B6 >> 12
  X1  = (B2 * B62) >> 11
  X2  = AC2 * B6 >> 11
  X3  = X1 + X2
  B3  = (((AC1 * 4 + X3) << OVERSAMPLE) + 2) >> 2
BMP180 I2C Digital Barometric Pressure Sensor schematic
  X1 = AC3 * B6 >> 13
  X2 = (B1 * B62) >> 16
  X3 = ((X1 + X2) + 2) >> 2
  B4 = (AC4 * (X3 + 32768)) >> 15
  B7 = (UP - B3) * (50000 >> OVERSAMPLE)
  P = (B7 * 2) / B4
  X1 = (P >> 8) * (P >> 8)
  X1 = (X1 * 3038) >> 16
  X2 = (-7357 * P) >> 16
  pressure = P + ((X1 + X2 + 3791) >> 4)
  return (temperature/10.0,pressure/ 100.0)
def main():
    
  (chip_id, chip_version) = readBmp180Id()
  print "Chip ID     :", chip_id
  print "Version     :", chip_version
  print
  
  (temperature,pressure)=readBmp180()
  print "Temperature : ", temperature, "C"
  print "Pressure    : ", pressure, "mbar"
  
if __name__=="__main__":
   main()

It is recommended that you download this script directly to your Pi using the following command :

wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/bmp180.py

or use this link in a browser.

In order to run it you can use the following command :

 

For more detail: BMP180 I2C Digital Barometric Pressure Sensor


About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:
LinkedinTwitter

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top