极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

楼主: do335maomao

用BMP180显示气压和温度

[复制链接]
发表于 2015-2-5 14:13:18 | 显示全部楼层 |阅读模式
本帖最后由 do335maomao 于 2015-2-5 14:17 编辑



这是BMP180模块,四个引脚

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

库:



  1. #include <SFE_BMP180.h>
  2. #include <Wire.h>

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

  4. double baseline; // 基准气压

  5. void setup()
  6. {
  7.   Serial.begin(9600);
  8.   Serial.println("REBOOT");

  9.   // 初始化传感器
  10.   if (pressure.begin())
  11.     Serial.println("BMP180 init success");
  12.   else
  13.   {
  14.     // 糟糕,气压计出问题了,多半是连线有问题
  15.     Serial.println("BMP180 init fail (disconnected?)\n\n");
  16.     while(1); // 暂停
  17.   }

  18.   //获得基准气压
  19.   baseline = getP();
  20.   
  21.   Serial.print("baseline pressure: ");
  22.   Serial.print(baseline);
  23.   Serial.println(" hPa");  
  24. }

  25. void loop()
  26. {
  27.   double a,p,t;
  28.   p = getP();// 获得一个气压值
  29.   a = pressure.altitude(p,baseline);//获得基于基准气压的高度值
  30.   
  31.   Serial.print("relative altitude: ");
  32.   if (a >= 0.0) Serial.print(" "); // 调整正数显示格式
  33.   Serial.print(a,1);
  34.   Serial.print(" meters ");  
  35.   
  36.   t = getT();// 获得一个温度值
  37.   Serial.print("temperature: ");
  38.   Serial.print(t,1);
  39.   Serial.println(" degrees");  
  40.   
  41.   
  42.   delay(500);//刷新率
  43. }


  44. double getP()
  45. {
  46.   char status;
  47.   double T,P,p0,a;
  48.   // You must first get a temperature measurement to perform a pressure reading.
  49.   // Start a temperature measurement:
  50.   // If request is successful, the number of ms to wait is returned.
  51.   // If request is unsuccessful, 0 is returned.
  52.   status = pressure.startTemperature();
  53.   if (status != 0)
  54.   {
  55.     // Wait for the measurement to complete:
  56.     delay(status);
  57.     // Retrieve the completed temperature measurement:
  58.     // Note that the measurement is stored in the variable T.
  59.     // Use '&T' to provide the address of T to the function.
  60.     // Function returns 1 if successful, 0 if failure.

  61.     status = pressure.getTemperature(T);
  62.     if (status != 0)
  63.     {
  64.       // Start a pressure measurement:
  65.       // The parameter is the oversampling setting, from 0 to 3 (highest res, longest wait).
  66.       // If request is successful, the number of ms to wait is returned.
  67.       // If request is unsuccessful, 0 is returned.
  68.       status = pressure.startPressure(3);
  69.       if (status != 0)
  70.       {
  71.         // Wait for the measurement to complete:
  72.         delay(status);
  73.         // Retrieve the completed pressure measurement:
  74.         // Note that the measurement is stored in the variable P.
  75.         // Use '&P' to provide the address of P.
  76.         // Note also that the function requires the previous temperature measurement (T).
  77.         // (If temperature is stable, you can do one temperature measurement for a number of pressure measurements.)
  78.         // Function returns 1 if successful, 0 if failure.
  79.         status = pressure.getPressure(P,T);
  80.         if (status != 0)
  81.         {
  82.           return P;
  83.         }
  84.         else Serial.println("error retrieving pressure measurement\n");
  85.       }
  86.       else Serial.println("error starting pressure measurement\n");
  87.     }
  88.     else Serial.println("error retrieving temperature measurement\n");
  89.   }
  90.   else Serial.println("error starting temperature measurement\n");
  91. }

  92. double getT()
  93. {
  94.   char status;
  95.   double T,p0;
  96.   status = pressure.startTemperature();
  97.   if (status != 0)
  98.   {
  99.     delay(status);
  100.     status = pressure.getTemperature(T);
  101.     if (status != 0)
  102.     {
  103.       status = pressure.startPressure(3);
  104.       return T;
  105.     }
  106.     else Serial.println("error retrieving temperature measurement\n");
  107.   }
  108.   else Serial.println("error starting temperature measurement\n");
  109. }
