Skip to main content
Learn how to build environmental monitoring systems using BME680 and DHT sensors with the Kinematrix sensor framework.

BME680 Environmental Sensor

The BME680 is a comprehensive environmental sensor that measures temperature, humidity, pressure, and gas resistance (air quality).

Basic BME680 Setup

1

Enable Required Modules

Add the sensor module definitions before including Kinematrix:
#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_BME680_V2
#define ENABLE_SENSOR_UTILITY_V2
#include "Kinematrix.h"
2

Initialize the Sensor

Create and configure the BME680 sensor:
BME680SensV2 bme680Sensor;

void setup() {
    Serial.begin(115200);
    
    JsonDocument doc;
    bme680Sensor.setDocumentValue(&doc);
    bme680Sensor.setDocument("bme680");
    
    if (bme680Sensor.init()) {
        Serial.println("BME680 sensor initialized successfully");
    } else {
        Serial.println("BME680 sensor initialization failed!");
    }
}
3

Read Sensor Data

Update and read environmental values:
void loop() {
    if (bme680Sensor.update()) {
        Serial.print("Temperature: ");
        Serial.print((float)bme680Sensor.getFloatValue("temperature"));
        Serial.print("°C, Humidity: ");
        Serial.print((float)bme680Sensor.getFloatValue("humidity"));
        Serial.print("%, Pressure: ");
        Serial.print((float)bme680Sensor.getFloatValue("pressure"));
        Serial.print(" hPa, Gas: ");
        Serial.print((float)bme680Sensor.getFloatValue("gas"));
        Serial.println(" Ohms");
    }
    
    delay(2000);
}
Source: /home/daytona/workspace/source/example/sensors/SensorModuleV2/SensorList/BME680SensV2/BasicBME680Sensor/BasicBME680Sensor.ino:1

DHT Temperature and Humidity Sensor

DHT sensors (DHT11, DHT22) provide temperature and humidity readings with heat index calculation.
#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_DHT_V2
#define ENABLE_SENSOR_UTILITY_V2
#include "Kinematrix.h"

DHTSensV2 dhtSensor(2, DHT22);  // Pin 2, DHT22 type

void setup() {
    Serial.begin(115200);
    
    JsonDocument doc;
    dhtSensor.setDocumentValue(&doc);
    dhtSensor.setDocument("dht");
    
    if (dhtSensor.init()) {
        Serial.println("DHT sensor initialized successfully");
    }
}

void loop() {
    if (dhtSensor.update()) {
        Serial.print("Temperature: ");
        Serial.print((float)dhtSensor.getFloatValue("temperature"));
        Serial.print("°C, Humidity: ");
        Serial.print((float)dhtSensor.getFloatValue("humidity"));
        Serial.print("%, Heat Index: ");
        Serial.print((float)dhtSensor.getFloatValue("heatIndex"));
        Serial.println("°C");
    }
    
    delay(2000);
}
Source: /home/daytona/workspace/source/example/sensors/SensorModuleV2/SensorList/DHTSensV2/BasicDHTSensor/BasicDHTSensor.ino:1

Multiple Environmental Sensors

Monitor both indoor and outdoor conditions with multiple BME680 sensors:
#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_BME680_V2
#define ENABLE_SENSOR_FILTER_V2
#define ENABLE_SENSOR_ALERT_SYSTEM_V2
#define ENABLE_SENSOR_UTILITY_V2
#include "Kinematrix.h"

SensorModuleV2 sensorModule;

void setup() {
    Serial.begin(115200);
    
    // Create two BME680 sensors with different I2C addresses
    BME680SensV2* indoor = new BME680SensV2();
    BME680SensV2* outdoor = new BME680SensV2(0x77);
    
    sensorModule.addSensor("indoor", indoor);
    sensorModule.addSensor("outdoor", outdoor);
    sensorModule.init();
    
    // Add Kalman filters to indoor sensor
    FilterParams kalmanParams;
    kalmanParams.kalman.processNoise = 0.1;
    kalmanParams.kalman.measurementNoise = 0.5;
    sensorModule.attachFilter("indoor", "temperature", FILTER_KALMAN, kalmanParams);
    sensorModule.attachFilter("indoor", "humidity", FILTER_KALMAN, kalmanParams);
    
    // Add moving average filters to outdoor sensor
    FilterParams movingAvgParams;
    movingAvgParams.movingAverage.windowSize = 5;
    sensorModule.attachFilter("outdoor", "pressure", FILTER_MOVING_AVERAGE, movingAvgParams);
    sensorModule.attachFilter("outdoor", "gas", FILTER_MOVING_AVERAGE, movingAvgParams);
    
    // Set threshold alerts
    sensorModule.setThreshold("indoor", "temperature", 20.0, 28.0, ALERT_OUTSIDE);
    sensorModule.setThreshold("indoor", "humidity", 40.0, 70.0, ALERT_OUTSIDE);
    sensorModule.setThreshold("outdoor", "pressure", 950.0, 1050.0, ALERT_OUTSIDE);
    
    Serial.println("Multiple BME680 environmental sensors ready");
}

