BME280 – Measure air pressure, humidity, and temperature

FunduinoPublished on

Umgebungswerte auslesen – vom BME280 bis zur Luftqualität mit dem BME680

Aufgabe: Der BME280 soll Temperatur, Luftdruck und Luftfeuchtigkeit der Umgebung messen und die Werte im Seriellen Monitor anzeigen. Im zweiten Teil dieser Anleitung lernst du den größeren Bruder kennen: Der BME680 misst zusätzlich die Luftqualität, und du gibst seine Werte wahlweise im Seriellen Monitor, auf einem LCD oder als Webseite aus.

Wiring diagram with the BME280 connected to the Arduino UNO: VCC to 3.3V, GND to GND, SCL to A5, SDA to A4
Circuit diagram: BME280 on the Arduino UNO
Four-line LCD with temperature, humidity, IAQ, and estimated CO2 levels
The readings on the LCD
1 / 2

You need this

GY-BME280 sensor module - air pressure, humidity, temperature, I2C GY-BME280 sensor module - air pressure, humidity, temperature, I2C 10,04 € *Item no. F23106637About the product — GY-BME280 sensor module - air pressure, humidity, temperature, I2C
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard cables male-female - 40 pieces, 20 cm Breadboard cables male-female - 40 pieces, 20 cm 1,74 € *Item no. FUN-1010554About the product — Breadboard cables male-female - 40 pieces, 20 cm

All 4 parts together 25,80 € *

* All prices include statutory VAT, plus shipping costs.

Choose a microcontroller

Pins, schematic, and sketch in the steps change with your selection. It remains saved.

Connect the BME280

The BME280 has six pins. For this task, you’ll use four of them:

  • VCC – Power supply pin, connected to the Arduino’s 3.3V pin
  • GND – Ground pin, connected to the Arduino’s GND pin
  • SCL – Serial Clock, together with SDA the signal line of the I²C bus, connected to the Arduino’s A5 pin
  • SDA – Serial Data, together with SCL the signal line of the I²C bus, connected to the Arduino’s A4 pin

You’ll also find two more pins that you won’t use in this task:

  • SDO – Pin for setting the hexadecimal encoding. The standard I²C address is 0x77; if you connect SDO to a ground pin, the address changes to 0x76.
  • CSB – Chip Select

IMPORTANT: The sensor operates at 3.3 V. A power supply above approximately 3.6 V can permanently damage the module. In addition, vibrations can affect the BME280's readings.

Wiring diagram with the BME280 connected to the Arduino UNO: VCC to 3.3V, GND to GND, SCL to A5, SDA to A4
Circuit diagram: BME280 on the Arduino UNO

Install the libraries for the BME280

Once the wiring is complete and you’ve checked your work again, let’s take a look at the sketch.

Please note that the sensor requires both the I²C library Wire.h and the separate Seeed_BME280.h library. Wire.h is already included with the Arduino software. You’ll need to install Seeed_BME280.h manually—you can find it in the Library Manager by searching for “Grove – Barometer Sensor BME280”.

You can find detailed installation instructions at Adding Libraries to the Arduino Software.

Read the BME280

This sketch outputs the BME280 temperature, air pressure (in pascals and bar), and humidity to the Serial Monitor every second.

Program code – read BME280
#include <Seeed_BME280.h>  // Include the Seeed library, which enables communication with the BME280
#include <Wire.h>          // Include the Wire library, which allows the Arduino to communicate with devices that use the I²C protocol
BME280 bme280;
void setup()
{
  Serial.begin(9600);  // Set the baud rate
  if (!bme280.init())  // If no data can be retrieved from the BME280 ...
  {
    Serial.println("FEHLER!");  // ... then an error message should be displayed.
  }
}
void loop()  // Start our loop
{
  float Pascal;  // Declare the variables
  double Bar;
  Serial.print("Temperature : ");
  Serial.print(bme280.getTemperature());  // Retrieve and display the temperature
  Serial.println("C");
  Serial.print("Pressure : ");
  Pascal = bme280.getPressure();  // Retrieve the pressure in pascals
  Serial.print(Pascal);  // Display the pressure in pascals
  Serial.print(" pascals, or ");
  Bar = (Pascal / 100000);  // Convert the value from pascals to bar
  Serial.print(Bar);  // Display the pressure in bar
  Serial.println(" bar");
  Serial.print("Humidity : ");
  Serial.print(bme280.getHumidity());  // Retrieve and display the humidity
  Serial.println("%");
  Serial.println(" ");
  delay(1000);
}

