风筝
发表于: 2019-9-9 19:04:14 | 显示全部楼层

在本篇文章中,我们将向您展示如何使用BMP180传感器和Arduino Uno开发板制作一个气压测量系统。我们还使用简单的I2C OLED模块直接在面包板上显示测量结果。


下载BMP085库

为了连接BMP180模块和Arduino,我们需要下载专为BMP180和BMP085模块设计的BMP085库。你可以在Github上找到这个库。


连接该库后,我们可以调用专用函数,以便更轻松地使用BMP180传感器。

display the measurements.png


什么是气压计?

气压计是一种测量大气压力的装置。


在学校,我们学习了第一个气压计是一个带有汞的板,上面有一个倒置的试管。发明者是意大利物理学家兼数学家福音传教士托里切利。


气压计有两种常见类型:酒精和汞基。但是,我们不能在机器人技术中使用这种庞大的气压计。我们需要更小,更容易连接的东西,所以我们使用电子气压计。


电子气压计

对于电子应用,我们需要一种小巧、高能效的设备,可轻松连接到像Arduino Uno这样的微控制器。大多数现代气压计都是采用MEMS技术制造的,就像加速度计和陀螺仪一样。 MEMS气压计基于压阻或应变仪方法,其使用在变形力的作用下改变材料的电阻的效应。


BMP085和BMP180传感器

最经济的压力传感器 - 通常由飞行控制器和各种自制电子设备使用 - 包括BOSCH传感器,如BMP085和BMP180。第二个压力传感器较新,但与旧版本完全兼容。


BMP180规格:

●    测量值范围:从300 hPa到1100 hPa(从海拔+ 9000m起-500m)。

●    供电电压范围为3.3V至5V。

●    电流强度:5μA,轮询速率为1赫兹。

●    噪声级:粗调模式(超低功耗模式)下0.06 hPa(0.5m),最大分辨率模式下的0.02 hPa(0.17m)(高级分辨率模式)。

●    我们可以将此传感器连接到任何微控制器并尝试估算大气压力。

bmp180.png


所需的组件

●    Arduino UNO开发板

●    BMP180模块

●    OLED显示屏12864

●    Arduino IDE

●    BMP180库

●    U8GLIB库


连接气压计

连接所有内容,如下图所示。将BMP180连接到A4和A5,OLED连接到另一个I2C端口。

Wiring the Barometer.png

传感器有一个I2C接口,因此您可以轻松地将它们连接到Arduino系列的任何平台。


上传代码

以下是本文使用的完整代码:

