Arduino atmospheric pressure sensor BMP280 experiment

hardware preparation

Arduino Uno
insert image description here(Arduino is not limited and suitable for Mege2560, nano, etc.)
BMP280 atmospheric pressure sensor
insert image description here

Wiring part

BMP280 pin display
insert image description here

Arduino BMP280
3.3V VCC
GND GND
13 SCL
12 SDA
11 CSB
10 SDO

Notice! ! ! Do not connect to 5V , so as not to burn out the BMP280

code section

BMP280 library file configuration

Download the BMP280 library file in the Arduino Management Library
insert image description here

code section

Define pin usage (different models, or you can modify the pin definition if necessary)

/*定义BMP280引脚*/
#define BMP_SCK 13   //SCL引脚 
#define BMP_MISO 10   //SDO引脚
#define BMP_MOSI 12   //SDA引脚
#define BMP_CS 11     //CSB引脚

Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);  

BMP280 atmospheric pressure sensor has an initialization detection SPI communication step

void setup() {
    
    
  Serial.begin(9600);     //设置波特率
  Serial.println(F("BMP280传感器初始化检测"));
  Serial.println(F("BMP280传感器检测成功"));
  /*检测SPI总线通讯*/
  if (!bmp.begin()) {
    
      
    Serial.println(F("BMP280传感器初始化失败"));
    while (1);
  }
}

Atmospheric pressure serial port reading calculation

Serial.print(F("当前海拔高度 = "));
    Serial.print(bmp.readAltitude(1013.25));
    Serial.println(" M");   

full code

#include <Wire.h>     
#include <SPI.h>      //SPI总线库

#include <Adafruit_Sensor.h>  
#include <Adafruit_BMP280.h>      //BMP280库

/*定义BMP280引脚*/
#define BMP_SCK 13   //SCL引脚 
#define BMP_MISO 10   //SDO引脚
#define BMP_MOSI 12   //SDA引脚
#define BMP_CS 11     //CSB引脚

Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);  

/*传感器初始化检测*/
void setup() {
    
    
  Serial.begin(9600);     //设置波特率
  Serial.println(F("BMP280传感器初始化检测"));
  Serial.println(F("BMP280传感器检测成功"));
  /*检测SPI总线通讯*/
  if (!bmp.begin()) {
    
      
    Serial.println(F("BMP280传感器初始化失败"));
    while (1);
  }
}

void loop() {
    
    
    Serial.print(F("当前温度:"));
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");    
    Serial.print(F("当前气压值 = "));
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");
    Serial.print(F("当前海拔高度 = "));
    Serial.print(bmp.readAltitude(1013.25));
    Serial.println(" M");   
    delay(800);     //延时检测
}

Open the serial monitor. Set the baud rate to 9600, and you can view the current detection data
insert image description here
( the above data is queried according to the current Baidu map, which is close to the match. The place where I live is the plateau area )

Good luck! ! !

Guess you like

Origin blog.csdn.net/weixin_50679163/article/details/119636651