怎么把BMP180接到Arduino 板测高度

我想使用BMP180来测一测水火箭的发射高度数据,并使用Arduino nano 来记录数据以便用电脑读取数据。可是不知道怎么将BMP连接到Arduino板上去,程序也没有写好 求帮助啊。

以Arduino Uno为例:VCC 接3.3v,GND接GND,SCL接A5,SDA接A4

代码如下


#include <SFE_BMP180.h>

#include <Wire.h>

 

SFE_BMP180 pressure;// 创建一个气压计对象

 

double baseline; // 基准气压

 

void setup()

{

  Serial.begin(9600);

  Serial.println("REBOOT");

 

  // 初始化传感器

  if (pressure.begin())

    Serial.println("BMP180 init success");

  else

  {

    // 糟糕,气压计出问题了,多半是连线有问题

    Serial.println("BMP180 init fail (disconnected?)\n\n");

    while(1); // 暂停

  }

 

  //获得基准气压

  baseline = getP();

 

  Serial.print("baseline pressure: ");

  Serial.print(baseline);

  Serial.println(" hPa");  

}

 

void loop()

{

  double a,p,t;

  p = getP();// 获得一个气压值

  a = pressure.altitude(p,baseline);//获得基于基准气压的高度值

 

  Serial.print("relative altitude: ");

  if (a >= 0.0) Serial.print(" "); // 调整正数显示格式

  Serial.print(a,1);

  Serial.print(" meters ");  

 

  t = getT();// 获得一个温度值

  Serial.print("temperature: ");

  Serial.print(t,1);

  Serial.println(" degrees");  

 

 

  delay(500);//刷新率

}

 

 

double getP()

{

  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.

      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");

}

 

double getT()

{

  char status;

  double T,p0;

  status = pressure.startTemperature();

  if (status != 0)

  {

    delay(status);

    status = pressure.getTemperature(T);

    if (status != 0)

    {

      status = pressure.startPressure(3);

      return T;

    }

    else Serial.println("error retrieving temperature measurement\n");

  }

  else Serial.println("error starting temperature measurement\n");

}



注意:串口会同时输出两组数据,A较精确


温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-11-22
手册有吗?

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网