【Arduino 连接BMP280:测量温度、压力和高度】

Arduino 连接BMP280:测量温度、压力和高度

介绍

BMP280:测量温度、压力和高度

  • OLED 显示屏,英制和公制单位。
    在这里插入图片描述

组件耗材

元件名 数量
OLED 128x32 i2c 1
Arduino 1
BMP280 1

项目描述

您好,欢迎来到本教程,我在 Arduino UNO 板和 OLED 显示器的帮助下使用 BMP280 测量温度、压力和高度。以下是其数据表中的一些 BMP280 关键参数:
在这里插入图片描述所以对于这个项目,我们将像往常一样使用该模块在公制和英制系统中测量所有这些东西,我不太确定这些单位,但你总是可以通过代码转换它们,这是我们要的组件使用:
Arduino UNO 开发板
在这里插入图片描述
BMP280
在这里插入图片描述128x32 OLED 显示屏(可选)
在这里插入图片描述
测试和故障排除:
连接模块后,最好测试库示例中的代码,其名称为“BMP280test”,如果未检测到模块,如下图所示:
在这里插入图片描述您可以运行 i²c 扫描仪代码(但拔下任何其他 i²c 设备,如 LCD 或 OLED),如果您的模块没有损坏,或者没有焊接问题,您会看到扫描仪检测到该设备:

在这里插入图片描述现在注意地址并转到您的库 c++ 文件,库通常安装在“Documents/Arduino/Libraries”中:
在这里插入图片描述
并用合适的编辑器打开它,我使用的是 Dev c++
在这里插入图片描述然后转到“41”行,_i2caddr 将“a”更改为您在串行监视器上找到的地址:
在这里插入图片描述全部保存并关闭,该库旨在使用具有 0x77 作为 i²c 地址的模块,但我使用的模块具有 0x76。

再次运行测试代码:
在这里插入图片描述
测试图片:
在这里插入图片描述在这里插入图片描述
我希望它对你有用,如果你有任何问题,请在评论中留言。

电路图

在这里插入图片描述

代码

1.测量温度和压力,并将它们与高度一起显示在串行监视器上

/* This code is to use with Adafruit BMP280   (Imperial)
 * It measures   both temperature and pressure and it displays them on the Serial monitor with the   altitude
 * It's a modified version of the Adafruit example code
 */

#include <Adafruit_BMP280.h>

Adafruit_BMP280   bmp; // I2C

void setup() {
    
    
  Serial.begin(9600);
  Serial.println(F("BMP280   test"));

  if (!bmp.begin()) {
    
    
    Serial.println(F("Could not find   a valid BMP280 sensor, check wiring!"));
    while (1);
  }

  /* Default   settings from datasheet. */
  bmp.setSampling(Adafruit_BMP280::MODE_NORMAL,     /*   Operating Mode. */
                  Adafruit_BMP280::SAMPLING_X2,     /* Temp.   oversampling */
                  Adafruit_BMP280::SAMPLING_X16,    /* Pressure   oversampling */
                  Adafruit_BMP280::FILTER_X16,      /* Filtering.   */
                  Adafruit_BMP280::STANDBY_MS_500); /* Standby time. */
}

void   loop() {
    
    
    
    float f = bmp.readTemperature()*9/5 + 32 ; //Conversion   from C to F
    Serial.print(F("Temperature = "));    
    Serial.print(f);
     Serial.println(" *F");
    
    float P = bmp.readPressure()/3386.39;    //displaying the Pressure in inches of mercury, you can change the unit
    Serial.print(F("Pressure   = "));
    Serial.print(P);
    Serial.println(" inHg");

    float   A = bmp.readAltitude(1019.66)*3.28084;   //The "1019.66" is the pressure(hPa)   at sea level in day in your region
    Serial.print(F("Approx altitude = "));         //If you don't know it, modify it until you get your current altitude
     Serial.print(A);
    Serial.println(" ft");

    Serial.println();
     delay(2000);
}

测量温度和压力,并将它们与海拔一起显示在 OLED 显示屏上(公制)

/* This code is to use with Adafruit BMP280 and OLED screen   (Metric)
   * It measures both temperature and pressure and it displays them on the OLED display   with the altitude
 * It's a modified version of the Adafruit example code
 */

#include   <Adafruit_GFX.h>      //Libraries for the OLED and BMP280
#include <Adafruit_SSD1306.h>
#include   <Adafruit_BMP280.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define   SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET    -1 //   Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH,   SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)
Adafruit_BMP280   bmp;

