Skip to main content
Learn how to interface with digital sensors using I2C and other protocols in the Kinematrix sensor framework.

I2C Environmental Sensors

Digital sensors like BME680, BME280, and other I2C devices provide high-precision measurements through a simple two-wire interface.

BME680 (I2C)

The BME680 communicates over I2C and supports multiple sensors on the same bus using different addresses.
1

Hardware Setup

Connect the BME680 using I2C:
BME680    ESP32/Arduino
------    -------------
VCC   ->  3.3V
GND   ->  GND
SCL   ->  SCL (GPIO 22 on ESP32)
SDA   ->  SDA (GPIO 21 on ESP32)
2

Basic I2C Sensor

#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_BME680_V2
#define ENABLE_SENSOR_UTILITY_V2
#include "Kinematrix.h"

BME680SensV2 bme680Sensor;  // Default I2C address 0x76

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

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

Multiple I2C Sensors

Use different I2C addresses for multiple sensors on the same bus:
BME680SensV2* indoor = new BME680SensV2();      // Default: 0x76
BME680SensV2* outdoor = new BME680SensV2(0x77); // Alternate address

sensorModule.addSensor("indoor", indoor);
sensorModule.addSensor("outdoor", outdoor);
Source: /home/daytona/workspace/source/example/sensors/SensorModuleV2/SensorList/BME680SensV2/BasicBME680Sensor/BasicBME680Sensor.ino:1

DHT Digital Sensors

DHT sensors use a single-wire digital protocol for temperature and humidity readings.

DHT22/DHT11 Setup

#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_DHT_V2
#define ENABLE_SENSOR_UTILITY_V2
#include "Kinematrix.h"

DHTSensV2 dhtSensor(2, DHT22);  // GPIO 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

DHT Sensor Types

  • DHT11: Lower accuracy (±2°C, ±5% RH), 1Hz sampling
  • DHT22: Higher accuracy (±0.5°C, ±2% RH), 0.5Hz sampling
DHTSensV2 dht11Sensor(3, DHT11);   // DHT11 on GPIO 3
DHTSensV2 dht22Sensor(4, DHT22);   // DHT22 on GPIO 4

RTC and Time-Based Sensors

Real-time clock sensors maintain accurate time and date:
#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_RTC_DS3231_V2
#define ENABLE_SENSOR_UTILITY_V2
#include "Kinematrix.h"

SensorModuleV2 sensorModule;

void setup() {
    Serial.begin(115200);
    
    sensorModule.addSensor("rtc", new RTCDS3231SensV2());
    sensorModule.init();
    
    // Enable temperature reading from RTC
    RTCDS3231SensV2* rtc = (RTCDS3231SensV2*)sensorModule.getSensorByName("rtc");
    if (rtc) {
        rtc->enableTemperature(true);
    }
}

void loop() {
    sensorModule.update();
    
    if (sensorModule.isUpdated("rtc")) {
        Serial.print("Time: ");
        Serial.print((int)sensorModule["rtc"]["hour"]);
        Serial.print(":");
        Serial.print((int)sensorModule["rtc"]["minute"]);
        Serial.print(":");
        Serial.println((int)sensorModule["rtc"]["second"]);
    }
    
    delay(1000);
}

Calibration for Digital Sensors

Even digital sensors can benefit from 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.addSensor("dht11", new DHTSensV2(3, DHT11));
    calibrationModule.init();
    
    // Add calibration entries
    calibrationModule.addCalibrationEntry("dht22", "temperature");
    calibrationModule.addCalibrationEntry("dht11", "temperature");
    calibrationModule.addCalibrationEntry("dht22", "humidity");
    calibrationModule.addCalibrationEntry("dht11", "humidity");
    
    // Two-point calibration against reference sensors
    calibrationModule.calibrateTwoPoint("dht22", "temperature", 20.0, 19.8, 30.0, 30.3);
    calibrationModule.calibrateTwoPoint("dht11", "temperature", 20.0, 19.8, 30.0, 30.3);
    
    // One-point humidity calibration
    calibrationModule.calibrateOnePoint("dht22", "humidity", 60.0);
    calibrationModule.calibrateOnePoint("dht11", "humidity", 60.0);
    
    Serial.println("DHT sensors with calibration ready");
}

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

I2C Scanner Utility

Find connected I2C devices and their addresses:
#include <Wire.h>

void setup() {
    Serial.begin(115200);
    Wire.begin();
    
    Serial.println("I2C Scanner");
    for (byte address = 1; address < 127; address++) {
        Wire.beginTransmission(address);
        byte error = Wire.endTransmission();
        
        if (error == 0) {
            Serial.print("I2C device found at address 0x");
            if (address < 16) Serial.print("0");
            Serial.println(address, HEX);
        }
    }
    Serial.println("Scan complete");
}

void loop() {}

Common I2C Addresses

SensorDefault AddressAlternate Address
BME6800x760x77
BME2800x760x77
DS3231 RTC0x68-
MLX906140x5AConfigurable
INA2190x400x41-0x4F

Wiring Diagrams

I2C Bus (Multiple Sensors)

                3.3V
                 |
            4.7kΩ  4.7kΩ (pull-up resistors)
                 |   |
    ESP32/Arduino    |   |
         SDA --------+---+--- SDA (All sensors)
         SCL ------------+--- SCL (All sensors)
         GND ------------+--- GND (All sensors)
        3.3V ------------+--- VCC (All sensors)

DHT22 Single-Wire Digital

DHT22     ESP32/Arduino
-----     -------------
VCC   ->  3.3V or 5V
DATA  ->  GPIO 2 (with 10kΩ pull-up to VCC)
NC    ->  Not connected
GND   ->  GND

Troubleshooting

I2C Communication Fails

  • Check pull-up resistors (4.7kΩ recommended)
  • Verify correct I2C address
  • Use I2C scanner to detect devices
  • Check wire length (keep short, < 1m)
  • Verify 3.3V logic levels

DHT Sensor Returns NaN

  • Check pull-up resistor (10kΩ required)
  • Increase delay between readings (min 2 seconds)
  • Verify correct sensor type (DHT11 vs DHT22)
  • Check power supply stability

Next Steps

Analog Sensors

Learn about analog sensor interfaces

Multi-Sensor Systems

Build systems combining multiple sensor types

Build docs developers (and LLMs) love