STM32 Nucleo and BME680 Arduino code example
In this article we will connect a BME680 sensor to an STM32 NUcleo, the code example is written in the Arduino IDE
BME680 is an integrated environmental sensor developed specifically for mobile applications and wearables where size and low power consumption are key requirements. Expanding Bosch Sensortec’s existing family of environmental sensors, the BME680 integrates for the first time high-linearity and high-accuracy gas, pressure, humidity and temperature sensors. The gas sensor within the BME680 can detect a broad range of gases to measure air quality for personal well being.
Gases that can be detected by the BME680 include Volatile Organic Compounds (VOC) from paints (such as formaldehyde), lacquers, paint strippers, cleaning supplies, furnishings, office equipment, glues, adhesives and alcohol.
Parameter | Technical data |
---|---|
Package dimensions | 8-Pin LGA with metal 3.0 x 3.0 x 0.93 mm³ |
Operation range (full accuracy) | Pressure: 300…1100 hPa Humidity 0…100% Temperature: -40…85°C |
Supply voltage VDDIO Supply voltage VDD |
1.2 … 3.6 V 1.71 … 3.6 V |
Interface | I²C and SPI |
Average current consumption (1Hz data refresh rate) |
2.1 µA at 1 Hz humidity and temperature 3.1 µA at 1 Hz pressure and temperature 3.7 µA at 1 Hz humidity, pressure and temperature 0.09‒12 mA for p/h/T/gas depending on operation mode |
Average current consumption in sleep mode | 0.15 μA |
Gas sensor Response time (τ 33-63%) Sensor-to-sensor deviation Power consumption Output data processing |
< 1 s (for new sensors) +/- 15% +/- 15 < 0.1 mA in ultra-low power mode direct output of IAQ: Index for Air Quality |
Humidity sensor Response time (τ0-63%) Accuracy tolerance Hysteresis |
8 s ± 3 % relative humidity ≤ 1.5 % relative humidity |
Pressure sensor RMS Noise Sensitivity Error Temperature coefficient offset |
0.12 Pa (equiv. to 1.7 cm) ± 0.25 % (equiv. to 1 m at 400 m height change) ±1.3 Pa/K (equiv. to ±10.9 cm at 1°C temperature change) |
Parts List
Schematic
We use the I2C connection for the sensor
Code
You will need to import the adafruit sensor and bme680 libraries – you can add these using the library manager
My particular sensor used address 0x76, the default is 0x77 so you may have to change this line from if (!bme.begin(0x76)) to if (!bme.begin())
[codesyntax lang=”cpp”]
#include <Wire.h> #include <Adafruit_Sensor.h> #include "Adafruit_BME680.h" #define SEALEVELPRESSURE_HPA (1013.25) Adafruit_BME680 bme; // I2C void setup() { Serial.begin(9600); while (!Serial); Serial.println(F("BME680 test")); if (!bme.begin(0x76)) { Serial.println("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() { if (! bme.performReading()) { Serial.println("Failed to perform reading :("); return; } Serial.print("Temperature = "); Serial.print(bme.temperature); Serial.println(" *C"); Serial.print("Pressure = "); Serial.print(bme.pressure / 100.0); Serial.println(" hPa"); Serial.print("Humidity = "); Serial.print(bme.humidity); Serial.println(" %"); Serial.print("Gas = "); Serial.print(bme.gas_resistance / 1000.0); Serial.println(" KOhms"); Serial.print("Approx. Altitude = "); Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA)); Serial.println(" m"); Serial.println(); delay(2000); }
[/codesyntax]
Output
Open the serial monitor and you will see something like this
BME680 test
Temperature = 20.52 *C
Pressure = 1001.15 hPa
Humidity = 37.45 %
Gas = 0.00 KOhms
Approx. Altitude = 101.40 m
Temperature = 20.80 *C
Pressure = 1001.13 hPa
Humidity = 37.33 %
Gas = 5.47 KOhms
Approx. Altitude = 101.57 m