Raspberry Pi Pico: BME680 Environmental Sensor (Arduino IDE)

Get started with the BME680 environmental sensor module with the Raspberry Pi Pico board programmed with the Arduino IDE to get data about temperature, humidity, pressure, and gas (air quality). We’ll show you how to wire the sensor, install the required libraries, and write a simple sketch to display sensor readings.

Raspberry Pi Pico BME680 Environmental Sensor Arduino IDE

New to the Raspberry Pi Pico? Get started with the Raspberry Pi Pico here.

Table of Contents:

Raspberry Pi Pico with Arduino IDE

You need to install the Raspberry Pi Pico boards on Arduino IDE and you must know how to upload code to the board. Check out the following tutorial first if you haven’t already:

Introducing BME680 Environmental Sensor Module

The BME680 is an environmental sensor that combines gas, pressure, humidity, and temperature sensors. The gas sensor can detect a broad range of gases like volatile organic compounds (VOC). For this reason, the BME680 can be used in indoor air quality control.

BME680 Gas sensor humidity barometric pressure ambient temperature gas air quality front

BME680 Measurements

The BME680 is a 4-in-1 digital sensor that measures:

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

Gas Sensor

The BME680 contains a MOX (Metal-oxide) sensor that detects VOCs in the air. This sensor gives you a qualitative idea of the sum of VOCs/contaminants in the surrounding air – it is not specific for a specific gas molecule.

MOX sensors are composed of a metal-oxide surface, a sensing chip to measure changes in conductivity and a heater. It detects VOCs by adsorption of oxygen molecules on its sensitive layer. The BME680 reacts to most VOCs polluting indoor air (except CO2).

When the sensor comes into contact with the reducing gases, the oxygen molecules react and increase the conductivity across the surface. As a raw signal, the BME680 outputs resistance values. These values change due to variations in VOC concentrations:

BME680 Gas Environmental Air Quality Sensor Resistance How It Works
  • Higher concentration of VOCs » Lower resistance
  • Lower concentration of VOCs » Higher resistance

The reactions that occur on the sensor surface (thus, the resistance) are influenced by parameters other than VOC concentration like temperature and humidity.

Relevant Information Regarding Gas Sensor

The gas sensor gives you a qualitative idea of VOCs gases in the surrounding air. So, you can get trends, compare your results, and see if the air quality is increasing or decreasing. To get precise measurements, you must calibrate the sensor against known sources and build a calibration curve.

When you first get the sensor, it is recommended to run it for 48 hours before starting to collect “real” data. After that, it is also recommended to run the sensor for 30 minutes before getting a gas reading.

BME680 Accuracy

Here’s the accuracy of the temperature, humidity, and pressure sensors of the BME680:

SensorAccuracy
Temperature+/- 1.0ºC
Humidity+/- 3%
Pressure+/- 1 hPa

BME680 Operation Range

The following table shows the operation range for the temperature, humidity, and pressure sensors for the BME680.

SensorOperation Range
Temperature-40 to 85 ºC
Humidity0 to 100 %
Pressure300 to 1100 hPa

BME680 Pinout

Here’s the BME680 Pinout:

VCCPowers the sensor
GNDCommon GND
SCLSCL pin for I2C communication
SCK pin for SPI communication
SDASDA pin for I2C communication
SDI (MOSI) pin for SPI communication
SDOSDO (MISO) pin for SPI communication
CSChip select pin for SPI communication

BME680 Interface

The BME680 supports I2C and SPI Interfaces.

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

BME680 I2C

This sensor communicates using I2C communication protocol, so the wiring is straighforward. You can use any I2C pin combination of the Raspberry Pi Pico. We’ll be using GPIO 5 (SCL) and GPIO 4 (SDA). You can use any other combination of I2C pins as long as you add them to the code.

BME680Raspberry Pi Pico
Vin3.3V
GNDGND
SCLGPIO 5
SDAGPIO 4

Learn more about the Raspberry Pi Pico GPIOs: Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained

Parts Required

Raspberry Pi Pico with BME680 environmental sensor

For this project, you need to wire the BME680 sensor module to the Raspberry Pi Pico I2C pins. Here’s a list of parts you need for this tutorial:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Installing the BME680 Library for Raspberry Pi Pico – Arduino IDE

There are different libraries you can use to get readings from the BME680 sensor. We’ll use the Adafruit_BME680 library.

Open your Arduino IDE and go to Sketch Include Library > Manage Libraries. The Library Manager should open.

Search for “adafruit_bme680 ” in the Search box and install the library by Adafruit. Install any other required dependencies.

install adafruit BME680 arduino ide

Wiring the BME680 to the Raspberry Pi Pico

Wire the BME680 to any combination of the Pico I2C pins—we’ll be using GPIO 4 (SDA) and GPIO 5 (SCL).

Raspberry Pi Pico wiring to BME680 sensor circuit diagram

Recommended reading: Raspberry Pi Pico and Pico W Pinout Guide: GPIOs Explained

Code – Reading BME680 Gas, Pressure, Humidity, and Temperature

To show you how to read temperature, humidity, and pressure from the BME680, we’ll use a simple sketch that prints the current readings every second to the Serial Monitor. The following example was adapted from the Adafruit BME680 library examples.

/*********
  Rui Santos & Sara Santos - Random Nerd Tutorials
  Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-bme680-arduino/
  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files.
  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*********/

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

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BME680 bme; // I2C (default pins for Raspberry Pi Pico: GPIO 4 (SDA), GPIO 5(SCL)

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

View raw code

How Does the Code Work

Continue reading this section to learn how the code works, or skip to the Demonstration section.

Libraries

The code starts by including the needed libraries: the wire library to use I2C, the Adafruit_Sensor and Adafruit_BME680 libraries to interface with the BME680 sensor.

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

Sea level pressure

A variable called SEALEVELPRESSURE_HPA is created.

#define SEALEVELPRESSURE_HPA (1013.25)

This variable saves the pressure at the sea level in hectopascal (is equivalent to milibar). This variable is used to estimate the altitude for a given pressure by comparing it with the sea level pressure. This example uses the default value, but for accurate results, replace the value with the current sea level pressure at your location.

I2C

This example uses I2C communication protocol by default. The following line creates an Adafruit_BME680 object called bme on the following RPi Pico GPIOs: GPIO 5 (SCL), GPIO 4 (SDA).

Adafruit_BME680 bme; // I2C

setup()

In the setup() start a serial communication.

Serial.begin(115200);

Init BME680 Sensor

Initialize the BME680 sensor:

if (!bme.begin()) {
  Serial.println(F("Could not find a valid BME680 sensor, check wiring!"));
  while (1);
}

Set up the following parameters (oversampling, filter, and gas heater) for the sensor.

// 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

To increase the resolution of the raw sensor data, it supports oversampling. We’ll use the default oversampling parameters, but you can change them.

  • setTemperatureOversampling(): set temperature oversampling.
  • setHumidityOversampling(): set humidity oversampling.
  • setPressureOversampling(): set pressure oversampling.

These methods can accept one of the following parameters:

  • BME680_OS_NONE: turn off reading;
  • BME680_OS_1X
  • BME680_OS_2X
  • BME680_OS_4X
  • BME680_OS_8X
  • BME680_OS_16X

The BME680 sensor integrates an internal IIR filter to reduce short-term changes in sensor output values caused by external disturbances. The setIIRFilterSize() method sets the IIR filter. It accepts the filter size as a parameter:

  • BME680_FILTER_SIZE_0 (no filtering)
  • BME680_FILTER_SIZE_1
  • BME680_FILTER_SIZE_3
  • BME680_FILTER_SIZE_7
  • BME680_FILTER_SIZE_15
  • BME680_FILTER_SIZE_31
  • BME680_FILTER_SIZE_63
  • BME680_FILTER_SIZE_127

The gas sensor integrates a heater. Set the heater profile using the setGasHeater() method that accepts as arguments:

  • the heater temperature (in degrees Centigrade)
  • the time the heater should be on (in milliseconds)

We’ll use the default settings: 320 ºC for 150 ms.

loop()

In the loop(), we’ll get measurements from the BME680 sensor.

First, tell the sensor to start an asynchronous reading with bme.beginReading(). This returns the time when the reading would be ready.

// 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);

Then, call the endReading() method to end an asynchronous reading. If the asynchronous reading is still in progress, block until it ends.

if (!bme.endReading()) {
  Serial.println(F("Failed to complete reading :("));
  return;
}

After this, we can get the readings as follows:

  • bme.temperature: returns temperature reading
  • bme.pressure: returns pressure reading
  • bme.humidity: returns humidity reading
  • bme.gas_resistance: returns gas resistance
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"));

For more information about the library methods, take a look at the Adafruit_BME680 Class Reference.

Uploading the Code

Upload the code to the Raspberry Pi Pico.

To upload code to the Raspberry Pi Pico, it needs to be in bootloader mode.

If the Raspberry Pi is currently running MicroPython firmware, you need to manually put it into bootloader mode. For that, connect the Raspberry Pi Pico to your computer while holding the BOOTSEL button at the same time.

Raspberry Pi Pico Bootloader mode

For future uploads using Arduino IDE, the board should go automatically into bootloader mode without the need to press the BOOTSEL button.

Now, select your COM port in Tools Port. It may be the case that the COM port is grayed out. If that’s the case, don’t worry it will automatically find the port once you hit the upload button.

Upload the code.

Arduino 2.0 Upload Button

If you don’t know how to upload code to the Raspberry Pi Pico using Arduino IDE, check this tutorial: Programming the Raspberry Pi Pico with Arduino IDE.

Demonstration

After uploading the code to the board, open the Serial Monitor at a baud rate of 115200. The sensor measurements will be displayed every 5 seconds.

Raspberry Pi Pico with BME680 using Arduino IDE - demonstration

Wrapping Up

This tutorial was a getting started guide to the BME680 environmental and air quality sensor with the Raspberry Pi Pico using Arduino IDE.

We hope you’ve found this tutorial useful. We have tutorials for other popular environmental sensors:

Learn more about the Raspberry Pi Pico with our resources:

Thanks for reading.



Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »
Learn how to build a home automation system and we’ll cover the following main subjects: Node-RED, Node-RED Dashboard, Raspberry Pi, ESP32, ESP8266, MQTT, and InfluxDB database DOWNLOAD »

Recommended Resources

Build a Home Automation System from Scratch » With Raspberry Pi, ESP8266, Arduino, and Node-RED.

Home Automation using ESP8266 eBook and video course » Build IoT and home automation projects.

Arduino Step-by-Step Projects » Build 25 Arduino projects with our course, even with no prior experience!

What to Read Next…


Enjoyed this project? Stay updated by subscribing our newsletter!

3 thoughts on “Raspberry Pi Pico: BME680 Environmental Sensor (Arduino IDE)”

  1. Hello, thank you again for this very useful article.

    I have used several BME680 together with a “big” Raspi. It worked a while, but then each time humidity killed the BME680.

    What would be a smart idea for housing the sensor?

    Tanks

    Reply
  2. Hi, very nice article indeed. On question, though: the experimental project has short wires between the BMP680 and the RPi Pico. Especially for the I2C communication.
    But how do you solve cases where the RPi Pico and the BMP680 are located meters away from each other? Say, the RPi Pico is located at one side of the house and the BMP680 is located at the other end of the house, 20 meters away (cable length).
    For I2C, this is far too long, so how do you solve this?

    Reply
    • Good question.

      Using the BME680 with a “real” Rasperry pi i hat 8 m I2C-cable (old phone cord).

      What kind of housing for the BME680 do you prefer?

      I hade defective sensors, because von humidity.

      Reply

Leave a Comment

Download Our Free eBooks and Resources

Get instant access to our FREE eBooks, Resources, and Exclusive Electronics Projects by entering your email address below.