Free shipping for orders above 60 €

How to Measure Indoor Air Quality (IAQ) with the BME680

One of the main features of the BME680 sensor is its ability to measure the air quality of indoor environments. That sounds great, until you realize, that the actual output of the BME680 is not a clear, easy-to-understand value like the IAQ-Index, but it delivers instead a resistance value in ohms.

What a bummer! Yeah, I know, the resistance of the internal heating plate of the sensor does vary with the presence of certain substances in the air. However, interpreting the air quality from that resistance value is really not straightforward and subject to interpretation. 

To be fair, the good folks at Bosch Sensortec did not develop the BME680 with the Arduino Uno in mind. The sensor shows its real potential when used together with the BSEC algorithm (Bosch Sensortec Environment Cluster). The BSEC algorithm calculates the IAQ index based not only on measured values (temperature, humidity, pressure, and resistance) but also based on previous measurements.

Developed to run with the powerful processors inside modern smartphones, the BSEC algorithm does not run smoothly on an 8-bit microcontroller (as far as I can tell!). But it does run on the popular ESP8266 WiFi microcontroller. So this is what you will learn here: how to measure indoor air quality (IAQ) with the BME680 and the ESP8266.

Connecting the Arduino IDE to the ESP8266

Just as the ATmega328 is the microcontroller running the Arduino Uno, the ESP8266 microcontroller is the main component of the open-source platform NodeMCU. There are plenty of great resources on the internet about the NodeMCU, so here we will just focus on programming the NodeMCU with the Arduino IDE and using it to read a BME680. OK, so let’s get started:

1.     Open the Arduino IDE (I am using version 1.8.5) and go to File > Preferences. Now copy and paste the following address into the field Additional Boards Manager URLs, as shown in the picture below, and click OK.


http://arduino.esp8266.com/stable/package_esp8266com_index.json

2.     Go to Tools > Boards: > Boards Manager… and search for the esp8266 library by the ESP8266 Community. I am currently using version 2.6.0, but it should probably run just as well with newer versions.

3.     Go to Tools > Boards: > NodeMCU 1.0 board (ESP-12E Module) to connect to the ESP8266.

If you did everything right, then the NodeMCU board info will be shown at the bottom of the Arduino IDE.

Integrating the BSEC Library to the Arduino IDE

4.   The easiest way to install the BSEC library is through the Arduino IDE library manager. Just open the Arduino IDE and go to Sketch > Include Library > Manage Libraries… and search for the BSEC library on the Library Manager. In this example, I am using the BSEC library version 1.6.1.1480. 

5.     Now go up to the following folder:


C:\Users\username\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.6.0

 

6.     Open the file platform.txt.

Yes, this folder is really well hidden. Even more so, because the folder AppData is usually not shown on Windows Explorer. If you are not sure how to show hidden files and folders in Windows (it’s really easy), check out these links from Microsoft: 

View hidden files and folders in Windows 10 (in English) or Anzeigen versteckter Dateien (in German)

7.     Look for the following piece of code on line 82:


# These can be overridden in platform.local.txt compiler.c.extra_flags= compiler.c.elf.extra_flags= compiler.S.extra_flags= compiler.cpp.extra_flags= compiler.ar.extra_flags= compiler.objcopy.eep.extra_flags= compiler.elf2hex.extra_flags=

8.     Did you find it? Good, because we now need to add this little piece of code at the bottom.


compiler.libraries.ldflags=

 

Your code should now look like this (new piece of code highlighted in red):

# These can be overridden in platform.local.txt
compiler.c.extra_flags=
compiler.c.elf.extra_flags=
compiler.S.extra_flags=
compiler.cpp.extra_flags=
compiler.ar.extra_flags=
compiler.objcopy.eep.extra_flags=
compiler.elf2hex.extra_flags=
compiler.libraries.ldflags=

 9.     Now look for the following piece of code on line 112:


## Combine gc-sections, archives, and objects recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {build.exception_flags} -Wl,-Map "-Wl,{build.path}/{build.project_name}.map" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" -Wl,--start-group {object_files} "{archive_file_path}" {compiler.c.elf.libs} -Wl,--end-group "-L{build.path}"

 10.     Close to the end of this line add this little piece of code to it:


{compiler.libraries.ldflags}

 11.     The code should look like this (added code highlighted in red):


## Combine gc-sections, archives, and objects recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" {build.exception_flags} -Wl,-Map "-Wl,{build.path}/{build.project_name}.map" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} -o "{build.path}/{build.project_name}.elf" -Wl,--start-group {object_files} "{archive_file_path}" {compiler.c.elf.libs} {compiler.libraries.ldflags} -Wl,--end-group "-L{build.path}"

12.     That’s it! Don’t forget to save and close the file.

Connecting the BME680 sensor to the NodeMCU board

13.     Now it is time to connect the BME680 sensor to the NodeMCU board. The header pins of the NodeMCU board are already soldered. In this example, I am using the board NodeMCU Lolin V3. You will need five pins from this board to connect to the BME680 sensor: two ground pins (G), 3V, D3, and D4.

14.     Connect the BME680 sensor to the NodeMCU board as follows: 

BME680 (VCC pin) >>  NodeMCU (3V pin)
BME680 (GND pin) >> NodeMCU (G)
BME680 (SDI pin) >> NodeMCU (D3)
BME680 (SCK pin) >> NodeMCU (D4)
BME680 (SDO pin) >> NodeMCU (G)

Connecting the SDO pin from the BME680 to the GND is important because the original code was programmed to use the alternative I²C address (0x77). You can access this I2C address from the BME680 sensor by connecting the SDO pin to the Ground.

15.     Finally, connect the NodeMCU board to your PC through a MicroUSB cable.

16.     Open the example sketch basic from the BSEC library you downloaded before.

17.     Select the COM Port for the NodeMCU board under Tools.

18. One last change is necessary to run this sketch. Look for the Wire.begin() function at the top of the code.  

void setup(void)
{
Serial.begin(115200);
Wire.begin();

Now make the following change to the Wire.begin() function: 

void setup(void)
{
Serial.begin(115200);
Wire.begin(0,2);

19. Compile and run the sketch. Please note that the Baud Rate from the Serial Monitor should be set at 115200. If this is the first time you upload the code to your microcontroller, it will be necessary to reset the board through the RST pin.

Serial Monitor Output

The following parameters will be displayed every 3 seconds:

  • Timestamp in milliseconds
  • Raw Temperature in °C
  • Pressure in hPa
  • Raw Relative Humidity in %
  • Raw data from the gas sensor as a resistance value in Ohm
  • IAQ index
  • IAQ Accuracy (begins at 0 after start-up, goes to 1 after a few minutes, and reaches 3 when the sensor is calibrated).
  • Temperature in °C
  • Relative Humidity in %
  • Static IAQ
  • CO2 equivalent (estimation of the CO2 equivalent in ppm in the environment)
  • Breath VOC equivalent output (estimates the total VOC concentration in ppm in the environment) 

The initial value of the IAQ index is 25.00. and it stays that way for a good while. Only after several minutes does the IAQ value start to drift.  I hope with this tutorial you’ve learned how to measure indoor air quality (IAQ) with the BME680.

Leave a Reply

Your email address will not be published. Required fields are marked *