Source Code .png

  1. #include <Wire.h>
  2. #include <Adafruit_BMP085.h>
  3. #include "U8glib.h"

  4. // Connect VCC of the BMP085 sensor to 3.3V (NOT 5.0V!)
  5. // Connect GND to Ground
  6. // Connect SCL to i2c clock - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 5
  7. // Connect SDA to i2c data - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 4
  8. // EOC is not used, it signifies an end of conversion
  9. // XCLR is a reset pin, also not used here
  10. Adafruit_BMP085 bmp;

  11. U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);  // Display which does not send AC
  12. //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0);  // I2C / TWI
  13. //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST); // Fast I2C / TWI
  14. //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK);  // Display which does not send AC

  15. float pressure = 0.0;
  16. float tempC = 0.0;
  17. float altitude = 0.0;
  18. void BmpSensorRead(float* pressure, float* tempC, float* altitude);
  19. void DisplayPresTemp(float* pressure, float* tempC, float* altitude);

  20. void setup(void)
  21. {
  22.   Serial.begin(9600);

  23.   // assign default color value
  24.   if (u8g.getMode() == U8G_MODE_R3G3B2)
  25.   {
  26.     u8g.setColorIndex(255);     // white
  27.   }
  28.   else if (u8g.getMode() == U8G_MODE_GRAY2BIT)
  29.   {
  30.     u8g.setColorIndex(3);         // max intensity
  31.   }
  32.   else if (u8g.getMode() == U8G_MODE_BW)
  33.   {
  34.     u8g.setColorIndex(1);         // pixel on
  35.   }
  36.   else if (u8g.getMode() == U8G_MODE_HICOLOR)
  37.   {
  38.     u8g.setHiColorByRGB(255, 255, 255);
  39.   }

  40.   for (int a = 0; a < 30; a++)
  41.   {
  42.     u8g.firstPage();

  43.     do
  44.     {
  45.       u8g.setFont(u8g_font_fub11);
  46.       u8g.setFontRefHeightExtendedText();
  47.       u8g.setDefaultForegroundColor();
  48.       u8g.setFontPosTop();
  49.       u8g.drawStr(4, a, "MAKER . PRO ");
  50.     }
  51.     while (u8g.nextPage());
  52.   }

  53.   delay(3000);

  54.   if (!bmp.begin())
  55.   {
  56.     u8g.firstPage();

  57.     do
  58.     {
  59.       u8g.setFont(u8g_font_fub11);
  60.       u8g.setFontRefHeightExtendedText();
  61.       u8g.setDefaultForegroundColor();
  62.       u8g.setFontPosTop();
  63.       u8g.drawStr(4, 0, "BMP085 Sensor");
  64.       u8g.drawStr(4, 20, " ERROR!");
  65.     }
  66.     while (u8g.nextPage());

  67.     Serial.println("BMP085 sensor, ERROR!");

  68.     while (1) {}
  69.   }
  70. }
  71. void loop(void)
  72. {
  73.   BmpSensorRead(&pressure, &tempC, &altitude);
  74.   DisplayPresTemp(&pressure, &tempC, &altitude);
  75.   delay(1000);
  76. }
  77. void DisplayPresTemp(float* pressure, float* tempC, float* altitude)
  78. {
  79.   u8g.firstPage();

  80.   do
  81.   {
  82.     u8g.setFont(u8g_font_fub11);
  83.     u8g.setFontRefHeightExtendedText();
  84.     u8g.setDefaultForegroundColor();
  85.     u8g.setFontPosTop();
  86.     u8g.drawStr(2, 0, "Pressure");
  87.     u8g.setPrintPos(75, 0);
  88.     u8g.print(*pressure);
  89.     u8g.drawStr(4, 20, "Temp C");
  90.     u8g.setPrintPos(75, 20);
  91.     u8g.print(*tempC);
  92.     u8g.drawStr(4, 40, "Altitude");
  93.     u8g.setPrintPos(75, 40);
  94.     u8g.print(*altitude);
  95.   }
  96.   while (u8g.nextPage());
  97. }
  98. void BmpSensorRead(float* pressure, float* tempC, float* altitude)
  99. {
  100.   *tempC = bmp.readTemperature();
  101.   Serial.print("Temperature = ");
  102.   Serial.print(*tempC);
  103.   Serial.println(" *C");

  104.   *pressure = bmp.readPressure() / 100.0;
  105.   Serial.print("Pressure = ");
  106.   Serial.print(*pressure / 100.0);
  107.   Serial.println(" hPa");

  108.   // Calculate altitude assuming 'standard' barometric
  109.   // pressure of 1013.25 millibar = 101325 Pascal
  110.   *altitude = bmp.readAltitude();
  111.   Serial.print("Altitude = ");
  112.   Serial.print(*altitude);
  113.   Serial.println(" meters");
  114. }
复制代码

上传代码后,您应该会看到下图所示:

test.png

123.png

跳转到指定楼层
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题 700 | 回复: 1480



手机版|

GMT+8, 2024-4-25 14:30 , Processed in 0.036456 second(s), 6 queries , Gzip On, MemCache On. Powered by Discuz! X3.5

YiBoard一板网 © 2015-2022 地址:河北省石家庄市长安区高营大街 ( 冀ICP备18020117号 )

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