The BME680 – what else the sensor can do

The Bosch BME680 sensor measures temperature, humidity, and air pressure. It also contains a MOX sensor (metal oxide): Depending on the concentration of volatile organic compounds (VOC) in the air, the heated metal oxide changes its resistance. Air quality is initially measured as resistance in ohms. The higher the VOC concentration, the lower the resistance.

Many materials indoors release gases:

  • Cleaning and care products
  • Furniture and furnishings
  • Carpets and wallpaper
  • Building materials (paints)
  • Electronic devices (computers, printers)
  • Heating appliances (stoves, ovens)
  • Human breath and perspiration

The MOX sensor takes about 30 minutes to calibrate. To get a baseline for air quality, it’s best to calibrate it outdoors or in a well-ventilated room. You can then use this value as a reference for good air quality.

Read the BME680 using the Adafruit library

The Serial Monitor displays the readings for temperature, humidity, air pressure, and air quality. You can also determine the approximate altitude of your location in meters from the ratio of the current air pressure to the air pressure at sea level.

For this part, you need a BME680 and either an UNO R3, a Wemos D1 Mini, an ESP32-WROOM, or a NodeMCU.

Wiring diagram for the BME680 connected to an Arduino UNO via the I²C bus
Schematic: BME680 on Arduino UNO
Serial monitor with temperature, humidity, air pressure, gas resistance, and approximate altitude
Output in the Serial Monitor using the Adafruit library
1 / 2
Program code – BME680 with the Adafruit library
#include "Wire.h"
#include "Adafruit_Sensor.h"
#include "Adafruit_BME680.h"
// Sea-level air pressure
#define SEALEVELPRESSURE_HPA (1013.25)
// I2C connection
Adafruit_BME680 bme;
void setup()
{
  // Start the serial monitor
  Serial.begin(9600);
  while (!Serial);
  delay(1000);
  Serial.println(F("BME680 Test"));
  // Start the BME680
  if (!bme.begin()) {
    Serial.println("BME680 sensor not found!");
    // Stop the program
    while (1);
  }
  /*
    oversampling: Setting a higher sampling rate than the default rate
    possible values:
    BME680_OS_NONE -> disabled
    BME680_OS_1X
    BME680_OS_4X
    BME680_OS_8X
    BME680_OS_16X
    bme.setTemperatureOversampling(BME680_OS_NONE);
    bme.setHumidityOversampling(BME680_OS_2X);
    bme.setPressureOversampling(BME680_OS_4X);
    // IIR = infinite impulse response (filter with infinite impulse response)
    // provides an infinitely long-lasting impulse response
    bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
    bme.setGasHeater(320, 150);
    The default values are sufficient for the measurement
  */
}
void loop()
{
  // if the sensor does not transmit data ...
  if (!bme.performReading())
  {
    Serial.println("No measurement data");
    return;
  }
  Serial.print("Temperature: ");
  // Convert the measured value to a string
  String Temperatur = String(bme.temperature);
  // Replace . with ,
  Temperatur.replace(".", ",");
  Serial.println(Temperatur + " °C");
  String Luftfeuchtigkeit = String(bme.humidity);
  Luftfeuchtigkeit.replace(".", ",");
  Serial.print("Humidity: ");
  Serial.println(Luftfeuchtigkeit + " %");
  Serial.print("Air pressure = ");
  String Luftdruck = String(bme.pressure / 100.0);
  Luftdruck.replace(".", ",");
  Serial.println(Luftdruck + " hPa");
  Serial.print("Gas resistance = ");
  String GasWiderstand = String(bme.gas_resistance / 1000.0);
  Serial.println(GasWiderstand + " KOhm");
  Serial.print("Approximate altitude: ");
  Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
  Serial.println(" m");
  Serial.println();
  delay(2000);
}

BME680 with the BSEC library on the ESP32

Air quality is initially measured as a resistance in ohms. The higher the VOC concentration, the lower the resistance value. An internal algorithm in Bosch’s BSEC software converts this raw value into an air quality value (IAQ = Index for Air Quality).

Of course, these readings aren’t as accurate as those from professional measuring devices. However, they can give you reliable indications of indoor air quality. Temperature, humidity, and air pressure data are available shortly after the sensor is activated. The MOX sensor can take up to 30 minutes to display its first readings. Stable and reliable readings are only ensured after several days. The IAQ accuracy value must reach 3 for this.

Preparation: Install the board via the Board Manager ESP32-Wroom and then install the BSEC library via Sketch → Include Library → Manage Libraries.

You absolutely need an ESP32 controller for this part - the BSEC library does not run on the UNO R3.

