|
在本篇文章中,我们将向您展示如何使用BMP180传感器和Arduino Uno开发板制作一个气压测量系统。我们还使用简单的I2C OLED模块直接在面包板上显示测量结果。
下载BMP085库 为了连接BMP180模块和Arduino,我们需要下载专为BMP180和BMP085模块设计的BMP085库。你可以在Github上找到这个库。
连接该库后,我们可以调用专用函数,以便更轻松地使用BMP180传感器。
什么是气压计? 气压计是一种测量大气压力的装置。
在学校,我们学习了第一个气压计是一个带有汞的板,上面有一个倒置的试管。发明者是意大利物理学家兼数学家福音传教士托里切利。
气压计有两种常见类型:酒精和汞基。但是,我们不能在机器人技术中使用这种庞大的气压计。我们需要更小,更容易连接的东西,所以我们使用电子气压计。
电子气压计 对于电子应用,我们需要一种小巧、高能效的设备,可轻松连接到像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)(高级分辨率模式)。 ● 我们可以将此传感器连接到任何微控制器并尝试估算大气压力。
所需的组件 ● Arduino UNO开发板 ● BMP180模块 ● OLED显示屏12864 ● Arduino IDE ● BMP180库 ● U8GLIB库
连接气压计 连接所有内容,如下图所示。将BMP180连接到A4和A5,OLED连接到另一个I2C端口。
传感器有一个I2C接口,因此您可以轻松地将它们连接到Arduino系列的任何平台。
上传代码 以下是本文使用的完整代码:
- #include <Wire.h>
- #include <Adafruit_BMP085.h>
- #include "U8glib.h"
- // Connect VCC of the BMP085 sensor to 3.3V (NOT 5.0V!)
- // Connect GND to Ground
- // Connect SCL to i2c clock - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 5
- // Connect SDA to i2c data - on '168/'328 Arduino Uno/Duemilanove/etc thats Analog 4
- // EOC is not used, it signifies an end of conversion
- // XCLR is a reset pin, also not used here
- Adafruit_BMP085 bmp;
- U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send AC
- //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE|U8G_I2C_OPT_DEV_0); // I2C / TWI
- //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_DEV_0|U8G_I2C_OPT_NO_ACK|U8G_I2C_OPT_FAST); // Fast I2C / TWI
- //U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NO_ACK); // Display which does not send AC
- float pressure = 0.0;
- float tempC = 0.0;
- float altitude = 0.0;
- void BmpSensorRead(float* pressure, float* tempC, float* altitude);
- void DisplayPresTemp(float* pressure, float* tempC, float* altitude);
- void setup(void)
- {
- Serial.begin(9600);
- // assign default color value
- if (u8g.getMode() == U8G_MODE_R3G3B2)
- {
- u8g.setColorIndex(255); // white
- }
- else if (u8g.getMode() == U8G_MODE_GRAY2BIT)
- {
- u8g.setColorIndex(3); // max intensity
- }
- else if (u8g.getMode() == U8G_MODE_BW)
- {
- u8g.setColorIndex(1); // pixel on
- }
- else if (u8g.getMode() == U8G_MODE_HICOLOR)
- {
- u8g.setHiColorByRGB(255, 255, 255);
- }
- for (int a = 0; a < 30; a++)
- {
- u8g.firstPage();
- do
- {
- u8g.setFont(u8g_font_fub11);
- u8g.setFontRefHeightExtendedText();
- u8g.setDefaultForegroundColor();
- u8g.setFontPosTop();
- u8g.drawStr(4, a, "MAKER . PRO ");
- }
- while (u8g.nextPage());
- }
- delay(3000);
- if (!bmp.begin())
- {
- u8g.firstPage();
- do
- {
- u8g.setFont(u8g_font_fub11);
- u8g.setFontRefHeightExtendedText();
- u8g.setDefaultForegroundColor();
- u8g.setFontPosTop();
- u8g.drawStr(4, 0, "BMP085 Sensor");
- u8g.drawStr(4, 20, " ERROR!");
- }
- while (u8g.nextPage());
- Serial.println("BMP085 sensor, ERROR!");
- while (1) {}
- }
- }
- void loop(void)
- {
- BmpSensorRead(&pressure, &tempC, &altitude);
- DisplayPresTemp(&pressure, &tempC, &altitude);
- delay(1000);
- }
- void DisplayPresTemp(float* pressure, float* tempC, float* altitude)
- {
- u8g.firstPage();
- do
- {
- u8g.setFont(u8g_font_fub11);
- u8g.setFontRefHeightExtendedText();
- u8g.setDefaultForegroundColor();
- u8g.setFontPosTop();
- u8g.drawStr(2, 0, "Pressure");
- u8g.setPrintPos(75, 0);
- u8g.print(*pressure);
- u8g.drawStr(4, 20, "Temp C");
- u8g.setPrintPos(75, 20);
- u8g.print(*tempC);
- u8g.drawStr(4, 40, "Altitude");
- u8g.setPrintPos(75, 40);
- u8g.print(*altitude);
- }
- while (u8g.nextPage());
- }
- void BmpSensorRead(float* pressure, float* tempC, float* altitude)
- {
- *tempC = bmp.readTemperature();
- Serial.print("Temperature = ");
- Serial.print(*tempC);
- Serial.println(" *C");
- *pressure = bmp.readPressure() / 100.0;
- Serial.print("Pressure = ");
- Serial.print(*pressure / 100.0);
- Serial.println(" hPa");
- // Calculate altitude assuming 'standard' barometric
- // pressure of 1013.25 millibar = 101325 Pascal
- *altitude = bmp.readAltitude();
- Serial.print("Altitude = ");
- Serial.print(*altitude);
- Serial.println(" meters");
- }
复制代码
上传代码后,您应该会看到下图所示:
|