This page is based on the new v2 of the BMP085 driver, which uses Adafruit's new Unified Sensor Driver. The driver provides better support for altitude calculations, and makes it easy to switch between the BMP085 and any other supported pressure sensor in your projects.
If you haven't already done so, you'll need to install the Adafrut_Sensor library on your system as well, since Adafruit_BMP085 relies on this library to generate the sensor data in a universal manner.
Using the BMP085 / BMP180
To use this sensor and calculate the altitude and barometric pressure, there's a lot of very hairy and unpleasant math. You can check out the math in the datasheet but really, its not intuitive or educational - its just how the sensor works. So we took care of all the icky math and wrapped it up into a nice Arduino library.
You can find the Arduino library repository on github To install it, click this button to download the compressed ZIP file then install it. This guide will help you with the install process if you have never installed an Arduino library.
The same code is used for both the BMP085 and BMP180 (they are compatible!)
Restart the IDE
Now you can run this first example sketch
#include <Wire.h> #include <Adafruit_Sensor.h> #include <Adafruit_BMP085_U.h> Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085); void setup(void) { Serial.begin(9600); Serial.println("Pressure Sensor Test"); Serial.println(""); /* Initialise the sensor */ if(!bmp.begin()) { /* There was a problem detecting the BMP085 ... check your connections */ Serial.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!"); while(1); } } void loop(void) { /* Get a new sensor event */ sensors_event_t event; bmp.getEvent(&event); /* Display the results (barometric pressure is measure in hPa) */ if (event.pressure) { /* Display atmospheric pressure in hPa */ Serial.print("Pressure: "); Serial.print(event.pressure); Serial.println(" hPa"); } else { Serial.println("Sensor error"); } delay(250); }
Altitude Measurements
Since we know that pressure drops as we gain altitude (that's why air is so thin on mountain-tops) we can compute the current altitude knowing the pressure and temperature. Again, there's a bit of hairy math involved, you can read about the calculations on wikipedia (where this graph is from).void loop(void) { /* Get a new sensor event */ sensors_event_t event; bmp.getEvent(&event); /* Display the results (barometric pressure is measure in hPa) */ if (event.pressure) { /* Display atmospheric pressue in hPa */ Serial.print("Pressure: "); Serial.print(event.pressure); Serial.println(" hPa"); /* Calculating altitude with reasonable accuracy requires pressure * * sea level pressure for your position at the moment the data is * * converted, as well as the ambient temperature in degress * * celcius. If you don't have these values, a 'generic' value of * * 1013.25 hPa can be used (defined as SENSORS_PRESSURE_SEALEVELHPA * * in sensors.h), but this isn't ideal and will give variable * * results from one day to the next. * * * * You can usually find the current SLP value by looking at weather * * websites or from environmental information centers near any major * * airport. * * * * For example, for Paris, France you can check the current mean * * pressure and sea level at: http://bit.ly/16Au8ol */ /* First we get the current temperature from the BMP085 */ float temperature; bmp.getTemperature(&temperature); Serial.print("Temperature: "); Serial.print(temperature); Serial.println(" C"); /* Then convert the atmospheric pressure, SLP and temp to altitude */ /* Update this next line with the current SLP for better results */ float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA; Serial.print("Altitude: "); Serial.print(bmp.pressureToAltitude(seaLevelPressure, event.pressure, temperature)); Serial.println(" m"); Serial.println(""); } else { Serial.println("Sensor error"); } delay(1000); }
Many weather sites, (particularly near major airports) will provide pressure readings. If you happened to be near Paris, France, for example, you might look up the current air pressure at Charles de Gaulle airport, which we can see is 1009 hPa (a meaningful difference from the generoc 1013.25 hPa value we are plugging in via the SENSORS_PRESSURE_SEALEVELHPA macro):
float seaLevelPressure = 1009;
Text editor powered by tinymce.