就是想简单读取一下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米,这里只会更加高,胡思乱想今天下午会下雨,现在是冬天。。。
后面看到手机的天气气压,就不胡思乱想了,哈哈😊 【气压计算海拔公式】