do335maomao 发表于 2015-2-5 14:13:18

用BMP180显示气压和温度

本帖最后由 do335maomao 于 2015-2-5 14:17 编辑



这是BMP180模块,四个引脚

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

效果:


参考资料:https://learn.sparkfun.com/tutorials/bmp180-barometric-pressure-sensor-hookup-

cjt 发表于 2017-3-27 19:22:43

I want to ask you ,how is line falsel what should I do

数字 发表于 2015-2-5 16:16:14

问题是我看数据表说这个bmp180是在海拔500米到9000米范围用的,我想很多地方都不适用吧.

do335maomao 发表于 2015-2-5 16:25:01

数字 发表于 2015-2-5 16:16 static/image/common/back.gif
问题是我看数据表说这个bmp180是在海拔500米到9000米范围用的,我想很多地方都不适用吧.

是海拔-500米到9000米,在很多地方都是适用的

数字 发表于 2015-2-5 16:41:22

查了BOSCH的网站还真是,淘宝上的介绍也太不靠谱了.不知能否直接代用085而不改程序?

do335maomao 发表于 2015-2-5 18:06:41

数字 发表于 2015-2-5 16:41 static/image/common/back.gif
查了BOSCH的网站还真是,淘宝上的介绍也太不靠谱了.不知能否直接代用085而不改程序?

这个还真没试过,听说是可以的

StarFlying 发表于 2015-2-6 22:14:32

请问楼主,数据稳定性如何?

StarFlying 发表于 2015-2-6 22:42:32

楼主,我做过MS5611气压计的试验,发现刚上电的时候,要等一段时间数据才能稳定(刚打开读数是一个值,然后一直掉或者一直升,到一个数值稳定下来,一般得大几秒才能稳定下来)。另外,读数的频率不一样,最终稳定的值也不一样,不知道BMP180是不是这样的

do335maomao 发表于 2015-2-8 14:50:30

本帖最后由 do335maomao 于 2015-2-8 14:52 编辑

StarFlying 发表于 2015-2-6 22:42 static/image/common/back.gif
楼主,我做过MS5611气压计的试验,发现刚上电的时候,要等一段时间数据才能稳定(刚打开读数是一个值,然后 ...

这位朋友不会是也是5imx的模友吧,ms5611没用过,bmp180的话没有你说的现象,基本在正负0.5m的范围内浮动,数据与时间和没有明显关系

smallfivecn 发表于 2015-3-9 14:20:24

Latitude怎么是负的?

do335maomao 发表于 2015-3-12 14:33:55

smallfivecn 发表于 2015-3-9 14:20 static/image/common/back.gif
Latitude怎么是负的?

因为是用标准大气压算的,和当地实际大气压是有出入的

我的孤独 发表于 2015-4-4 16:21:28

我的A4,A5口已经有东西用了,怎么改到其他口去啊

zpA 发表于 2015-10-18 21:07:37

太好了   谢谢楼主

b9ss 发表于 2015-10-26 18:32:38

数字 发表于 2015-2-5 16:41 static/image/common/back.gif
查了BOSCH的网站还真是,淘宝上的介绍也太不靠谱了.不知能否直接代用085而不改程序?

这两个型号硬件软件都可以直代

Salem 发表于 2016-12-24 17:15:02

我的孤独 发表于 2015-4-4 16:21
我的A4,A5口已经有东西用了,怎么改到其他口去啊

直接并联。I2C协议只要地址不冲突就可以直接并联。
页: [1]
查看完整版本: 用BMP180显示气压和温度