Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.26 KB | None | 0 0
  1. /* SFE_BMP180 library example sketch
  2.  
  3. This sketch shows how to use the SFE_BMP180 library to read the
  4. Bosch BMP180 barometric pressure sensor.
  5. https://www.sparkfun.com/products/11824
  6.  
  7. Like most pressure sensors, the BMP180 measures absolute pressure.
  8. This is the actual ambient pressure seen by the device, which will
  9. vary with both altitude and weather.
  10.  
  11. Before taking a pressure reading you must take a temparture reading.
  12. This is done with startTemperature() and getTemperature().
  13. The result is in degrees C.
  14.  
  15. Once you have a temperature reading, you can take a pressure reading.
  16. This is done with startPressure() and getPressure().
  17. The result is in millibar (mb) aka hectopascals (hPa).
  18.  
  19. If you'll be monitoring weather patterns, you will probably want to
  20. remove the effects of altitude. This will produce readings that can
  21. be compared to the published pressure readings from other locations.
  22. To do this, use the sealevel() function. You will need to provide
  23. the known altitude at which the pressure was measured.
  24.  
  25. If you want to measure altitude, you will need to know the pressure
  26. at a baseline altitude. This can be average sealevel pressure, or
  27. a previous pressure reading at your altitude, in which case
  28. subsequent altitude readings will be + or - the initial baseline.
  29. This is done with the altitude() function.
  30.  
  31. Hardware connections:
  32.  
  33. - (GND) to GND
  34. + (VDD) to 3.3V
  35.  
  36. (WARNING: do not connect + to 5V or the sensor will be damaged!)
  37.  
  38. You will also need to connect the I2C pins (SCL and SDA) to your
  39. Arduino. The pins are different on different Arduinos:
  40.  
  41. Any Arduino pins labeled: SDA SCL
  42. Uno, Redboard, Pro: A4 A5
  43. Mega2560, Due: 20 21
  44. Leonardo: 2 3
  45.  
  46. Leave the IO (VDDIO) pin unconnected. This pin is for connecting
  47. the BMP180 to systems with lower logic levels such as 1.8V
  48.  
  49. Have fun! -Your friends at SparkFun.
  50.  
  51. The SFE_BMP180 library uses floating-point equations developed by the
  52. Weather Station Data Logger project: http://wmrx00.sourceforge.net/
  53.  
  54. Our example code uses the "beerware" license. You can do anything
  55. you like with this code. No really, anything. If you find it useful,
  56. buy me a beer someday.
  57.  
  58. V10 Mike Grusin, SparkFun Electronics 10/24/2013
  59. V1.1.2 Updates for Arduino 1.6.4 5/2015
  60. */
  61.  
  62. // Your sketch must #include this library, and the Wire library.
  63. // (Wire is a standard library included with Arduino.):
  64.  
  65. #include <SFE_BMP180.h>
  66. #include <Wire.h>
  67.  
  68. // You will need to create an SFE_BMP180 object, here called "pressure":
  69.  
  70. SFE_BMP180 pressure;
  71.  
  72. #define ALTITUDE 1655.0 // Altitude of SparkFun's HQ in Boulder, CO. in meters
  73.  
  74. void setup()
  75. {
  76. Serial.begin(9600);
  77. Serial.println("REBOOT");
  78.  
  79. // Initialize the sensor (it is important to get calibration values stored on the device).
  80.  
  81. if (pressure.begin())
  82. Serial.println("BMP180 init success");
  83. else
  84. {
  85. // Oops, something went wrong, this is usually a connection problem,
  86. // see the comments at the top of this sketch for the proper connections.
  87.  
  88. Serial.println("BMP180 init fail\n\n");
  89. while(1); // Pause forever.
  90. }
  91. }
  92.  
  93. void loop()
  94. {
  95. char status;
  96. double T,P,p0,a;
  97.  
  98. // Loop here getting pressure readings every 10 seconds.
  99.  
  100. // If you want sea-level-compensated pressure, as used in weather reports,
  101. // you will need to know the altitude at which your measurements are taken.
  102. // We're using a constant called ALTITUDE in this sketch:
  103.  
  104. Serial.println();
  105. Serial.print("provided altitude: ");
  106. Serial.print(ALTITUDE,0);
  107. Serial.print(" meters, ");
  108. Serial.print(ALTITUDE*3.28084,0);
  109. Serial.println(" feet");
  110.  
  111. // If you want to measure altitude, and not pressure, you will instead need
  112. // to provide a known baseline pressure. This is shown at the end of the sketch.
  113.  
  114. // You must first get a temperature measurement to perform a pressure reading.
  115.  
  116. // Start a temperature measurement:
  117. // If request is successful, the number of ms to wait is returned.
  118. // If request is unsuccessful, 0 is returned.
  119.  
  120. status = pressure.startTemperature();
  121. if (status != 0)
  122. {
  123. // Wait for the measurement to complete:
  124. delay(status);
  125.  
  126. // Retrieve the completed temperature measurement:
  127. // Note that the measurement is stored in the variable T.
  128. // Function returns 1 if successful, 0 if failure.
  129.  
  130. status = pressure.getTemperature(T);
  131. if (status != 0)
  132. {
  133. // Print out the measurement:
  134. Serial.print("temperature: ");
  135. Serial.print(T,2);
  136. Serial.print(" deg C, ");
  137. Serial.print((9.0/5.0)*T+32.0,2);
  138. Serial.println(" deg F");
  139.  
  140. // Start a pressure measurement:
  141. // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
  142. // If request is successful, the number of ms to wait is returned.
  143. // If request is unsuccessful, 0 is returned.
  144.  
  145. status = pressure.startPressure(3);
  146. if (status != 0)
  147. {
  148. // Wait for the measurement to complete:
  149. delay(status);
  150.  
  151. // Retrieve the completed pressure measurement:
  152. // Note that the measurement is stored in the variable P.
  153. // Note also that the function requires the previous temperature measurement (T).
  154. // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
  155. // Function returns 1 if successful, 0 if failure.
  156.  
  157. status = pressure.getPressure(P,T);
  158. if (status != 0)
  159. {
  160. // Print out the measurement:
  161. Serial.print("absolute pressure: ");
  162. Serial.print(P,2);
  163. Serial.print(" mb, ");
  164. Serial.print(P*0.0295333727,2);
  165. Serial.println(" inHg");
  166.  
  167. // The pressure sensor returns abolute pressure, which varies with altitude.
  168. // To remove the effects of altitude, use the sealevel function and your current altitude.
  169. // This number is commonly used in weather reports.
  170. // Parameters: P = absolute pressure in mb, ALTITUDE = current altitude in m.
  171. // Result: p0 = sea-level compensated pressure in mb
  172.  
  173. p0 = pressure.sealevel(P,ALTITUDE); // we're at 1655 meters (Boulder, CO)
  174. Serial.print("relative (sea-level) pressure: ");
  175. Serial.print(p0,2);
  176. Serial.print(" mb, ");
  177. Serial.print(p0*0.0295333727,2);
  178. Serial.println(" inHg");
  179.  
  180. // On the other hand, if you want to determine your altitude from the pressure reading,
  181. // use the altitude function along with a baseline pressure (sea-level or other).
  182. // Parameters: P = absolute pressure in mb, p0 = baseline pressure in mb.
  183. // Result: a = altitude in m.
  184.  
  185. a = pressure.altitude(P,p0);
  186. Serial.print("computed altitude: ");
  187. Serial.print(a,0);
  188. Serial.print(" meters, ");
  189. Serial.print(a*3.28084,0);
  190. Serial.println(" feet");
  191. }
  192. else Serial.println("error retrieving pressure measurement\n");
  193. }
  194. else Serial.println("error starting pressure measurement\n");
  195. }
  196. else Serial.println("error retrieving temperature measurement\n");
  197. }
  198. else Serial.println("error starting temperature measurement\n");
  199.  
  200. delay(1000); // Pause for 1 seconds.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement