2017년 8월 8일 화요일

기압센서 사용하기

만약 weather center를 만든다면 기압센서를 추가하면 데이타가 더 그럴싸 하겠죠? ^^

기압, 고도, 온도를 측정할 수 있는 BMP180 센서를 이용하면 되는데 제가  집에 굴러다니는 것이 

이전 모델인 BMP085 센서라 이걸로 먼저 테스트해 보겠습니다. 

라이브러리가 호환이 되고 BMP180 이 개선 버전이라고 하네요.

단위별 변환값은 아래와 같습니다.

1 hPa = 100 Pa = 1 mbar = 0.001 bar
1 hPa = 0.75006168 Torr
1 hPa = 0.01450377 psi (pounds per square inch)
1 hPa = 0.02953337 inHg (inches of mercury)
1 hpa = 0.00098692 atm (standard atmospheres)
#BMP180 스펙
  • Vin: 3 to 5VDC
  • Logic: 3 to 5V compliant
  • Pressure sensing range: 300-1100 hPa (9000m to -500m above sea level)
  • Up to 0.03hPa / 0.25m resolution
  • -40 to +85°C operational range, +-2°C temperature accuracy
  • This board/chip uses I2C 7-bit address 0x77.
온도 오차는 좀 커서 온도 센서는 따로 이용해야 겠네요.
습기에 민감하므로 센서에 구멍이 있는데 여기로 물이 들어가지 않도록 주의해야 하고 
바람에 오차가 발생할 수 있으니 야외에 사용할때는 스펀지를 대주는 것도 한가지 방법이
되겠습니다.
/* SFE_BMP180 altitude example sketch
This sketch shows how to use the Bosch BMP180 pressure sensor
as an altimiter.
Like most pressure sensors, the BMP180 measures absolute pressure.
Since absolute pressure varies with altitude, you can use the pressure
to determine your altitude.
Because pressure also varies with weather, you must first take a pressure
reading at a known baseline altitude. Then you can measure variations
from that pressure
Hardware connections:
- (GND) to GND
+ (VDD) to 3.3V
(WARNING: do not connect + to 5V or the sensor will be damaged!)
You will also need to connect the I2C pins (SCL and SDA) to your
Arduino. The pins are different on different Arduinos:
Any Arduino pins labeled:  SDA  SCL
Uno, Redboard, Pro:        A4   A5
Mega2560, Due:             20   21
Leonardo:                   2    3
Leave the IO (VDDIO) pin unconnected. This pin is for connecting
the BMP180 to systems with lower logic levels such as 1.8V
Have fun! -Your friends at SparkFun.
The SFE_BMP180 library uses floating-point equations developed by the
Weather Station Data Logger project: http://wmrx00.sourceforge.net/
Our example code uses the "beerware" license. You can do anything
you like with this code. No really, anything. If you find it useful,
buy me a beer someday.
V10 Mike Grusin, SparkFun Electronics 10/24/2013
V1.1.2 Updates for Arduino 1.6.4 5/2015
*/
// Your sketch must #include this library, and the Wire library.
// (Wire is a standard library included with Arduino.):
#include <SFE_BMP180.h>
#include <Wire.h>
// You will need to create an SFE_BMP180 object, here called "pressure":
SFE_BMP180 pressure;
double baseline; // baseline pressure
void setup()
{
  Serial.begin(9600);
  Serial.println("REBOOT");
  // Initialize the sensor (it is important to get calibration values stored on the device).
  if (pressure.begin())
    Serial.println("BMP180 init success");
  else
  {
    // Oops, something went wrong, this is usually a connection problem,
    // see the comments at the top of this sketch for the proper connections.
    Serial.println("BMP180 init fail (disconnected?)\n\n");
    while(1); // Pause forever.
  }
  // Get the baseline pressure:
  baseline = getPressure();
  Serial.print("baseline pressure: ");
  Serial.print(baseline);
  Serial.println(" mb");  
}
void loop()
{
  double a, P;
  // Get a new pressure reading:
  P = getPressure();
  Serial.print("Pressure: ");
  Serial.print(P);
  Serial.println("hPa");
  // Show the relative altitude difference between
  // the new reading and the baseline reading:
  a = pressure.altitude(P,baseline);
  Serial.print("relative altitude: ");
  if (a >= 0.0) Serial.print(" "); // add a space for positive numbers
  Serial.print(a,1);
  Serial.print(" meters, ");
  if (a >= 0.0) Serial.print(" "); // add a space for positive numbers
  Serial.print(a*3.28084,0);
  Serial.println(" feet");
  delay(2000);
}
double getPressure()
{
  char status;
  double T,P,p0,a;
  // You must first get a temperature measurement to perform a pressure reading.
  // Start a temperature measurement:
  // If request is successful, the number of ms to wait is returned.
  // If request is unsuccessful, 0 is returned.
  status = pressure.startTemperature();
  if (status != 0)
  {
    // Wait for the measurement to complete:
    delay(status);
    // Retrieve the completed temperature measurement:
    // Note that the measurement is stored in the variable T.
    // Use '&T' to provide the address of T to the function.
    // Function returns 1 if successful, 0 if failure.
    status = pressure.getTemperature(T);
    if (status != 0)
    {
      // Start a pressure measurement:
      // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
      // If request is successful, the number of ms to wait is returned.
      // If request is unsuccessful, 0 is returned.
      Serial.print("Temp: ");
      Serial.print(T);
      Serial.println("C");
      status = pressure.startPressure(3);
      if (status != 0)
      {
        // Wait for the measurement to complete:
        delay(status);
        // Retrieve the completed pressure measurement:
        // Note that the measurement is stored in the variable P.
        // Use '&P' to provide the address of P.
        // Note also that the function requires the previous temperature measurement (T).
        // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
        // Function returns 1 if successful, 0 if failure.
        status = pressure.getPressure(P,T);
        if (status != 0)
        {
          return(P);
        }
        else Serial.println("error retrieving pressure measurement\n");
      }
      else Serial.println("error starting pressure measurement\n");
    }
    else Serial.println("error retrieving temperature measurement\n");
  }
  else Serial.println("error starting temperature measurement\n");
}

댓글 없음:

댓글 쓰기