1#include <Wire.h> 2#include <Adafruit_Sensor.h> 3#include <Adafruit_BMP085_U.h> 4 5/* This driver uses the Adafruit unified sensor library (Adafruit_Sensor), 6 which provides a common 'type' for sensor data and some helper functions. 7 8 To use this driver you will also need to download the Adafruit_Sensor 9 library and include it in your libraries folder. 10 11 You should also assign a unique ID to this sensor for use with 12 the Adafruit Sensor API so that you can identify this particular 13 sensor in any data logs, etc. To assign a unique ID, simply 14 provide an appropriate value in the constructor below (12345 15 is used by default in this example). 16 17 Connections 18 =========== 19 Connect SCL to analog 5 20 Connect SDA to analog 4 21 Connect VDD to 3.3V DC 22 Connect GROUND to common ground 23 24 History 25 ======= 26 2013/JUN/17 - Updated altitude calculations (KTOWN) 27 2013/FEB/13 - First version (KTOWN) 28*/ 29 30Adafruit_BMP085_Unified bmp = Adafruit_BMP085_Unified(10085); 31 32/**************************************************************************/ 33/* 34 Displays some basic information on this sensor from the unified 35 sensor API sensor_t type (see Adafruit_Sensor for more information) 36*/ 37/**************************************************************************/ 38void displaySensorDetails(void) 39{ 40 sensor_t sensor; 41 bmp.getSensor(&sensor); 42 Serial.println("------------------------------------"); 43 Serial.print ("Sensor: "); Serial.println(sensor.name); 44 Serial.print ("Driver Ver: "); Serial.println(sensor.version); 45 Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id); 46 Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" hPa"); 47 Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" hPa"); 48 Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" hPa"); 49 Serial.println("------------------------------------"); 50 Serial.println(""); 51 delay(500); 52} 53 54/**************************************************************************/ 55/* 56 Arduino setup function (automatically called at startup) 57*/ 58/**************************************************************************/ 59void setup(void) 60{ 61 Serial.begin(9600); 62 Serial.println("Pressure Sensor Test"); Serial.println(""); 63 64 /* Initialise the sensor */ 65 if(!bmp.begin()) 66 { 67 /* There was a problem detecting the BMP085 ... check your connections */ 68 Serial.print("Ooops, no BMP085 detected ... Check your wiring or I2C ADDR!"); 69 while(1); 70 } 71 72 /* Display some basic information on this sensor */ 73 displaySensorDetails(); 74} 75 76/**************************************************************************/ 77/* 78 Arduino loop function, called once 'setup' is complete (your own code 79 should go here) 80*/ 81/**************************************************************************/ 82void loop(void) 83{ 84 /* Get a new sensor event */ 85 sensors_event_t event; 86 bmp.getEvent(&event); 87 88 /* Display the results (barometric pressure is measure in hPa) */ 89 if (event.pressure) 90 { 91 /* Display atmospheric pressue in hPa */ 92 Serial.print("Pressure: "); 93 Serial.print(event.pressure); 94 Serial.println(" hPa"); 95 96 /* Calculating altitude with reasonable accuracy requires pressure * 97 * sea level pressure for your position at the moment the data is * 98 * converted, as well as the ambient temperature in degress * 99 * celcius. If you don't have these values, a 'generic' value of * 100 * 1013.25 hPa can be used (defined as SENSORS_PRESSURE_SEALEVELHPA * 101 * in sensors.h), but this isn't ideal and will give variable * 102 * results from one day to the next. * 103 * * 104 * You can usually find the current SLP value by looking at weather * 105 * websites or from environmental information centers near any major * 106 * airport. * 107 * * 108 * For example, for Paris, France you can check the current mean * 109 * pressure and sea level at: http://bit.ly/16Au8ol */ 110 111 /* First we get the current temperature from the BMP085 */ 112 float temperature; 113 bmp.getTemperature(&temperature); 114 Serial.print("Temperature: "); 115 Serial.print(temperature); 116 Serial.println(" C"); 117 118 /* Then convert the atmospheric pressure, and SLP to altitude */ 119 /* Update this next line with the current SLP for better results */ 120 float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA; 121 Serial.print("Altitude: "); 122 Serial.print(bmp.pressureToAltitude(seaLevelPressure, 123 event.pressure)); 124 Serial.println(" m"); 125 Serial.println(""); 126 } 127 else 128 { 129 Serial.println("Sensor error"); 130 } 131 delay(1000); 132}