ESP32 BME680 Environmental Sensor
The BME680 is a versatile environmental sensor capable of measuring air quality (VOCs), temperature, humidity, and pressure. It supports I2C and SPI interfaces and is ideal for indoor monitoring, IoT devices, and smart homes.
Jump to Code Examples
Quick Links
BME680 Price
About BME680 Environmental Sensor
The BME680 is an advanced environmental sensor from Bosch Sensortec, capable of measuring gas (air quality, VOCs), temperature, pressure, and humidity. Its compact design makes it suitable for applications like smart homes, HVAC systems, and wearable devices. The sensor supports both I2C and SPI interfaces for flexible communication. It offers low power consumption, ensuring compatibility with battery-operated devices. With its gas sensor, the BME680 can assess air quality levels, making it ideal for indoor monitoring and environmental control.
BME680 Sensor Technical Specifications
Below you can see the BME680 Environmental Sensor Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.
- Protocol: I2C/SPI
- Interface: I2C/SPI
- Measurement Capabilities: Gas, Temperature, Pressure, Humidity
- Operating Voltage: 1.71V to 3.6V
- Gas Sensor: Volatile Organic Compounds (VOCs)
- Pressure Range: 300 hPa to 1100 hPa
- Temperature Range: -40°C to +85°C
- Humidity Range: 0% to 100% RH
- Power Consumption: 2.1 µA in sleep mode
- Communication Speed: 10 kHz to 3.4 MHz (I2C), 10 kHz to 10 MHz (SPI)
- Package Dimensions: 3.0 mm × 3.0 mm × 0.93 mm
BME680 Sensor Pinout
Below you can see the pinout for the BME680 Environmental Sensor. The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32!
The BME680 module features the following pins: VIN
: Supplies power to the module, typically 3.3V or 5V. GND
: Ground pin for the module. SDA
: Serial Data pin for I2C communication. Connect to the microcontroller's SDA pin. SCL
: Serial Clock pin for I2C communication. Connect to the microcontroller's SCL pin. CS
: Chip Select pin for SPI communication. If unused, tie to VCC. SDO
: Optional output for I2C/SPI communication, configurable for address selection or SPI operation.
BME680 Wiring with ESP32
Below you can see the wiring for the BME680 Environmental Sensor with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.
Below you can see BME680 wiring with ESP32 in SPI mode
To wire the BME680 module with an ESP32 (I2C mode): VIN
: Connect to 3.3V pin on ESP32. GND
: Connect to GND pin on ESP32. SDA
: Connect to GPIO21 (default SDA). SCL
: Connect to GPIO22 (default SCL). For SPI mode, additional connections are required for CS and SDO pins.
Code Examples
Below you can find code examples of BME680 Environmental Sensor with ESP32 in several frameworks:
ESP32 BME680 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the BME680 Environmental Sensor:
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME680.h>
Adafruit_BME680 bme;
void setup() {
Serial.begin(115200);
if (!bme.begin(0x76)) {
Serial.println("Could not find a valid BME680 sensor, check wiring!");
while (1);
}
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
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");
delay(2000);
}
This Arduino code initializes the BME680 sensor and configures it for temperature, humidity, pressure, and gas readings. Oversampling settings are used to improve data accuracy. The loop()
function reads sensor data every two seconds and outputs the values to the Serial Monitor. The setGasHeater
function configures the gas sensor for VOC detection.
Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.
ESP32 BME680 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the BME680 Environmental Sensor, here's how you can set it up and read data from the sensor. Fill in this code in the main
ESP-IDF file:
#include "bme680.h"
#include "esp_log.h"
#define I2C_MASTER_SCL_IO 22
#define I2C_MASTER_SDA_IO 21
#define I2C_MASTER_FREQ_HZ 100000
#define BME680_ADDR 0x76
static const char *TAG = "BME680";
void app_main() {
ESP_LOGI(TAG, "Initializing BME680...");
bme680_dev dev;
bme680_init(&dev, I2C_MASTER_SCL_IO, I2C_MASTER_SDA_IO, I2C_MASTER_FREQ_HZ, BME680_ADDR);
while (1) {
bme680_data data;
bme680_read(&dev, &data);
ESP_LOGI(TAG, "Temperature: %.2f°C", data.temperature);
ESP_LOGI(TAG, "Pressure: %.2f hPa", data.pressure);
ESP_LOGI(TAG, "Humidity: %.2f%%", data.humidity);
ESP_LOGI(TAG, "Gas Resistance: %.2f KOhms", data.gas_resistance);
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}
This ESP-IDF code configures the ESP32 to interface with the BME680 sensor over I2C. The bme680_init
function initializes the sensor. In the app_main()
loop, the sensor's environmental data (temperature, pressure, humidity, and gas resistance) is read and logged to the console every two seconds.
Update the I2C pins (I2C_MASTER_SDA_IO
and I2C_MASTER_SCL_IO
) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.
ESP32 BME680 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the BME680 Environmental Sensor
sensor:
- platform: bme680
temperature:
name: "BME680 Temperature"
pressure:
name: "BME680 Pressure"
humidity:
name: "BME680 Humidity"
gas_resistance:
name: "BME680 Gas Resistance"
address: 0x76
This ESPHome configuration integrates the BME680 sensor. It creates sensor entities for temperature, pressure, humidity, and gas resistance, which are monitored via the I2C address 0x76
. The setup is ideal for home automation and IoT systems.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.
ESP32 BME680 PlatformIO Code Example
For PlatformIO, make sure to configure the platformio.ini
file with the appropriate environment and libraries, and then proceed with the code.
Configure platformio.ini
First, your platformio.ini
should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
adafruit/Adafruit BME680 Library
wire
monitor_speed = 115200
ESP32 BME680 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the BME680 Environmental Sensor:
#include <Wire.h>
#include "Adafruit_BME680.h"
Adafruit_BME680 bme;
void setup() {
Serial.begin(115200);
if (!bme.begin()) {
Serial.println("Could not find a valid BME680 sensor, check wiring!");
while (1);
}
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setGasHeater(320, 150);
}
void loop() {
if (bme.performReading()) {
Serial.printf("Temp: %.2f*C, Pressure: %.2fhPa, Humidity: %.2f%%, Gas: %.2f KOhms\n",
bme.temperature, bme.pressure / 100.0, bme.humidity, bme.gas_resistance / 1000.0);
}
delay(2000);
}
This PlatformIO code integrates the Adafruit BME680 library for reading environmental data from the sensor. Temperature, humidity, pressure, and gas readings are fetched and printed to the Serial Monitor every two seconds.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.
ESP32 BME680 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the BME680 Environmental Sensor with your ESP32.
from machine import I2C, Pin
from bme680 import BME680_I2C
# Initialize I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# Initialize BME680
bme = BME680_I2C(i2c=i2c)
while True:
print(f"Temperature: {bme.temperature:.2f} °C")
print(f"Pressure: {bme.pressure:.2f} hPa")
print(f"Humidity: {bme.humidity:.2f} %")
print(f"Gas: {bme.gas:.2f} KOhms")
sleep(2)
This MicroPython code initializes the BME680 sensor using I2C. The sensor reads temperature, pressure, humidity, and gas values, which are printed to the console every two seconds.
Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy
.
Conclusion
We went through technical specifications of BME680 Environmental Sensor, its pinout, connection with ESP32 and BME680 Environmental Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.