ESP8266 NodeMCU and BME680 Sensor Tutorial: Read Gas, Pressure, Humidity, Temp

by AKP in Circuits > Arduino

187 Views, 0 Favorites, 0 Comments

ESP8266 NodeMCU and BME680 Sensor Tutorial: Read Gas, Pressure, Humidity, Temp

esp8266-nodemcu-bme680-sensor-arduino.png

The BME680 is a digital sensor that measures gas, pressure, humidity, and temperature. This guide will show you how to use the BME680 sensor module with the ESP8266 NodeMCU board using the  Arduino IDE. The sensor communicates with the microcontroller via I2C or SPI protocols.

You will learn how to connect the sensor to the ESP8266 NodeMCU board, install the necessary libraries, use a simple sketch to display sensor readings in the Serial Monitor, and set up a web server to monitor the sensor remotely.

Supplies

ESP8266 NodeMCU CP2102 Amazon

BME680 Digital Temperature Humidity Pressure Sensor Amazon

Introducing the BME680 Environmental Sensor Module

BME680-Gas-sensor-humidity-barometric-pressure-ambient-temperature-gas-air-quality-front.jpg
BME680-Gas-Sensor-Resistance-How-It-Works.jpg

The BME680 integrates gas, pressure, humidity, and temperature sensors. Its gas sensor can detect various volatile organic compounds (VOCs), making it suitable for indoor air quality monitoring.

BME680 Capabilities

The BME680 is a multi-functional digital sensor that measures:

  • Temperature
  • Humidity
  • Barometric pressure
  • Gas: Volatile Organic Compounds (VOCs) like ethanol and carbon monoxide

Gas Sensor Details

The BME680 features a MOX (Metal-oxide) sensor that detects VOCs in the air. This sensor provides a qualitative indication of the total VOCs/contaminants in the environment, though it does not identify specific gases.

MOX sensors have a metal-oxide surface, a sensing chip to measure conductivity changes, and a heater. They detect VOCs by adsorbing oxygen molecules on the sensitive layer. The BME680 reacts to most indoor air pollutants (excluding CO2).

When the sensor encounters reducing gases, the oxygen molecules react, increasing surface conductivity. The BME680 outputs resistance values as raw signals, which vary with VOC concentration:

  • Higher VOC concentration → Lower resistance
  • Lower VOC concentration → Higher resistance

The sensor’s surface reactions (and thus resistance) are also influenced by temperature and humidity.

Important Information About the Gas Sensor

The gas sensor provides a qualitative assessment of VOC levels in the air, allowing you to track trends and compare results to determine if air quality is improving or worsening. For precise measurements, calibrate the sensor against known sources and create a calibration curve.

When first using the sensor, run it for 48 hours before collecting accurate data. Additionally, it is recommended to run the sensor for 30 minutes before taking gas readings.

BME680 Pinout

BME680-Gas-sensor-humidity-barometric-pressure-ambient-temperature-gas-air-quality-back.jpg

Here’s the BME680 Pinout:

VCC: Powers the sensor

GND: Common GND

SCL: SCL pin for I2C communication / SCK pin for SPI communication

SDA: SDA pin for I2C communication / SDI (MISO) pin for SPI communication

SDO: SDO (MOSI) pin for SPI communication

CS: Chip select pin for SPI communication

Schematic – ESP8266 NodeMCU With BME680

ESP8266-BME680-Environmental-Sensor-Wiring-Diagram-I2C.jpg
ESP8266-BME680-Environmental-Sensor-Wiring-Diagram-SPI.jpg

The BME680 sensor can use either I2C or SPI communication protocols.

ESP8266 with BME680 using I2C

To connect the BME680 to the ESP8266 using the default I2C pins, follow the schematic diagram below.

ESP8266 with BME680 using SPI

If you prefer to use the SPI communication protocol, follow the corresponding schematic diagram to connect the BME680 to the ESP8266 using the default SPI pins.

Code – Reading BME680 Gas, Pressure, Humidity and Temperature

To read gas, pressure, temperature, and humidity from the BME680, we will use an example sketch provided by the library.

After installing the BME680 and Adafruit_Sensor libraries, open the  Arduino IDE and navigate to File > Examples > Adafruit BME680 Library > bme680async.

Here’s the code to get you started:

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

/*#define BME_SCK 14
#define BME_MISO 12
#define BME_MOSI 13
#define BME_CS 15*/

#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);
}

See Full Article Here