• Cảm biến áp xuất - Nhiệt độ - Độ ẩm BME680 Arduino.

QC

Cảm biến áp xuất - Nhiệt độ - Độ ẩm BME680 Arduino.

 KẾT NỐI :



Installing the BME680 Library

Sketch Include Library > Manage Libraries

 “adafruit bme680 ”


Installing the Adafruit_Sensor Library

Sketch Include Library > Manage Libraries and  Gõ “Adafruit Unified Sensor















CODE :

File > Examples > Adafruit BME680 Library > bme680async.

#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BME680.h"

/*#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10*/

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME680 bme; // I2C
//Adafruit_BME680 bme(BME_CS); // hardware SPI
//Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK);

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println(F("BME680 async test"));

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

  // Set up oversampling and filter initialization
  bme.setTemperatureOversampling(BME680_OS_8X);
  bme.setHumidityOversampling(BME680_OS_2X);
  bme.setPressureOversampling(BME680_OS_4X);
  bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme.setGasHeater(320, 150); // 320*C for 150 ms
}

void loop() {
  // Tell BME680 to begin measurement.
  unsigned long endTime = bme.beginReading();
  if (endTime == 0) {
    Serial.println(F("Failed to begin reading :("));
    return;
  }
  Serial.print(F("Reading started at "));
  Serial.print(millis());
  Serial.print(F(" and will finish at "));
  Serial.println(endTime);

  Serial.println(F("You can do other work during BME680 measurement."));
  delay(50); // This represents parallel work.
  // There's no need to delay() until millis() >= endTime: bme.endReading()
  // takes care of that. It's okay for parallel work to take longer than
  // BME680's measurement time.

  // Obtain measurement results from BME680. Note that this operation isn't
  // instantaneous even if milli() >= endTime due to I2C/SPI latency.
  if (!bme.endReading()) {
    Serial.println(F("Failed to complete reading :("));
    return;
  }
  Serial.print(F("Reading completed at "));
  Serial.println(millis());

  Serial.print(F("Temperature = "));
  Serial.print(bme.temperature);
  Serial.println(F(" *C"));

  Serial.print(F("Pressure = "));
  Serial.print(bme.pressure / 100.0);
  Serial.println(F(" hPa"));

  Serial.print(F("Humidity = "));
  Serial.print(bme.humidity);
  Serial.println(F(" %"));

  Serial.print(F("Gas = "));
  Serial.print(bme.gas_resistance / 1000.0);
  Serial.println(F(" KOhms"));

  Serial.print(F("Approx. Altitude = "));
  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(F(" m"));

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


How the Code Works ( code hoạt động).

Libraries

#include <Wire.h> #include <SPI.h> #include <Adafruit_Sensor.h> #include "Adafruit_BME680.h"

SPI communication

  Chúng tôi thích sử dụng giao thức truyền thông I2C với cảm biến. Tuy nhiên, mã được chuẩn bị nếu bạn muốn sử dụng SPI. Bạn chỉ cần bỏ ghi chú các dòng mã sau để xác định các chân SPI.

/*#define BME_SCK 13 #define BME_MISO 12 #define BME_MOSI 11 #define BME_CS 10*/

Sea level pressure

#define SEALEVELPRESSURE_HPA (1013,25)
Biến này tiết kiệm áp suất ở mực nước biển tính bằng hectopascal (tương đương với milibar). Biến này được sử dụng để ước tính độ cao cho một áp suất nhất định bằng cách so sánh nó với áp suất mực nước biển. Ví dụ này sử dụng giá trị mặc định, nhưng để có kết quả chính xác, hãy thay thế giá trị bằng áp suất mực nước biển hiện tại tại vị trí của bạn.

I2C

Ví dụ này sử dụng giao thức truyền thông I2C theo mặc định. Dòng sau tạo một đối tượng Adafruit_BME680 được gọi là bme trên các chân Arduino I2C: D5 (SCL), D4 (SDA).
Adafruit_BME680 bme; // I2C

Để sử dụng SPI, bạn cần chú thích dòng trước này và bỏ chú thích dòng sau.

//Adafruit_BME680 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI

setup()

In the setup() start a serial communications ( khởi tạo giao tiếp ).

Serial.begin(115200);

Init BME680 Sensor

Initialize the BME680 sensor: ( Khởi tạo cảm biến ).

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

Thiết lập các thông số sau (lấy mẫu quá mức, bộ lọc và bộ gia nhiệt khí) cho cảm biến.

// Set up oversampling and filter initialization bme.setTemperatureOversampling(BME680_OS_8X); bme.setHumidityOversampling(BME680_OS_2X); bme.setPressureOversampling(BME680_OS_4X); bme.setIIRFilterSize(BME680_FILTER_SIZE_3); bme.setGasHeater(320, 150); // 320*C for 150 ms

Để tăng độ phân giải của dữ liệu cảm biến thô, nó hỗ trợ lấy mẫu quá mức. Chúng tôi sẽ sử dụng các thông số lấy mẫu quá mức mặc định, nhưng bạn có thể thay đổi chúng.

  • setTemperatureOversampling(): set temperature oversampling.
  • setHumidityOversampling(): set humidity oversampling.
  • setPressureOversampling(): set pressure oversampling.

Các phương thức này có thể chấp nhận một trong các tham số sau:

  • BME680_OS_NONE: turn off reading;
  • BME680_OS_1X
  • BME680_OS_2X
  • BME680_OS_4X
  • BME680_OS_8X
  • BME680_OS_16X

Cảm biến BME680 tích hợp bộ lọc IIR bên trong để giảm những thay đổi ngắn hạn trong giá trị đầu ra của cảm biến do nhiễu bên ngoài gây ra. Phương thức setIIRFilterSize () đặt bộ lọc IIR. Nó chấp nhận kích thước bộ lọc dưới dạng tham số:

  • BME680_FILTER_SIZE_0 (no filtering)
  • BME680_FILTER_SIZE_1
  • BME680_FILTER_SIZE_3
  • BME680_FILTER_SIZE_7
  • BME680_FILTER_SIZE_15
  • BME680_FILTER_SIZE_31
  • BME680_FILTER_SIZE_63
  • BME680_FILTER_SIZE_127

Cảm biến gas tích hợp lò sưởi. Đặt cấu hình bộ gia nhiệt bằng phương thức setGasHeater () chấp nhận làm đối số:

nhiệt độ lò sưởi (tính bằng độ C)

thời gian máy sưởi sẽ được bật (tính bằng mili giây)

Chúng tôi sẽ sử dụng cài đặt mặc định: 320 ºC trong 150 mili giây.

loop()

Trong vòng lặp (), chúng tôi sẽ nhận được các phép đo từ cảm biến BME680.

Đầu tiên, yêu cầu cảm biến bắt đầu đọc không đồng bộ bằng bme.beginReading (). Điều này trả về thời gian khi bài đọc sẽ sẵn sàng.

// Tell BME680 to begin measurement. unsigned long endTime = bme.beginReading(); if (endTime == 0) { Serial.println(F("Failed to begin reading :(")); return; } Serial.print(F("Reading started at ")); Serial.print(millis()); Serial.print(F(" and will finish at ")); Serial.println(endTime);

Sau đó, gọi phương thức endReading () để kết thúc quá trình đọc không đồng bộ. Nếu quá trình đọc không đồng bộ vẫn đang diễn ra, hãy chặn cho đến khi quá trình này kết thúc.

if (!bme.endReading()) { Serial.println(F("Failed to complete reading :(")); return; }

Sau đó, chúng ta có thể nhận được các bài đọc như sau:

  • bme.temperature: returns temperature reading
  • bme.pressure: returns pressure reading
  • bme.humidity: returns humidity reading
  • bme.gas_resistance: returns gas resistance

Serial.print(F("Temperature = ")); Serial.print(bme.temperature); Serial.println(F(" *C")); Serial.print(F("Pressure = ")); Serial.print(bme.pressure / 100.0); Serial.println(F(" hPa")); Serial.print(F("Humidity = ")); Serial.print(bme.humidity); Serial.println(F(" %")); Serial.print(F("Gas = ")); Serial.print(bme.gas_resistance / 1000.0); Serial.println(F(" KOhms")); Serial.print(F("Approx. Altitude = ")); Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); Serial.println(F(" m"));





Nap Code vào PY32F003 dùng Stlink

 Nap Code vào PY32F003 dùng Stlink Bước 1: Cài đặt  KeilC v5.39 theo link sau ( chú ý 5.39 keil c mới nạp ok). https://edge07.111.ir.cdn.ir/...