第二十六篇、基于Arduino uno,获取BMP280大气压强传感器的温度、大气压强和海拔信息——结果导向

0、结果

说明:先来看看串口调试助手显示的结果,第一个值是温度值,第二个值是大气压强,单位是百帕,第三个值是当前的海拔高度,如果是你想要的,可以接着往下看。


1、外观

 说明:虽然BMP280大气压强传感器形态各异,但是原理和代码都是适用的。


2、连线

 说明:只需要连接四根线。

  • uno————BMP280大气压强传感器
  •     3.3V--------------VCC
  •    GND--------------GND
  •     SCL--------------SCL
  •    SDA--------------SDA

3、源程序

说明:采用非阻塞方式编写,一定时间检测一次温度、大气压强和海拔信息,并将对应功能进行函数化,方便移植。

/****************************************bmp280 part****************************************/
/*
  wiring:
  3.3V------VCC
   GND------GND
   SCL------SCL
   SDA------SDA
*/
#include <Adafruit_BMP280.h>                                                //include library

Adafruit_BMP280 bme;                                                        //Instantiate object

#define bmp280TimeInterval 1000                                             //Detect the time interval of a trip
unsigned long bmp280Times = 0;                                              //Record the device running time

float bmp280Temp, bmp280Press, bmp280Altitude;                              //They are temperature, pressure and altitude
/****************************************set up and loop part*********************************/
void setup() {
  Serial.begin(9600);                                                       //Example Set the baud rate of the serial port to 9600

  bme.begin(0x76);                                                          //Example Initialize the IIC address of BMP280

  Serial.println("Go online!");
}
void loop() {
  getBmp280Data();                                                          //Example Obtain data about BMP280
}
/****************************************bmp280 part****************************************/
/*Example Obtain data about BMP280*/
void getBmp280Data() {
  if (millis() - bmp280Times >= bmp280TimeInterval) {                       //This command is executed once in a while
    bmp280Times = millis();

    bmp280Temp = bme.readTemperature();                                     //Acquired temperature
    bmp280Press = bme.readPressure() / 100;                                 //Get the air pressure, hpa
    bmp280Altitude = bme.readAltitude(1013.25);                             //Elevation acquisition   this should be adjusted to your local forcase

    Serial.print("Temp: ");                                                 // The serial port displays the corresponding value
    Serial.print(bmp280Temp);                                               // The serial port displays the corresponding value
    Serial.print(" (C) , ");                                                // The serial port displays the corresponding value

    Serial.print("Press: ");                                                // The serial port displays the corresponding value
    Serial.print(bmp280Press);                                              // The serial port displays the corresponding value
    Serial.print(" (hPa) , ");                                              // The serial port displays the corresponding value

    Serial.print("Altitude: ");                                             // The serial port displays the corresponding value
    Serial.print(bmp280Altitude);                                           // The serial port displays the corresponding value
    Serial.println(" (m).");                                                // The serial port displays the corresponding value
  }
}

4、注意事项

说明:需要下载对应的库文件才不会编译报错。大气压强单位我转化成了百帕。如果求海拔高度不是很准,需要更改bme.readAltitude(1013.25)中的数字,根据当地的压强来改就行了,但实际也不用改,大差不差。导电物体例如手,最好不要碰到传感器,不然会造成短路,损坏传感器。

5、基本原理

        热电效应:BMP280传感器内部具有一种PTAT(Proportional To Absolute Temperature)温度传感器和一种CTAT(Complementary To Absolute Temperature)温度传感器。当传感器发生温度变化时,PTAT和CTAT产生的热电效应就会引起输出电压的变化,从而实现温度测量。
        压阻效应:BMP280传感器内部还安装了一个高精度的压阻元件,用来测量大气压强。当外界气压变化时,压阻元件的电阻值也相应改变,从而通过外部电路转化为电压信号输出。
        信号处理:BMP280传感器内部的芯片会对PTAT、CTAT和压阻元件产生的电压信号进行采样和处理,以获得更加高精度和稳定的温度和气压值。
        数据输出:BMP280传感器可以通过I2C或SPI接口与Arduino等控制器进行通信,将测量到的温度和气压值以数字信号的形式输出给控制器,从而实现对大气压强的测量和监控。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
BMP280是一种常用的数字温度和气压传感器,可以通过I2C或SPI接口与Arduino通信。使用BMP280传感器,你可以轻松地测量大气压力、温度海拔高度。而OLEDD显示器则可以用于显示这些测量数据。 下面是基本的步骤: 1. 连接BMP280传感器Arduino板上的I2C或SPI接口。 2. 下载并安装BMP280库。 3. 使用库中的函数读取BMP280传感器数据,并将其存储在变量中。 4. 连接OLEDD显示器,并使用库中的函数将数据显示在屏幕上。 这里是一个简单的代码示例: ``` #include <Wire.h> #include <Adafruit_BMP280.h> #include <Adafruit_SSD1306.h> #define BMP_SCK 13 #define BMP_MISO 12 #define BMP_MOSI 11 #define BMP_CS 10 #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 32 Adafruit_BMP280 bmp(BMP_CS); // 初始化BMP280传感器 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // 初始化OLEDD显示屏 void setup() { Serial.begin(9600); if (!bmp.begin()) { Serial.println("Could not find a valid BMP280 sensor, check wiring!"); while (1); } display.begin(SSD1306_SWITCHCAPVCC, 0x3C); } void loop() { float temperature = bmp.readTemperature(); // 读取温度 float pressure = bmp.readPressure() / 100.0F; // 读取气压 float altitude = bmp.readAltitude(1013.25); // 读取海拔高度 display.clearDisplay(); display.setTextSize(2); display.setTextColor(WHITE); display.setCursor(0,0); display.print("Temp: "); display.print(temperature); display.println("C"); display.print("Pressure: "); display.print(pressure); display.println("hPa"); display.print("Altitude: "); display.print(altitude); display.println("m"); display.display(); delay(2000); } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值