Authorized Online Retailers:

AMAZON
AMAZON-jp
amzuk

Introduction
In this less, we’ll teach you how to use Raspberry Pi read data from BMP180 digital pressure sensor.

Hardware Preparation
1 * Raspberry Pi 3
1 * BMP180 digital pressure sensor
1 * Breadboard
Several jumper wires
1 * T-Extension Board with 40-Pin Cable(Optional)

Software Preparation
Note: In this lesson, we remotely control raspberry pi via PuTTy on PC. To learn how to config raspberry pi, please visit lesson 1: getting started with raspberry pi.

Experimental Principle
BMP180 digital pressure sensor can detect pressure, temperature and altitude. We will use Raspberry Pi to input these data from BMP180 sensor through II2 interface, and print the value in the terminal window.

Schematic diagram

Note: Learn more about GPIO of raspberry pi, please review our lesson 2: Introduction Of Raspberry Pi GPIO

Hardware Setup
Assembling the Circuit as followed Connection Graph

Software

To use the I2C interface, we should enable the I2C first. Please enter the following command:

sudo  nano  /boot/config.txt

Open the file /boot/config.txt, find the code line”dtparam=i2c_arm”,checking if there is # sign in front of the line, uncomment it (remove the # in front of this line), and make sure the end of the line is”on”, finally the code should look like this:


Press Ctrl+X,and type “Y” to save the file you revised. reboot Pi.

We’ll provide two kinds of codes for C language users and Python language users.

For C language users, please follow the next step:


Please be sure installation wiringpi. If you have not installed wiringPi, read  lesson 3: installation wiringpi

Step 1) Download and unzip the code

cd ~
wget  http://osoyoo.com/driver/pi3_start_learning_kit_lesson_18/bmp180-c.tar.gz
sudo   tar  zxvf   bmp180-c.tar.gz

Step 2) Change the path
cd   bmp180-c

Step 3) Compile code
sudo gcc -Wall -o bmp180 bmp180test.c -lwiringPi -lm

Step 4) Run program
sudo  ./bmp180
Program result
Once the program starts running , the terminal will show pressure, temperature and altitude which BMP180 sensor detects.

bmp180.h and bmp180test.c Code

bmp180.h
 
  1. #ifndef _BMP180_
  2. #define _BMP180_
  3. //i2c address
  4. #define BMP180_Address 0x77
  5. //Operating Modes
  6. #define BMP180_ULTRALOWPOWER 0
  7. #define BMP180_STANDARD 1
  8. #define BMP180_HIGHRES 2
  9. #define BMP180_ULTRAHIGHRES 3
  10. //BMP185 Registers
  11. #define BMP180_CAL_AC1 0xAA //Calibration data (16 bits)
  12. #define BMP180_CAL_AC2 0xAC //Calibration data (16 bits)
  13. #define BMP180_CAL_AC3 0xAE //Calibration data (16 bits)
  14. #define BMP180_CAL_AC4 0xB0 //Calibration data (16 bits)
  15. #define BMP180_CAL_AC5 0xB2 //Calibration data (16 bits)
  16. #define BMP180_CAL_AC6 0xB4 //Calibration data (16 bits)
  17. #define BMP180_CAL_B1 0xB6 //Calibration data (16 bits)
  18. #define BMP180_CAL_B2 0xB8 //Calibration data (16 bits)
  19. #define BMP180_CAL_MB 0xBA //Calibration data (16 bits)
  20. #define BMP180_CAL_MC 0xBC //Calibration data (16 bits)
  21. #define BMP180_CAL_MD 0xBE //Calibration data (16 bits)
  22. #define BMP180_CONTROL 0xF4
  23. #define BMP180_TEMPDATA 0xF6
  24. #define BMP180_PRESSUREDATA 0xF6
  25. //Commands
  26. #define BMP180_READTEMPCMD 0x2E
  27. #define BMP180_READPRESSURECMD 0x34
  28. #endif
