Skip to main content

Overview

Kinematrix provides a powerful and unified sensor framework that makes reading sensor data simple and consistent. The SensorModuleV2 system supports multiple sensor types with a common API, automatic updates, and type-safe data access.

Quick Start

Reading a Single Sensor

Here’s how to read data from a DHT22 temperature and humidity sensor:
1

Enable Required Modules

Add these defines before including Kinematrix.h:
#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_DHT_V2
#define ENABLE_SENSOR_UTILITY_V2
#include "Kinematrix.h"
2

Create Sensor Instance

Initialize your sensor with the appropriate pin and type:
DHTSensV2 dhtSensor(2, DHT22);  // Pin 2, DHT22 sensor
3

Initialize in setup()

Configure the sensor with JSON document support:
void setup() {
    Serial.begin(115200);
    Serial.println("Basic DHT Sensor Example");
    
    JsonDocument doc;
    dhtSensor.setDocumentValue(&doc);
    dhtSensor.setDocument("dht");
    
    if (dhtSensor.init()) {
        Serial.println("DHT sensor initialized successfully");
    } else {
        Serial.println("DHT sensor initialization failed!");
    }
}
4

Read Sensor Data

Update and read sensor values in your main loop:
void loop() {
    if (dhtSensor.update()) {
        float temperature = (float)dhtSensor.getFloatValue("temperature");
        float humidity = (float)dhtSensor.getFloatValue("humidity");
        float heatIndex = (float)dhtSensor.getFloatValue("heatIndex");
        
        Serial.print("Temperature: ");
        Serial.print(temperature);
        Serial.print("°C, Humidity: ");
        Serial.print(humidity);
        Serial.print("%, Heat Index: ");
        Serial.print(heatIndex);
        Serial.println("°C");
    }
    
    delay(2000);
}

Reading Analog Sensors

Analog sensors like potentiometers, light sensors, and voltage dividers use the AnalogSensV2 class:
#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_ANALOG_V2
#define ENABLE_SENSOR_UTILITY_V2
#include "Kinematrix.h"

AnalogSensV2 analogSensor(A0, 5.0, 1023);  // Pin A0, 5V reference, 10-bit ADC

void setup() {
    Serial.begin(115200);
    Serial.println("Basic Analog Sensor Example");
    
    JsonDocument doc;
    analogSensor.setDocumentValue(&doc);
    analogSensor.setDocument("analog");
    
    analogSensor.init();
    Serial.println("Analog sensor initialized");
}

void loop() {
    if (analogSensor.update()) {
        int rawValue = (int)analogSensor.getIntValue("raw");
        float voltage = (float)analogSensor.getFloatValue("volt");
        
        Serial.print("Raw Value: ");
        Serial.print(rawValue);
        Serial.print(", Voltage: ");
        Serial.print(voltage);
        Serial.println("V");
    }
    
    delay(500);
}

Using SensorModuleV2 for Multiple Sensors

The SensorModuleV2 class manages multiple sensors with a unified interface:
#define ENABLE_SENSOR_MODULE_V2
#define ENABLE_SENSOR_DHT_V2
#define ENABLE_SENSOR_ANALOG_V2
#define ENABLE_SENSOR_ABSTRACT_V2
#define ENABLE_SENSOR_UTILITY_V2
#include "Kinematrix.h"

SensorModuleV2 sensorModule;

void setup() {
    Serial.begin(115200);
    Serial.println("Basic SensorModuleV2 Example");
    
    // Add sensors with unique names
    sensorModule.addSensor("tempSensor", new DHTSensV2(2, DHT22));
    sensorModule.addSensor("analog", new AnalogSensV2(A0, 5.0, 1023));
    sensorModule.addSensor("abstract", new AbstractSensV2(0));
    
    sensorModule.init();
}

void loop() {
    sensorModule.update();
    
    // Check if specific sensor has new data
    if (sensorModule.isUpdated("tempSensor")) {
        Serial.print("Temperature: ");
        Serial.println((float)sensorModule["tempSensor"]["temperature"]);
    }
    
    if (sensorModule.isUpdated("analog")) {
        Serial.print("Voltage: ");
        Serial.println((float)sensorModule["analog"]["volt"]);
    }
    
    delay(1000);
}

Available Sensor Types

Kinematrix supports a wide range of sensors:
Sensor TypeClass NameUse Case
DHT11/DHT22DHTSensV2Temperature & humidity
BME680BME680SensV2Environmental monitoring (temp, humidity, pressure, gas)
AnalogAnalogSensV2Potentiometers, light sensors, voltage monitoring
INA219INA219SensV2Current & voltage monitoring
MQ GasMQSensV2Gas detection (CO, CO2, smoke)
MLX90614MLX90614SensV2Infrared temperature
GP2Y DustGP2YDustSensV2Air quality
RTCRTCSensV2Real-time clock
MHRTCMHRTCSensV2Multi-harmony RTC

Data Access Methods

SensorModuleV2 provides multiple ways to access sensor data:

Direct Access with Brackets

float temp = (float)sensorModule["tempSensor"]["temperature"];

Typed Getters

float temp = sensorModule.getValue<float>("tempSensor", "temperature");
int raw = sensorModule.getValue<int>("analog", "raw");

Check for Updates

if (sensorModule.isUpdated("tempSensor")) {
    // New data available from this sensor
}

Key Concepts

Automatic Updates

Sensors automatically read and update their values based on configured intervals.

Type Safety

Use typed methods like getFloatValue() and getIntValue() for type-safe data access.

JSON Integration

All sensor data is stored in JSON documents for easy serialization and transmission.

Unified API

All sensors share the same update/read pattern regardless of hardware interface.

Common Parameters

AnalogSensV2 Constructor

AnalogSensV2(pin, referenceVoltage, maxValue)
  • pin: Analog pin (A0, A1, etc.)
  • referenceVoltage: ADC reference voltage (3.3V or 5.0V)
  • maxValue: Maximum ADC value (1023 for 10-bit, 4095 for 12-bit)

DHTSensV2 Constructor

DHTSensV2(pin, type)
  • pin: Digital pin number
  • type: Sensor type (DHT11, DHT22, DHT21)

Next Steps

IoT Data Logger

Learn to log sensor data with timestamps and filters

LED Control

Control LEDs and outputs based on sensor readings

Multi-Platform Development

Deploy across ESP32, ESP8266, and Arduino platforms

Debugging

Debug your sensor applications effectively

Build docs developers (and LLMs) love