void loop() {
    sensorModule.update();
    
    if (sensorModule.isUpdated("indoor")) {
        float filteredTemp = sensorModule.getFilteredValue("indoor", "temperature");
        float filteredHum = sensorModule.getFilteredValue("indoor", "humidity");
        bool tempAlert = sensorModule.isAlertActive("indoor", "temperature");
        bool humAlert = sensorModule.isAlertActive("indoor", "humidity");
        
        Serial.println("=== Indoor BME680 ===");
        Serial.print("Temp: ");
        Serial.print(filteredTemp);
        Serial.print("°C (Alert: ");
        Serial.print(tempAlert ? "YES" : "NO");
        Serial.print("), Humidity: ");
        Serial.print(filteredHum);
        Serial.print("% (Alert: ");
        Serial.print(humAlert ? "YES" : "NO");
        Serial.println(")");
    }
    
    if (sensorModule.isUpdated("outdoor")) {
        float filteredPressure = sensorModule.getFilteredValue("outdoor", "pressure");
        bool pressureAlert = sensorModule.isAlertActive("outdoor", "pressure");
        
        Serial.println("=== Outdoor BME680 ===");
        Serial.print("Pressure: ");
        Serial.print(filteredPressure);
        Serial.print(" hPa (Alert: ");
        Serial.print(pressureAlert ? "YES" : "NO");
        Serial.println(")");
    }
    
    delay(3000);
}
Source: /home/daytona/workspace/source/example/sensors/SensorModuleV2/SensorList/BME680SensV2/MultipleBME680Sensors/MultipleBME680Sensors.ino:1

Sensor Calibration

Improve accuracy with two-point calibration:
#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_CALIBRATION_MODULE_V2
#define ENABLE_SENSOR_DHT_V2
#include "Kinematrix.h"

SensorCalibrationModuleV2 calibrationModule;

void setup() {
    Serial.begin(115200);
    
    calibrationModule.addSensor("dht22", new DHTSensV2(2, DHT22));
    calibrationModule.init();
    
    // Add calibration entries
    calibrationModule.addCalibrationEntry("dht22", "temperature");
    calibrationModule.addCalibrationEntry("dht22", "humidity");
    
    // Two-point calibration: calibrateTwoPoint(sensor, value, refLow, measuredLow, refHigh, measuredHigh)
    calibrationModule.calibrateTwoPoint("dht22", "temperature", 20.0, 19.8, 30.0, 30.3);
    calibrationModule.calibrateOnePoint("dht22", "humidity", 60.0);
    
    Serial.println("DHT sensor with calibration ready");
}

void loop() {
    calibrationModule.update();
    
    if (calibrationModule.isUpdated("dht22")) {
        float rawTemp = calibrationModule["dht22"]["temperature"];
        float calTemp = calibrationModule.getCalibratedValue("dht22", "temperature");
        
        Serial.print("Temp - Raw: ");
        Serial.print(rawTemp);
        Serial.print("°C, Calibrated: ");
        Serial.print(calTemp);
        Serial.println("°C");
    }
    
    delay(3000);
}
Source: /home/daytona/workspace/source/example/sensors/SensorModuleV2/SensorList/DHTSensV2/DHTWithCalibration/DHTWithCalibration.ino:1

Wiring Diagrams

BME680 Wiring (I2C)

BME680    ESP32/Arduino
------    -------------
VCC   ->  3.3V
GND   ->  GND
SCL   ->  SCL (GPIO 22 on ESP32)
SDA   ->  SDA (GPIO 21 on ESP32)

DHT22 Wiring

DHT22     ESP32/Arduino
-----     -------------
VCC   ->  3.3V or 5V
DATA  ->  Digital Pin 2 (with 10k pull-up resistor to VCC)
GND   ->  GND

Available Values

BME680SensV2

  • temperature - Temperature in °C (float)
  • humidity - Relative humidity in % (float)
  • pressure - Atmospheric pressure in hPa (float)
  • gas - Gas resistance in Ohms (float)

DHTSensV2

  • temperature - Temperature in °C (float)
  • humidity - Relative humidity in % (float)
  • heatIndex - Heat index in °C (float)

Next Steps

Analog Sensors

Learn to read analog sensors like pressure and flow meters

Multi-Sensor Systems

Build complex systems with multiple sensor types

Build docs developers (and LLMs) love