Serial monitor with status messages and readings, including IAQ and estimated CO2 levels
Output in the Serial Monitor using the BSEC library
Table of IAQ value ranges and the corresponding air quality assessment
IAQ index and air quality rating (source: aqicn.org/scale/de/)
Schematic with BME680 connected to the ESP32-Wroom via the I²C bus
Schematic: BME680 on the ESP32
Arduino software library manager with the BSEC library
The BSEC library in the Library Manager
Overview of the BSEC library’s functions and measured parameters
Functions of the BSEC library
1 / 5
BSEC – Required Library and Variable
#include "bsec.h"
// Define an object (iaqSensor) of the Bsec class
Bsec iaqSensor;
BSEC - The setup section
void setup()
{
  Serial.begin(9600);
  // wait for serial connection
  while (!Serial);
  delay(1000);
  // Hex address of the BME680
  // BME68X_I2C_ADDR_HIGH = 0x77 (default)
  // BME68X_I2C_ADDR_LOW = 0x76 (SDO connection to GND)
  iaqSensor.begin(BME68X_I2C_ADDR_HIGH, Wire);
  // Array of available sensors
  bsec_virtual_sensor_t sensorList[13] =
  {
    BSEC_OUTPUT_IAQ,
    BSEC_OUTPUT_STATIC_IAQ,
    BSEC_OUTPUT_CO2_EQUIVALENT,
    BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
    BSEC_OUTPUT_RAW_TEMPERATURE,
    BSEC_OUTPUT_RAW_PRESSURE,
    BSEC_OUTPUT_RAW_HUMIDITY,
    BSEC_OUTPUT_RAW_GAS,
    BSEC_OUTPUT_STABILIZATION_STATUS,
    BSEC_OUTPUT_RUN_IN_STATUS,
    BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
    BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
    BSEC_OUTPUT_GAS_PERCENTAGE
  };
  // BSEC_SAMPLE_RATE_ULP: Ultra Low Power Mode -> measurement every 5 minutes
  // BSEC_SAMPLE_RATE_LP: Low Power Mode -> measurement every 3 seconds
  iaqSensor.updateSubscription(sensorList, 13, BSEC_SAMPLE_RATE_LP);
}
BSEC - The loop section
void loop()
{
  // wenn der Sensor arbeitet ...
  if (iaqSensor.run())
  {
    // Messwerte ermitteln, in Strings umwandeln, . durch , ersetzen
    String Temperatur = String(iaqSensor.temperature);
    Temperatur.replace(".", ",");
    String Luftfeuchtigkeit = String(iaqSensor.humidity);
    Luftfeuchtigkeit.replace(".", ",");
    String Luftdruck = String(iaqSensor.pressure / 100);
    Luftdruck.replace(".", ",");
    String IAQ = String(iaqSensor.iaq);
    IAQ.replace(".", ",");
    String StatischerIAQ = String(iaqSensor.staticIaq);
    StatischerIAQ.replace(".", ",");
    String CO2 = String(iaqSensor.co2Equivalent);
    CO2.replace(".", ",");
    // Ausgabe im Seriellen Monitor
    Serial.println("Statusmeldungen:");
    Serial.println("-----------------------------");
    Serial.println("Sensor bereit (0-1): " + String(int(iaqSensor.runInStatus)));
    Serial.println("Genauigkeit IAQ (0-3): " + String(int(iaqSensor.iaqAccuracy)));
    Serial.println("-----------------------------");
    Serial.println("Messwerte:");
    Serial.println("-----------------------------");
    Serial.println("Temperatur: " + Temperatur + " °C");
    Serial.println("Feuchtigkeit: " + Luftfeuchtigkeit + " %");
    Serial.println("Luftdruck: " + Luftdruck + " hPa");
    Serial.println("IAQ: " + IAQ);
    Serial.println("Statischer IAQ: " + StatischerIAQ);
    Serial.println("Schätzung CO2: " + CO2 + " ppm");
    Serial.println("-----------------------------");
  }
}

Extension: Display the measurement data on an LCD

Instead of only in the Serial Monitor, you can also display the measurements on a four-line I²C LCD. To do this, install the LCDIC2 library via Sketch → Include Library → Manage Libraries.

