Arduino BMP280

本文讲述了如何在ESP32S3开发板上连接并配置BMP280模块,包括硬件连接、I2C引脚设置,以及使用Adafruit_BMP280库进行初始化和获取温度、压力数据。作者还提到海拔数据显示异常的问题和解决办法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

就是想简单读取一下BMP280

硬件设备

ESP32S3
BMP280在这里插入图片描述

硬件连接

在这里插入图片描述
上图BMP280模块的电路图,CSB是选择通信模式引脚,CSB上拉就是选择I2C模式。SDO是I2C的设备地址选择引脚,SDO上拉是0x77,SDO下拉是0x76
由图可知模块本身CSB上拉,SDO下拉,这边直接采用这种方式。CSB和SDO引脚不接,直接由内部电路决定,就接GND,VDD,SDI(SDA),SCK(SCL)四个引脚

GND,VDD两个引脚接到ESP32S3的GND和3.3V比较好找,但SDA和SCL接那2个脚呢?
参考
这个问题本来的思路是从代码中找找设置
Arduino连接代码就一句

status = bmp.begin();

转到Adafruit_BMP280.h

 bool begin(uint8_t addr = BMP280_ADDRESS, uint8_t chipid = BMP280_CHIPID);
 这里没有关于sda和scl的消息

转到Adafruit_BMP280构造函数

Adafruit_BMP280(TwoWire *theWire = &Wire);
构造函数中有TwoWire类,正好是关于sda和scl引脚定义
class TwoWire: public Stream
{
protected:
    uint8_t num;
    int8_t sda;
    int8_t scl;
    ···
}
Adafruit_BMP280 bmp; // I2C
结果在代码中定义就是这个,采用的是默认值,这个要到哪里找?

找了好久没有找到包含默认定义的文件,后面网上搜寻找到了这个
在这里插入图片描述
在这里插入图片描述
可以在setup中用Wire.begin(SDA,SCL)自定义2个引脚,我这边定义是SDA=42,SCL=41

库文件安装

在这里插入图片描述

代码解析

先放个完整代码

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_BMP280.h>

#define BMP_SCK  (13)
#define BMP_MISO (12)
#define BMP_MOSI (11)
#define BMP_CS   (10)

Adafruit_BMP280 bmp; // I2C
//Adafruit_BMP280 bmp(BMP_CS); // hardware SPI
//Adafruit_BMP280 bmp(BMP_CS, BMP_MOSI, BMP_MISO,  BMP_SCK);

void setup() {
  Wire.begin(42,41);
  Serial.begin(115200);
  while ( !Serial ) delay(100);   // wait for native usb
  Serial.println(F("BMP280 test"));
  unsigned status;
  
  status = bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID);
  // status = bmp.begin();
  if (!status) {
    Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
                      "try a different address!"));
    Serial.print("SensorID was: 0x"); Serial.println(bmp.sensorID(),16);
    Serial.print("        ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
    Serial.print("   ID of 0x56-0x58 represents a BMP 280,\n");
    Serial.print("        ID of 0x60 represents a BME 280.\n");
    Serial.print("        ID of 0x61 represents a BME 680.\n");
    while (1) delay(10);
  }

  /* 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() {
    Serial.print(F("Temperature = "));
    Serial.print(bmp.readTemperature());
    Serial.println(" *C");

    Serial.print(F("Pressure = "));
    Serial.print(bmp.readPressure());
    Serial.println(" Pa");

    Serial.print(F("Approx altitude = "));
    Serial.print(bmp.readAltitude(1013.25)); /* Adjusted to local forecast! */
    Serial.println(" m");

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

代码相对于原来的代码有两处改动

1.增加一个自定义I2C引脚函数
在这里插入图片描述
2.改变bmp.begin(),因为默认设备地址是0x77,而我用的板子地址是0x76
在这里插入图片描述

编译下载查看

在这里插入图片描述
我是在杭州,而且在5楼,正郁闷为什么海拔是负的,杭州整体海拔都有19米,这里只会更加高,胡思乱想今天下午会下雨,现在是冬天。。。
后面看到手机的天气气压,就不胡思乱想了,哈哈😊 【气压计算海拔公式
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值