HCBMP180 - Arduino Library for the BMP180 Temp/Pres Sensor

Useful guides, libraries, and example sketches to support our Arduino based products.
admin
Site Admin
Posts: 865
Joined: Sun Aug 05, 2012 4:02 pm

HCBMP180 - Arduino Library for the BMP180 Temp/Pres Sensor

Post by admin » Wed May 25, 2016 10:05 am

[IMAGE TBA]



Arduino library for the BMP180 temperature/pressure sensor. Currently supported products:

BMP180 GY-68 Digital Baromic Pressure Sensor Module (HCMODU0101) available from hobbycomponents.com

This library can be used to obtain calibrated temperature and pressure reading from a BMP180 sensor or module. The example has been written particularly for the BMP180 GY-68 Digital Baromic Pressure Sensor Module (HCMODU0101) available from hobbycomponents.com but should work with any compatible BMP180 sensor/module.


You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.

On Windows:
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/

Linux:
Usually found within the users home area under /Arduino/libraries/



Using the HCBMP180 library

To use the library just include the HCBMP180.h header file and then create an instance of the library. E.g:

Code: Select all

#include "HCBMP180.h"
HCBMP180 HCBMP180(I2CBMP180ADD);

To initialise the library add the following line to the setup() section of your sketch:

Code: Select all

HCBMP180.Init();

The following functions are available with this library:

Code: Select all

HCBMP180.Mode(Oss);
Sets the measurement mode. Where Oss is the sampling mode. Valid values are:

ULTRA_LOW_POWER
STANDARD
HIGH_RESOLUTION
ULTRA_HIGH_RESOLUTION

If this function is omitted then the library defaults to ULTRA_HIGH_RESOLUTION


Code: Select all

Temp = HCBMP180.GetTemp();
Gets a calibrated temperature reading from the sensor.

Returns an integer value containing the temperature value in 0.1oC increments. To convert to oC simply multiply the result by 10


Code: Select all

Pressure = HCBMP180.GetPressure();
Gets a calibrated pressure reading from the sensor.

Returns a long integer value containing the pressure in Pascals. To convert to Millibar (mb) simply divide the result by 100.


Code: Select all

Altitude = HCBMP180.Altitude(p, p0);
Calculates the current altitude based the sensors pressure reading. Where:
p is the current pressure in Pascals and can be obtained using the GetPressure() library function.

p0 is the current pressure at sea level in pascals. You can obtain an up to date value by visiting an on-line weather site for your location. To convert a value from Millibar (mb) to Pascals simply multiply the value by 100

Returns a value of type double containing the current altitude of the sensor in meters above sea level.


Code: Select all

Pressure = HCBMP180.SeaLevelPressure(p, a);
Calculates the current pressure at sea level (used for weather forecasting) where:
p is the current pressure in Pascals and can be obtained using the GetPressure() library function.

a is the sensors current altitude above sea level in meters. You can obtain this value by visiting an on-line map that provides altitude information for you location.

Returns a long integer value containing the current pressure in Pascals at sea level. To convert to Millibars simply divide the result by 100.



Image

Code: Select all

/* FILE:    HCBMP180_Example
   DATE:    24/05/16
   VERSION: 0.1
   AUTHOR:  Andrew Davies

   Created by Hobby Components Ltd (HOBBYCOMPONENTS.COM)
   
24/05/16 version 0.1: Original version

   
This is an example of how to use the HCBMP180 Arduino library to obtain measurement 
readings from a BMP180 sensor or module. The example has been written particularly 
for the BMP180 GY-68 Digital Baromic Pressure Sensor Module (HCMODU0101) available 
from hobbycomponents.com 

The example demonstrates how to get the current calibrated temperature and 
pressure reading from the sensor and also how to use the library to obtain current
Altitude above sea level, differential altitude, and baromic pressure at sea level
(useful for weather forecasting). To use the module connect it to your Arduino as 
follows:


BMP180....Uno/Nano

VIN.......+5V (for HCMODU00101) for other BMP180 modules check for correct supply voltage.
GND.......GND
SCL.......A5
SDA.......A4


You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/


/* Include the HCBMP180 library */
#include "HCBMP180.h"


/* Current air pressure at sea level in Pascals. Used for calculating the absolute 
 * altitude above sea level. You can obtain an up to date value by visiting an on-line 
 * weather site for your location that also provides current baromic air pressure. 
 * To convert a value from Millibar (mb) to Pascals simply multiply the value by 100 */
#define PRESSURE_AT_SEA_LEVEL 102100


/* Current sensor altitude above sea level in meters. Used for calculating the pressure 
 * at sea level (useful for weather forecasting). You can obtain this value by visiting 
 * an on-line map that provides altitude information for you location. */
#define LOCATION_ALTITUDE 60.29


/* Create an instance of the library */
HCBMP180 HCBMP180(I2CBMP180ADD);

/* Used for the differential altitude example */
double Reference_Altitude;


void setup() 
{
  /* Initialise the HCBMP180 library and the serial interface */
  HCBMP180.Init();
  Serial.begin(9600);

  /* Make an initial altitude measurement which will then be used by the differential 
   * altitude example to calculate any changes in height since the sketch was started */
  Reference_Altitude = HCBMP180.Altitude(HCBMP180.GetPressure(), PRESSURE_AT_SEA_LEVEL);
}



