BMP280氣壓,溫度模組-ESP32
tags: esp32
bmp280
**_BMP280,該傳感器可以非常準確地測量氣壓和溫度。
您可以使用 I2C 或 SPI 連接協議將其與您的 Arduino 板連接。它有一個 3.3V 穩壓器和電平轉換,因此您可以毫無問題地將它與 3V 或 5V 邏輯微控制器一起使用。_**
接線
*網路上很多教學都沒有說SDO要接VCC,因為我一開始是
沒有接,發現一直顯示找不到Addr,後來查一下國外有人說
接上去才會找的到位址。
| BME280 | ESP32 |
| SDO–>VCC|
|
| VCC———-| 3.3V |
| GND———| GND |
| SCL———-| GPIO 22 |
| SDA———| GPIO 21 |
There are other versions of this sensor that can use either SPI or I2C communication protocols, like the module shown in the next figure:
下載函式庫
將上面兩個檔解壓後,整個資料夾放入Arduino的library裡
打開Arduino IDE–>檔案 > 範例> Adafruit BME280 library > bme280 test
將此程式碼上傳
#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() {
Serial.begin(9600);
Serial.println(F("BMP280 test"));
//if (!bmp.begin(BMP280_ADDRESS_ALT, BMP280_CHIPID)) {
if (!bmp.begin()) {
Serial.println(F("Could not find a valid BMP280 sensor, check wiring or "
"try a different address!"));
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);
}