Table of Contents
Sensor fusion combines data from multiple sensors to achieve more accurate and reliable measurements than any single sensor could provide. When your DHT22 reads 32 degrees Celsius and your BME280 reads 31.5 degrees Celsius, which one is correct? Sensor fusion gives you a mathematically optimal answer. This guide covers practical fusion techniques for ESP32-based IoT projects.
What is Sensor Fusion
Sensor fusion is the process of combining data from multiple sensor sources to produce a more accurate, reliable, or complete measurement. In IoT:
- Redundancy: If one sensor fails, others continue providing data
- Accuracy: Combining noisy sensors yields cleaner readings
- Coverage: Different sensors cover different aspects of the environment
- Validation: Cross-check readings to detect sensor faults
Types of Sensor Fusion
- Complementary fusion: Combine sensors with complementary strengths (e.g., GPS + IMU for navigation)
- Competitive fusion: Multiple sensors measure the same thing — weighted average for better accuracy
- Cooperative fusion: Sensors provide different parts of a larger picture (e.g., camera + lidar for 3D mapping)
Sensors for Fusion Experiments
Kalman Filter Basics for IoT
The Kalman filter is the gold standard for sensor fusion. It predicts the next state and corrects based on measurements:
// Simple 1D Kalman filter for temperature
class KalmanFilter {
float estimate; // Current estimate
float error_estimate; // Estimate uncertainty
float error_measure; // Measurement uncertainty
float gain; // Kalman gain
public:
KalmanFilter(float initial, float err_est, float err_meas) {
estimate = initial;
error_estimate = err_est;
error_measure = err_meas;
}
float update(float measurement) {
// Calculate Kalman gain
gain = error_estimate / (error_estimate + error_measure);
// Update estimate with measurement
estimate = estimate + gain * (measurement - estimate);
// Update error estimate
error_estimate = (1 - gain) * error_estimate;
return estimate;
}
};
// Usage: Fuse DHT22 and BME280 temperature readings
KalmanFilter tempFilter(25.0, 2.0, 1.5);
void loop() {
float dht_temp = dht.readTemperature();
float bme_temp = bme.readTemperature();
// Weight average based on sensor accuracy
float combined = (dht_temp * 0.4 + bme_temp * 0.6);
// Apply Kalman filter for smooth, accurate output
float fused_temp = tempFilter.update(combined);
Serial.printf("Fused temperature: %.2f Cn", fused_temp);
}
Complementary Filter for IMU Data
For IMU (accelerometer + gyroscope) data, a complementary filter is simpler and often sufficient:
// Complementary filter: Combine accelerometer and gyroscope
// Gyro: accurate short-term, drifts long-term
// Accel: accurate long-term, noisy short-term
float angle = 0;
const float ALPHA = 0.98; // Trust gyro 98%, accel 2%
unsigned long lastTime = 0;
void updateAngle(float gyroRate, float accelAngle) {
unsigned long now = millis();
float dt = (now - lastTime) / 1000.0;
lastTime = now;
// Complementary filter
angle = ALPHA * (angle + gyroRate * dt) + (1 - ALPHA) * accelAngle;
}
Multi-Sensor Environmental Monitoring
Combine multiple environmental sensors for comprehensive monitoring:
- Temperature: Fuse DHT22, BME280, and DS18B20 readings for accurate temperature
- Air quality: Combine MQ-135 (gases), PM2.5 sensor, and BME280 (pressure) for complete air quality index
- Occupancy: Merge PIR motion, CO2 level, and light sensor data for reliable occupancy detection
Implementing Sensor Fusion on ESP32
// Multi-sensor fusion on ESP32
#include
#include
#include
DHT dht(4, DHT22);
Adafruit_BME280 bme;
struct FusedData {
float temperature; // Fused from DHT22 + BME280
float humidity; // Fused from DHT22 + BME280
float pressure; // BME280 only
float confidence; // 0-1 confidence score
};
FusedData fuseSensors() {
FusedData data;
float dht_t = dht.readTemperature();
float dht_h = dht.readHumidity();
float bme_t = bme.readTemperature();
float bme_h = bme.readHumidity();
float bme_p = bme.readPressure() / 100.0F;
bool dht_ok = !isnan(dht_t) && !isnan(dht_h);
bool bme_ok = (bme_t != 0);
if (dht_ok && bme_ok) {
// Both sensors working: weighted average
// BME280 is more accurate, give it 60% weight
data.temperature = dht_t * 0.4 + bme_t * 0.6;
data.humidity = dht_h * 0.4 + bme_h * 0.6;
data.confidence = 0.95;
// Sanity check: if sensors disagree by >5C, flag issue
if (abs(dht_t - bme_t) > 5.0) {
data.confidence = 0.5;
Serial.println("WARNING: Sensor disagreement detected!");
}
} else if (bme_ok) {
data.temperature = bme_t;
data.humidity = bme_h;
data.confidence = 0.7;
} else if (dht_ok) {
data.temperature = dht_t;
data.humidity = dht_h;
data.confidence = 0.6;
} else {
data.confidence = 0;
}
data.pressure = bme_ok ? bme_p : 0;
return data;
}
Practical Applications in India
- Smart agriculture: Fuse soil moisture, temperature, humidity, and rainfall sensors for accurate irrigation decisions
- Traffic management: Combine ultrasonic vehicle detectors, cameras, and inductive loops for accurate traffic flow measurement
- Industrial safety: Merge gas sensors, temperature, humidity, and vibration data for comprehensive hazard detection in Indian factories
- Weather stations: Fuse multiple temperature and humidity sensors for research-grade accuracy at a fraction of the cost
Recommended Product
BME280 Environmental Sensor (Temperature, Humidity, Barometric Pressure)
Frequently Asked Questions
Why not just use the most accurate sensor?
Even the most accurate sensor can fail, drift over time, or have blind spots. Sensor fusion provides redundancy, catches failures automatically, and often achieves accuracy better than any individual sensor by combining their strengths.
Is a Kalman filter necessary for simple IoT projects?
For basic home monitoring, a simple weighted average or moving average filter is sufficient. Use Kalman filters when you need optimal estimation from noisy sensors or when combining sensors with different update rates.
How many sensors should I fuse?
Two sensors of the same type provide significant improvement. Three gives diminishing returns but adds redundancy. More than four of the same type rarely helps unless you are building a scientific instrument.
Can sensor fusion detect faulty sensors?
Yes. When multiple sensors agree and one deviates significantly, the fusion algorithm can identify and exclude the faulty reading. This is called fault detection and isolation (FDI).
{“@context”: “https://schema.org”, “@type”: “FAQPage”, “mainEntity”: [{“@type”: “Question”, “name”: “Why not just use the most accurate sensor?”, “acceptedAnswer”: {“@type”: “Answer”, “text”: “Even the most accurate sensor can fail, drift over time, or have blind spots. Sensor fusion provides redundancy, catches failures automatically, and often achieves accuracy better than any individual sensor by combining their strengths.”}}, {“@type”: “Question”, “name”: “Is a Kalman filter necessary for simple IoT projects?”, “acceptedAnswer”: {“@type”: “Answer”, “text”: “For basic home monitoring, a simple weighted average or moving average filter is sufficient. Use Kalman filters when you need optimal estimation from noisy sensors or when combining sensors with different update rates.”}}, {“@type”: “Question”, “name”: “How many sensors should I fuse?”, “acceptedAnswer”: {“@type”: “Answer”, “text”: “Two sensors of the same type provide significant improvement. Three gives diminishing returns but adds redundancy. More than four of the same type rarely helps unless you are building a scientific instrument.”}}, {“@type”: “Question”, “name”: “Can sensor fusion detect faulty sensors?”, “acceptedAnswer”: {“@type”: “Answer”, “text”: “Yes. When multiple sensors agree and one deviates significantly, the fusion algorithm can identify and exclude the faulty reading. This is called fault detection and isolation (FDI).”}}]}
Ready to Build Your IoT Project?
Browse our complete collection of ESP32 boards, sensors, and IoT components. Fast shipping across India with technical support.
Add comment