复制代码


效果:


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

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

发表于 2015-2-5 16:16:14 | 显示全部楼层
问题是我看数据表说这个bmp180是在海拔500米到9000米范围用的,我想很多地方都不适用吧.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-2-5 16:25:01 | 显示全部楼层
数字 发表于 2015-2-5 16:16
问题是我看数据表说这个bmp180是在海拔500米到9000米范围用的,我想很多地方都不适用吧.

是海拔-500米到9000米,在很多地方都是适用的
回复 支持 反对

使用道具 举报

发表于 2015-2-5 16:41:22 | 显示全部楼层
查了BOSCH的网站还真是,淘宝上的介绍也太不靠谱了.不知能否直接代用085而不改程序?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-2-5 18:06:41 | 显示全部楼层
数字 发表于 2015-2-5 16:41
查了BOSCH的网站还真是,淘宝上的介绍也太不靠谱了.不知能否直接代用085而不改程序?

这个还真没试过,听说是可以的
回复 支持 反对

使用道具 举报

发表于 2015-2-6 22:14:32 | 显示全部楼层
请问楼主,数据稳定性如何?
回复 支持 反对

使用道具 举报

发表于 2015-2-6 22:42:32 | 显示全部楼层
楼主,我做过MS5611气压计的试验,发现刚上电的时候,要等一段时间数据才能稳定(刚打开读数是一个值,然后一直掉或者一直升,到一个数值稳定下来,一般得大几秒才能稳定下来)。另外,读数的频率不一样,最终稳定的值也不一样,不知道BMP180是不是这样的
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-2-8 14:50:30 | 显示全部楼层
本帖最后由 do335maomao 于 2015-2-8 14:52 编辑
StarFlying 发表于 2015-2-6 22:42
楼主,我做过MS5611气压计的试验,发现刚上电的时候,要等一段时间数据才能稳定(刚打开读数是一个值,然后 ...


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

使用道具 举报

发表于 2015-3-9 14:20:24 | 显示全部楼层
Latitude怎么是负的?
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-12 14:33:55 | 显示全部楼层
smallfivecn 发表于 2015-3-9 14:20
Latitude怎么是负的?

因为是用标准大气压算的,和当地实际大气压是有出入的
回复 支持 反对

使用道具 举报

发表于 2015-4-4 16:21:28 | 显示全部楼层
我的A4,A5口已经有东西用了,怎么改到其他口去啊
回复 支持 反对

使用道具 举报

发表于 2015-10-18 21:07:37 | 显示全部楼层
太好了   谢谢楼主
回复 支持 反对

使用道具 举报

发表于 2015-10-26 18:32:38 | 显示全部楼层
数字 发表于 2015-2-5 16:41
查了BOSCH的网站还真是,淘宝上的介绍也太不靠谱了.不知能否直接代用085而不改程序?

这两个型号硬件软件都可以直代
回复 支持 反对

使用道具 举报

发表于 2016-12-24 17:15:02 | 显示全部楼层
我的孤独 发表于 2015-4-4 16:21
我的A4,A5口已经有东西用了,怎么改到其他口去啊

直接并联。I2C协议只要地址不冲突就可以直接并联。
回复 支持 反对

使用道具 举报

发表于 2017-3-27 19:22:43 | 显示全部楼层
I want to ask you ,how is line falsel what should I do
回复 支持 0 反对 1

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-3-29 13:29 , Processed in 0.053047 second(s), 26 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表