void setup() {
    
      
  bmp.begin();                                //Start   the bmp                  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start   the OLED display
  display.clearDisplay();
  display.display();
  display.setTextColor(WHITE);
   display.setTextSize(1); 
  display.print("SurtrTech");     //Show the name,   you can remove it or replace it
  display.setCursor(32,12);
  display.setTextSize(2);           
  display.println("BMP280"); 
  display.display();
  delay(2000);
}

void   loop() {
    
    

    display.clearDisplay();
    float T = bmp.readTemperature();            //Read temperature in C
    float P = bmp.readPressure()/100;         //Read   Pressure in Pa and conversion to hPa
    float A = bmp.readAltitude(1019.66);       //Calculating the Altitude, the "1019.66" is the pressure in (hPa) at sea   level at day in your region
                                              //If   you don't know it just modify it until you get the altitude of your place
    
     display.setCursor(0,0);                   //Oled display, just playing with   text size and cursor to get the display you want
    display.setTextColor(WHITE);
     display.setTextSize(2); 
    display.print("Temp");
    
    display.setCursor(0,18);
     display.print(T,1);
    display.setCursor(50,17);
    display.setTextSize(1);
     display.print("C");

    display.setTextSize(1);
    display.setCursor(65,0);
     display.print("Pres");
    display.setCursor(65,10);
    display.print(P);
     display.setCursor(110,10);
    display.print("hPa");

    display.setCursor(65,25);
     display.print("Alt");
    display.setCursor(90,25);
    display.print(A,0);
     display.setCursor(110,25);
    display.print("m");
    
    display.display();
     delay(2000);
}

测量温度和压力,并将它们与海拔一起显示在 OLED 显示屏上(英制)

/* This code is to use with Adafruit BMP280 and OLED screen   (Imperial)
   * It measures both temperature and pressure and it displays them on the OLED display   with the altitude
 * It's a modified version of the Adafruit example code
 */
 
#include   <Adafruit_GFX.h>      //Libraries for the OLED and BMP280
#include <Adafruit_SSD1306.h>
#include   <Adafruit_BMP280.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define   SCREEN_HEIGHT 32 // OLED display height, in pixels
#define OLED_RESET    -1 //   Reset pin # (or -1 if sharing Arduino reset pin)

Adafruit_SSD1306 display(SCREEN_WIDTH,   SCREEN_HEIGHT, &Wire, OLED_RESET); //Declaring the display name (display)
Adafruit_BMP280   bmp;

void setup() {
    
      
  bmp.begin();                                //Start   the bmp                  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C); //Start   the OLED display
  display.clearDisplay();
  display.display();
  display.setTextColor(WHITE);
   display.setTextSize(1); 
  display.print("SurtrTech");     //Show the name,   you can remove it or replace it
  display.setCursor(32,12);
  display.setTextSize(2);           
  display.println("BMP280"); 
  display.display();
  delay(2000);
}

void   loop() {
    
    

    display.clearDisplay();
    float T = bmp.readTemperature()*9/5   + 32;    //Read temperature in C and conversion to F
    float P = bmp.readPressure()/3386.39;         //Read Pressure in Pa and conversion to inches of mercury
    float A   = bmp.readAltitude(1019.66)*3.28084; //Calculating the Altitude, the "1019.66"   is the pressure in(hPa) at sea level at day in your region
                                                  //If   you don't know it just modify it until you get the altitude of your place

     display.setCursor(0,0);                       //Oled display, just playing with   text size and cursor to get the display you want
    display.setTextColor(WHITE);
     display.setTextSize(2); 
    display.print("Temp");
    
    display.setCursor(0,18);
     display.print(T,1);
    display.setCursor(50,17);
    display.setTextSize(1);
     display.print("F");

    display.setTextSize(1);
    display.setCursor(65,0);
     display.print("Pres");
    display.setCursor(65,10);
    display.print(P);
     display.setCursor(100,10);
    display.print("inHg");

    display.setCursor(65,25);
     display.print("Alt");
    display.setCursor(90,25);
    display.print(A,0);
     display.setCursor(110,25);
    display.print("ft");
    
    display.display();
     delay(2000);
}

附件

电路,代码,库文件等在个人主页找到,或私信我。

猜你喜欢

转载自blog.csdn.net/m0_46509684/article/details/129036684