void loop() 
{
  int Temp;
  long Pressure, SeaLevel;
  double Altitude;

  /* Get the current temperature in 0.1oC units */
  Temp = HCBMP180.GetTemp();

  /* Output the temperature to the serial port. Temperature is in 0.1oC units so to 
   * convert to oC simply divide by 10 */
  Serial.print("Temperature (oC): ");
  Serial.println((float)Temp / 10);


  /* Get the current pressure in Pascals */
  Pressure = HCBMP180.GetPressure();

  /* Output the pressure to the serial port in Millibars. To convert from Pascals to
   * Millibars simply divide by 100 */
  Serial.print("Pressure (mb): ");
  Serial.println(Pressure / 100);


  /* Calculate the absolute height above sea level using the current pressure at sea 
   * level */
  Altitude = HCBMP180.Altitude(Pressure, PRESSURE_AT_SEA_LEVEL);

  /* Output the calculated height to the serial port */
  Serial.print("Sensor height above sea level (meters): ");
  Serial.println(Altitude);

  /* Calculate the pressure at sea level in Pascals */
  SeaLevel = HCBMP180.SeaLevelPressure(Pressure, LOCATION_ALTITUDE);

  /* Output the current pressure at sea level to the serial port. Pressure is in Pascals
   * so to convert to Millibar simply divide by 100 */
  Serial.print("Pressure at sea level (mb): ");
  Serial.println(SeaLevel / 100);


  /* Calculate any differences in altitude since the the sketch was started and output it
   * to the serial port */
  Serial.print("Differential altitude (meters): ");
  Serial.println(Altitude - Reference_Altitude);

  Serial.println();
  delay(1000);
}


Image

The HCBMP180 library can be downloaded here (please log in to download):
HCBMP180.zip
You do not have the required permissions to view the files attached to this post.

Graywallis
Posts: 11
Joined: Tue Nov 08, 2016 2:39 pm

Re: HCBMP180 - Arduino Library for the BMP180 Temp/Pres Sens

Post by Graywallis » Tue Nov 08, 2016 3:56 pm

Hi,
Excellent little example but Wire.h has been omitted. (Was this deliberate?)
Graywallis

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCBMP180 - Arduino Library for the BMP180 Temp/Pres Sens

Post by andrew » Wed Nov 09, 2016 10:32 am

The wire library is included within the HCBMP180 library. Are you saying you needed to add it to the main sketch to work? And if so could you let me know what Arduino board are you using?
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Graywallis
Posts: 11
Joined: Tue Nov 08, 2016 2:39 pm

Re: HCBMP180 - Arduino Library for the BMP180 Temp/Pres Sens

Post by Graywallis » Wed Nov 09, 2016 3:25 pm

I had to include wire.h in the main sketch and I'm using Arduino Uno. Just added the include statement under the include for the HCBMP180 library. IDE complained about no wire.h declarations. Not sure which version of Uno but purchased from Farnell about a year ago as part of 'The Arduino Starter Kit' made in Italy.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCBMP180 - Arduino Library for the BMP180 Temp/Pres Sens

Post by andrew » Wed Nov 09, 2016 4:38 pm

Thanks for getting back to me with the information. I've just tried the above example sketch and it seems to compile and run for an Uno with no errors. May I ask what version of Arduino IDE are you using? It should say the version in the top left hand corner of the window and will be something like 1.x.x
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Graywallis
Posts: 11
Joined: Tue Nov 08, 2016 2:39 pm

Re: HCBMP180 - Arduino Library for the BMP180 Temp/Pres Sens

Post by Graywallis » Fri Nov 11, 2016 4:21 pm

Arduino 1.0.5-r2. I had to add the following line: #include "Wire.h" directly below: #include "HCBMP180.h". Otherwise test program worked exactly as advertised.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCBMP180 - Arduino Library for the BMP180 Temp/Pres Sens

Post by andrew » Fri Nov 11, 2016 4:51 pm

Arduino 1.0.5-r2
Ah yes that's a very old version of the Arduino IDE. I've just tried it out myself on that version and I can replicate what you are reporting. Thanks for pointing it out.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Graywallis
Posts: 11
Joined: Tue Nov 08, 2016 2:39 pm

Re: HCBMP180 - Arduino Library for the BMP180 Temp/Pres Sens

Post by Graywallis » Thu Nov 17, 2016 12:40 pm

Ok, moved to v 1.16.12 and all solved (almost); I get suggestion than ' ' is unrecognised in wire.h and becomes 'uncategorised'. All works though.
Next Q: is there a method of adjusting for pressure/altitude drift with time? I get some 50 metres altitude drift over 12 hours and it doesn't seem to correlate with anything obvious, such as temperature.

Graywallis
Posts: 11
Joined: Tue Nov 08, 2016 2:39 pm

Re: HCBMP180 - Arduino Library for the BMP180 Temp/Pres Sens

Post by Graywallis » Thu Nov 17, 2016 1:26 pm

Or is this just a weather/pressure change; a bit quicker than the norm?

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCBMP180 - Arduino Library for the BMP180 Temp/Pres Sens

Post by andrew » Thu Nov 17, 2016 5:39 pm

is there a method of adjusting for pressure/altitude drift with time?
Unfortunately not, to calculate the altitude using the sensor you need to know the current pressure at sea level which the sensor itself can't measure.
I get some 50 metres altitude drift over 12 hours
It's likely it's due to air pressure changes over time. Try and update it with the latest sea level value to see if it gives a more accurate calculation. If it does then that's defiantly the cause.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Post Reply

Return to “Arduino”