Four-line LCD with temperature, humidity, IAQ, and estimated CO2 levels
The readings on the LCD
Schematic with BME680 and four-line I²C LCD on ESP32
LCD schematic: red = 5V, black = GND, yellow = SCL, green = SDA
Arduino software library manager with the LCDIC2 library
The LCDIC2 library in the Library Manager
1 / 3
Program code – display measured values on an LCD
#include "bsec.h"
#include "LCDIC2.h"
// 4-zeiliges LCD
LCDIC2 lcd(0x27, 20, 4);
// Objekt (iaqSensor) der Klasse Bsec definieren
Bsec iaqSensor;
void setup()
{
  Serial.begin(9600);
  // auf serielle Verbindung warten
  while (!Serial);
  delay(1000);
  // LCD starten
  lcd.begin();
  // Cursor "verstecken"
  lcd.setCursor(false);
  // Hex-Adresse des BME680
  // BME68X_I2C_ADDR_HIGH = 0x77 (Standard)
  // BME68X_I2C_ADDR_LOW = 0x76 (Verbindung SDO zu GND)
  iaqSensor.begin(BME68X_I2C_ADDR_HIGH, Wire);
  // Array der verfügbaren Sensoren
  bsec_virtual_sensor_t sensorList[13] =
  {
    BSEC_OUTPUT_IAQ,
    BSEC_OUTPUT_STATIC_IAQ,
    BSEC_OUTPUT_CO2_EQUIVALENT,
    BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
    BSEC_OUTPUT_RAW_TEMPERATURE,
    BSEC_OUTPUT_RAW_PRESSURE,
    BSEC_OUTPUT_RAW_HUMIDITY,
    BSEC_OUTPUT_RAW_GAS,
    BSEC_OUTPUT_STABILIZATION_STATUS,
    BSEC_OUTPUT_RUN_IN_STATUS,
    BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
    BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
    BSEC_OUTPUT_GAS_PERCENTAGE
  };
  // BSEC_SAMPLE_RATE_ULP: Ultra Low Power Mode -> Messung alle 5 Minuten
  // BSEC_SAMPLE_RATE_LP: Low Power Mode -> Messung alle 3 Sekunden
  iaqSensor.updateSubscription(sensorList, 13, BSEC_SAMPLE_RATE_LP);
}
void loop()
{
  // wenn der Sensor arbeitet ...
  if (iaqSensor.run())
  {
    // Messwerte ermitteln, in Strings umwandeln, . durch , ersetzen
    String Temperatur = String(iaqSensor.temperature);
    Temperatur.replace(".", ",");
    String Luftfeuchtigkeit = String(iaqSensor.humidity);
    Luftfeuchtigkeit.replace(".", ",");
    String Luftdruck = String(iaqSensor.pressure / 100);
    Luftdruck.replace(".", ",");
    String IAQ = String(iaqSensor.iaq);
    IAQ.replace(".", ",");
    String StatischerIAQ = String(iaqSensor.staticIaq);
    StatischerIAQ.replace(".", ",");
    String CO2 = String(iaqSensor.co2Equivalent);
    CO2.replace(".", ",");
    // Ausgabe im Seriellen Monitor
    Serial.println("Statusmeldungen:");
    Serial.println("-----------------------------");
    Serial.println("Sensor bereit (0-1): " + String(int(iaqSensor.runInStatus)));
    Serial.println("Genauigkeit IAQ (0-3): " + String(int(iaqSensor.iaqAccuracy)));
    Serial.println("-----------------------------");
    Serial.println("Messwerte:");
    Serial.println("-----------------------------");
    Serial.println("Temperatur: " + Temperatur + " °C");
    Serial.println("Feuchtigkeit: " + Luftfeuchtigkeit + " %");
    Serial.println("Luftdruck: " + Luftdruck + " hPa");
    Serial.println("IAQ: " + IAQ);
    Serial.println("Statischer IAQ: " + StatischerIAQ);
    Serial.println("Schätzung CO2: " + CO2 + " ppm");
    Serial.println("-----------------------------");
    // Ausgabe auf dem LCD
    lcd.setCursor(0, 0);
    lcd.print("Temperatur: " + Temperatur + "\337C");
    lcd.setCursor(0, 1);
    lcd.print("Feuchtigkeit: " + Luftfeuchtigkeit + "%");
    lcd.setCursor(0, 2);
    lcd.print("IAQ: " + IAQ);
    lcd.setCursor(0, 3);
    lcd.print("CO2: " + CO2 + " ppm");
  }
}

Extension: Display the measurement data on the web

The ESP32 can also make the readings available as a webpage on the local network. To do this, the sketch starts a small web server, gets the time from an NTP time server, and builds the page with the latest readings and a Refresh button.

The source code for this extension is currently missing.

Browser page with the BME680 readings and a Refresh button
The measured values in the browser
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.