bmp180test.c
 
  1. #include < wiringPi.h>
  2. #include < wiringPiI2C.h>
  3. #include < stdio.h>
  4. #include < math.h>
  5. #include "bmp180.h"
  6. #define OSS BMP180_STANDARD
  7. short AC1,AC2,AC3,B1,B2,MB,MC,MD;
  8. unsigned short AC4,AC5,AC6;
  9. int fd;
  10. char I2C_readByte(int reg)
  11. {
  12. return (char)wiringPiI2CReadReg8(fd,reg);
  13. }
  14. unsigned short I2C_readU16(int reg)
  15. {
  16. int MSB,LSB;
  17. MSB = I2C_readByte(reg);
  18. LSB = I2C_readByte(reg + 1);
  19. int value = (MSB 32767)result -= 65536;
  20. return (short)result;
  21. }
  22. void I2C_writeByte(int reg,int val)
  23. {
  24. wiringPiI2CWriteReg8(fd,reg,val);
  25. }
  26. void load_calibration()
  27. {
  28. AC1 = I2C_readS16(BMP180_CAL_AC1);
  29. AC2 = I2C_readS16(BMP180_CAL_AC2);
  30. AC3 = I2C_readS16(BMP180_CAL_AC3);
  31. AC4 = I2C_readU16(BMP180_CAL_AC4);
  32. AC5 = I2C_readU16(BMP180_CAL_AC5);
  33. AC6 = I2C_readU16(BMP180_CAL_AC6);
  34. B1 = I2C_readS16(BMP180_CAL_B1);
  35. B2 = I2C_readS16(BMP180_CAL_B2);
  36. MB = I2C_readS16(BMP180_CAL_MB);
  37. MC = I2C_readS16(BMP180_CAL_MC);
  38. MD = I2C_readS16(BMP180_CAL_MD);
  39. }
  40. int read_raw_temp()
  41. {
  42. int raw;
  43. I2C_writeByte(BMP180_CONTROL,BMP180_READTEMPCMD);
  44. delay(5); //5ms;
  45. raw = I2C_readByte(BMP180_TEMPDATA)
  46. raw += I2C_readByte(BMP180_TEMPDATA+1);
  47. return raw;
  48. }
  49. int read_raw_pressure()
  50. {
  51. int MSB,LSB,XLSB,raw;
  52. I2C_writeByte(BMP180_CONTROL,BMP180_READPRESSURECMD +(OSS
  53. switch(OSS)
  54. {
  55. case BMP180_ULTRALOWPOWER:
  56. delay(5);break;
  57. case BMP180_HIGHRES:
  58. delay(14);break;
  59. case BMP180_ULTRAHIGHRES:
  60. delay(26);break;
  61. default :
  62. delay(8);
  63. }
  64. MSB = I2C_readByte(BMP180_PRESSUREDATA);
  65. LSB = I2C_readByte(BMP180_PRESSUREDATA + 1);
  66. XLSB = I2C_readByte(BMP180_PRESSUREDATA + 2);
  67. raw = ((MSB (8 - OSS);
  68. return raw;
  69. }
  70. float read_temperature()
  71. {
  72. float T;
  73. int UT,X1,X2,B5;
  74. UT = read_raw_temp();
  75. X1 = ((UT - AC6)*AC5) >> 15;
  76. X2 = (MC > 4) /10.0;
  77. return T;
  78. }
  79. int read_pressure()
  80. {
  81. int P;
  82. int UT,UP,X1,X2,X3,B3,B5,B6;
  83. unsigned int B4;
  84. int B7;
  85. UT = read_raw_temp();
  86. UP = read_raw_pressure();
  87. X1 = ((UT - AC6)*AC5) >> 15;
  88. X2 = (MC > 12) >> 11;
  89. X2 = (AC2 * B6) >> 11;
  90. X3 = X1 + X2;
  91. B3 = (((AC1 * 4 + X3) > 13;
  92. X2 = (B1 * ((B6 * B6) >> 12)) >> 16;
  93. X3 = ((X1 + X2) + 2) >> 2;
  94. B4 = (AC4 * (X3 + 32768)) >> 15;
  95. B7 = (UP - B3) * (50000 >> OSS);
  96. if (B7 < 0x80000000){P = (B7 * 2) / B4;} else {P = (B7 / B4) * 2;} X1 = (P >> 8) * (P >> 8);
  97. X1 = (X1 * 3038) >> 16;
  98. X2 = (-7357 * P) >> 16;
  99. P = P + ((X1 + X2 + 3791) >> 4);
  100. return P;
  101. }
  102. float read_altitude()
  103. {
  104. float pressure,altitude;
  105. float sealevel_pa = 101325.0;
  106. pressure = (float)read_pressure();
  107. altitude = 44330.0 * (1.0 - pow(pressure / sealevel_pa,(1.0/5.255)));
  108. return altitude;
  109. }
  110. float read_sealevel_pressure()
  111. {
  112. float altitude_m = 0.0;
  113. float pressure,p0;
  114. pressure =(float)read_pressure();
  115. p0 = pressure / pow(1.0 - altitude_m/44330.0,5.255);
  116. return p0;
  117. }
  118. int main(int argc,char **argv)
  119. {
  120. printf("BMP180 Test Program ...\n");
  121. if(wiringPiSetup() < 0) return 1;
  122. fd = wiringPiI2CSetup(BMP180_Address);
  123. load_calibration();
  124. while(1)
  125. {
  126. printf("\nTemperature : %.2f C\n",read_temperature());
  127. printf("Pressure : %.2f Pa\n",read_pressure()/100.0);
  128. printf("Altitude : %.2f h\n",read_altitude());
  129. delay(1000);
  130. }
  131. return 0;

For python users


Learn more about Python, please go to Raspberry Pi Starter Kit lesson 4

1) Please install a python tool software (smbus) to communicate with IIC

cd  ~

sudo apt-get install python-smbus

2) Download and unzip the code

cd  ~

wget  http://osoyoo.com/driver/pi3_start_learning_kit_lesson_18/bmp180-python.tar.gz

sudo  tar zxvf  bmp180-python.tar.gz

3) Change the path

cd  bmp180-python

4) Run program

sudo  python  ./BMP180test.py

5) Program result

Once the program starts running , the terminal will show pressure, temperature and altitude which BMP180 sensor detects.

BMP180.py, BMP180.pyc and BMP180test.py code

BMP180.py
 
  1. import time
  2. import smbus
  3. # BMP085 default address.
  4. BMP180_I2CADDR = 0x77
  5. # Operating Modes
  6. BMP180_ULTRALOWPOWER = 0
  7. BMP180_STANDARD = 1
  8. BMP180_HIGHRES = 2
  9. BMP180_ULTRAHIGHRES = 3
  10. # BMP085 Registers
  11. BMP180_CAL_AC1 = 0xAA # R Calibration data (16 bits)
  12. BMP180_CAL_AC2 = 0xAC # R Calibration data (16 bits)
  13. BMP180_CAL_AC3 = 0xAE # R Calibration data (16 bits)
  14. BMP180_CAL_AC4 = 0xB0 # R Calibration data (16 bits)
  15. BMP180_CAL_AC5 = 0xB2 # R Calibration data (16 bits)
  16. BMP180_CAL_AC6 = 0xB4 # R Calibration data (16 bits)
  17. BMP180_CAL_B1 = 0xB6 # R Calibration data (16 bits)
  18. BMP180_CAL_B2 = 0xB8 # R Calibration data (16 bits)
  19. BMP180_CAL_MB = 0xBA # R Calibration data (16 bits)
  20. BMP180_CAL_MC = 0xBC # R Calibration data (16 bits)
  21. BMP180_CAL_MD = 0xBE # R Calibration data (16 bits)
  22. BMP180_CONTROL = 0xF4
  23. BMP180_TEMPDATA = 0xF6
  24. BMP180_PRESSUREDATA = 0xF6
  25. # Commands
  26. BMP180_READTEMPCMD = 0x2E
  27. BMP180_READPRESSURECMD = 0x34
  28. class BMP180(object):
  29. def __init__(self, address=BMP180_I2CADDR, mode=BMP180_STANDARD):
  30. self._mode = mode
  31. self._address = address
  32. self._bus = smbus.SMBus(1)
  33. # Load calibration values.
  34. self._load_calibration()
  35. def _read_byte(self,cmd):
  36. return self._bus.read_byte_data(self._address,cmd)
  37. def _read_u16(self,cmd):
  38. MSB = self._bus.read_byte_data(self._address,cmd)
  39. LSB = self._bus.read_byte_data(self._address,cmd+1)
  40. return (MSB 32767:result -= 65536
  41. return result
  42. def _write_byte(self,cmd,val):
  43. self._bus.write_byte_data(self._address,cmd,val)
  44. def _load_calibration(self):
  45. "load calibration"
  46. self.cal_AC1 = self._read_s16(BMP180_CAL_AC1) # INT16
  47. self.cal_AC2 = self._read_s16(BMP180_CAL_AC2) # INT16
  48. self.cal_AC3 = self._read_s16(BMP180_CAL_AC3) # INT16
  49. self.cal_AC4 = self._read_u16(BMP180_CAL_AC4) # UINT16
  50. self.cal_AC5 = self._read_u16(BMP180_CAL_AC5) # UINT16
  51. self.cal_AC6 = self._read_u16(BMP180_CAL_AC6) # UINT16
  52. self.cal_B1 = self._read_s16(BMP180_CAL_B1) # INT16
  53. self.cal_B2 = self._read_s16(BMP180_CAL_B2) # INT16
  54. self.cal_MB = self._read_s16(BMP180_CAL_MB) # INT16
  55. self.cal_MC = self._read_s16(BMP180_CAL_MC) # INT16
  56. self.cal_MD = self._read_s16(BMP180_CAL_MD) # INT16
  57. def read_raw_temp(self):
  58. """Reads the raw (uncompensated) temperature from the sensor."""
  59. self._write_byte(BMP180_CONTROL, BMP180_READTEMPCMD)
  60. time.sleep(0.005) # Wait 5ms
  61. MSB = self._read_byte(BMP180_TEMPDATA)
  62. LSB = self._read_byte(BMP180_TEMPDATA+1)
  63. raw = (MSB
  64. return raw
  65. def read_raw_pressure(self):
  66. """Reads the raw (uncompensated) pressure level from the sensor."""
  67. self._write_byte(BMP180_CONTROL, BMP180_READPRESSURECMD + (self._mode
  68. if self._mode == BMP180_ULTRALOWPOWER:
  69. time.sleep(0.005)
  70. elif self._mode == BMP180_HIGHRES:
  71. time.sleep(0.014)
  72. elif self._mode == BMP180_ULTRAHIGHRES:
  73. time.sleep(0.026)
  74. else:
  75. time.sleep(0.008)
  76. MSB = self._read_byte(BMP180_PRESSUREDATA)
  77. LSB = self._read_byte(BMP180_PRESSUREDATA+1)
  78. XLSB = self._read_byte(BMP180_PRESSUREDATA+2)
  79. raw = ((MSB (8 - self._mode)
  80. return raw
  81. def read_temperature(self):
  82. """Gets the compensated temperature in degrees celsius."""
  83. UT = self.read_raw_temp()
  84. X1 = ((UT - self.cal_AC6) * self.cal_AC5) >> 15
  85. X2 = (self.cal_MC > 4) / 10.0
  86. return temp
  87. def read_pressure(self):
  88. """Gets the compensated pressure in Pascals."""
  89. UT = self.read_raw_temp()
  90. UP = self.read_raw_pressure()
  91. X1 = ((UT - self.cal_AC6) * self.cal_AC5) >> 15
  92. X2 = (self.cal_MC > 12) >> 11
  93. X2 = (self.cal_AC2 * B6) >> 11
  94. X3 = X1 + X2
  95. B3 = (((self.cal_AC1 * 4 + X3) > 13
  96. X2 = (self.cal_B1 * ((B6 * B6) >> 12)) >> 16
  97. X3 = ((X1 + X2) + 2) >> 2
  98. B4 = (self.cal_AC4 * (X3 + 32768)) >> 15
  99. B7 = (UP - B3) * (50000 >> self._mode)
  100. if B7 < 0x80000000: p = (B7 * 2) / B4 else: p = (B7 / B4) * 2 X1 = (p >> 8) * (p >> 8)
  101. X1 = (X1 * 3038) >> 16
  102. X2 = (-7357 * p) >> 16
  103. p = p + ((X1 + X2 + 3791) >> 4)
  104. return p
  105. def read_altitude(self, sealevel_pa=101325.0):
  106. """Calculates the altitude in meters."""
  107. # Calculation taken straight from section 3.6 of the datasheet.
  108. pressure = float(self.read_pressure())
  109. altitude = 44330.0 * (1.0 - pow(pressure / sealevel_pa, (1.0/5.255)))
  110. return altitude
  111. def read_sealevel_pressure(self, altitude_m=0.0):
  112. """Calculates the pressure at sealevel when given a known altitude in
  113. meters. Returns a value in Pascals."""
  114. pressure = float(self.read_pressure())
  115. p0 = pressure / pow(1.0 - altitude_m/44330.0, 5.255)
  116. return p0
BMP180test.py
 
  1. import time
  2. from BMP180 import BMP180
  3. # Initialise the BMP085 and use STANDARD mode (default value)
  4. # bmp = BMP085(0x77, debug=True)
  5. bmp = BMP180()
  6. # To specify a different operating mode, uncomment one of the following:
  7. # bmp = BMP085(0x77, 0) # ULTRALOWPOWER Mode
  8. # bmp = BMP085(0x77, 1) # STANDARD Mode
  9. # bmp = BMP085(0x77, 2) # HIRES Mode
  10. # bmp = BMP085(0x77, 3) # ULTRAHIRES Mode
  11. while True:
  12. temp = bmp.read_temperature()
  13. # Read the current barometric pressure level
  14. pressure = bmp.read_pressure()
  15. # To calculate altitude based on an estimated mean sea level pressure
  16. # (1013.25 hPa) call the function as follows, but this won't be very accurate
  17. altitude = bmp.read_altitude()
  18. # To specify a more accurate altitude, enter the correct mean sea level
  19. # pressure level. For example, if the current pressure level is 1023.50 hPa
  20. # enter 102350 since we include two decimal places in the integer value
  21. # altitude = bmp.readAltitude(102350)
  22. print "Temperature: %.2f C" % temp
  23. print "Pressure: %.2f hPa" % (pressure / 100.0)
  24. print "Altitude: %.2f\n" % altitude
  25. time.sleep(2)

Leave a Reply

WordPress spam blocked by CleanTalk.
Bombax Theme designed by itx
"