🛠 Lire température et gaz avec le BME688

Arduino Nicla Sense ME · debutant · Schéma : IMG-nicla-sense-me-debutant-3-niclasense-lecture-bme688.svg — Nicla Sense ME en gros plan avec les puces BME688 et BHI260AP mises en évidence et flèches vers un petit graphique température/gaz affiché sur l'ordinateur en série

Objectives

Read environment sensors from the Nicla Sense ME via the Arduino_BHY2 library. An important point, which is a source of many errors on forums and third-party datasheets: this board does not embed a BME680 but a BME688 (U6), a new-generation Bosch gas-sensitive sensor (eNose) that also measures temperature, humidity, and pressure; it is not read directly via I²C bit-banging as on a classic shield, but served by the BHI260AP sensor hub, which acts as the bus master. Therefore, we use virtual sensors identified by SENSOR_ID_TEMP (ID 128) and SENSOR_ID_GAS (ID 131), and the message pump BHY2.update, which must be called continuously.

Hardware

Complete Code

#include "Arduino.h"
#include "Arduino_BHY2.h"

Sensor temp(SENSOR_ID_TEMP);   // ID 128 : temperature du BME688
Sensor gas(SENSOR_ID_GAS);     // ID 131 : resistance gaz du BME688

void setup {
  Serial.begin(115200);
  while (!Serial);
  BHY2.begin;    // demarre l'interface avec le hub BHI260AP
  temp.begin;
  gas.begin;
}

void loop {
  static auto printTime = millis;
  BHY2.update;   // OBLIGATOIRE : pompe les donnees du hub
  if (millis - printTime >= 1000) {
    printTime = millis;
    if (temp.dataAvailable) {
      Serial.println(String("Temperature : ") + String(temp.value, 2) + " C");
      temp.clearDataAvailFlag;
    }
    if (gas.dataAvailable) {
      Serial.println(String("Gaz : ") + String(gas.value, 0) + " ohms");
      gas.clearDataAvailFlag;
    }
  }
}

Expected Verification

One temperature line and one gas resistance line are displayed every second. Blow on the board or bring it close to a heat source: the temperature changes within a few seconds. The gas resistance takes several minutes to stabilize upon startup (conditioning of the metal oxide layer, typical measurement cycle 10.8 s); very high values at the beginning are normal.

Common Errors

Other tutorials for this board

Code examples for this board

← Back to the board 🛠 All tutorials