Energy Monitoring System Calibration: Linear Regression Approach for Sensor Accuracy
Introduction
In my ongoing energy monitoring project, accurate room temperature and humidity measurements are crucial for optimizing HVAC system performance and energy consumption. The low-cost DHT11 sensors used in the system required calibration against a professional-grade HTC-1 reference sensor to ensure reliable data collection.
The Calibration Challenge
The DHT11 sensor, while cost-effective, exhibited significant measurement errors compared to the HTC-1 reference:
Pre-Calibration Performance:
- Temperature MAE: 3.84°C
- Humidity MAE: 14.18%
- Temperature Bias: -3.84°C
- Maximum Error: 4.8°C (temperature), 18% (humidity)
These errors were unacceptable for energy optimization algorithms that rely on precise environmental data.
Data Collection Methodology
Data was collected simultaneously from both sensors over a 9-hour period, capturing various environmental conditions. The dataset included 19 measurement points across different times of day, with temperature readings ranging from 23.4°C to 27.7°C and humidity readings from 46.8% to 73.8%. This comprehensive sampling ensured the calibration model would be robust across typical operating conditions.
Linear Regression Model
Mathematical Foundation
The calibration uses simple linear regression to map raw sensor readings to calibrated values:
$$ y_{\text{calibrated}} = \beta_1 \cdot x_{\text{raw}} + \beta_0 $$
Where:
- $y_{\text{calibrated}}$ is the calibrated measurement
- $x_{\text{raw}}$ is the raw sensor reading
- $\beta_1$ is the slope coefficient
- $\beta_0$ is the intercept constant
Temperature Calibration Model
For temperature calibration, the regression yielded:
$$ y_{\text{temp}} = 1.343x - 7.689 $$
Model Statistics:
- R² = 0.881
- Standard Error = 0.495°C
- F-statistic = 125.95
Humidity Calibration Model
For humidity calibration:
$$ y_{\text{humidity}} = 0.914x + 5.127 $$
Model Statistics:
- R² = 0.981
- Standard Error = 0.951%
- F-statistic = 899.14
Performance Evaluation
Post-Calibration Results
The linear regression calibration significantly improved measurement accuracy:
Temperature Performance:
- MAE: 0.80°C (improved from 3.84°C)
- R²: 0.939
- Bias: +0.75°C
- Maximum Error: 1.40°C
Humidity Performance:
- MAE: 1.15% (improved from 14.18%)
- R²: 0.991
- Bias: -0.6%
- Maximum Error: 2.2%
Mean Absolute Error (MAE) Calculation
The MAE is calculated as:
$$ \text{MAE} = \frac{1}{n} \sum_{i=1}^{n} |y_{\text{predicted}} - y_{\text{actual}}| $$
Where $n$ is the number of samples in the validation dataset.
Implementation Code
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, r2_score
class SensorCalibrator:
def __init__(self):
self.temp_model = LinearRegression()
self.humidity_model = LinearRegression()
# Calibration coefficients from regression analysis
self.temp_coef = 1.343379812
self.temp_intercept = -7.688931392
self.humidity_coef = 0.914240883
self.humidity_intercept = 5.127226491
def calibrate_temperature(self, raw_temp):
"""Calibrate temperature reading using linear regression"""
return self.temp_coef * raw_temp + self.temp_intercept
def calibrate_humidity(self, raw_humidity):
"""Calibrate humidity reading using linear regression"""
return self.humidity_coef * raw_humidity + self.humidity_intercept
def evaluate_calibration(self, raw_readings, reference_readings):
"""Evaluate calibration performance"""
calibrated = [self.calibrate_temperature(temp) for temp in raw_readings]
mae = mean_absolute_error(reference_readings, calibrated)
r2 = r2_score(reference_readings, calibrated)
return {
'mae': mae,
'r2': r2,
'calibrated_readings': calibrated
}
# Usage example
calibrator = SensorCalibrator()
# Calibrate a new reading
raw_temp = 25.0
calibrated_temp = calibrator.calibrate_temperature(raw_temp)
print(f"Raw: {raw_temp}°C → Calibrated: {calibrated_temp:.2f}°C")
Results Visualization
The calibration significantly improved measurement accuracy across the entire operating range. The scatter plots show excellent linear correlation between calibrated DHT11 readings and HTC-1 reference values.
Key Improvements:
- Temperature Accuracy: 79% reduction in MAE
- Humidity Accuracy: 92% reduction in MAE
- Measurement Consistency: High R² values indicate reliable predictions
- Energy Optimization: More accurate data for HVAC control algorithms
Applications in Energy Monitoring
The calibrated sensors now provide reliable data for:
- HVAC Optimization: Precise temperature control reduces energy waste
- Occupancy Detection: Accurate environmental changes indicate room usage
- Energy Consumption Analysis: Correlate environmental conditions with energy usage
- Predictive Maintenance: Detect abnormal temperature/humidity patterns
Conclusion
Linear regression provides an effective method for calibrating low-cost environmental sensors. The approach:
- Reduced temperature MAE from 3.84°C to 0.80°C
- Reduced humidity MAE from 14.18% to 1.15%
- Maintained computational efficiency for embedded systems
- Provided mathematically sound calibration with interpretable coefficients
This calibration enables cost-effective deployment of environmental monitoring systems while maintaining measurement accuracy suitable for energy optimization applications.
Future Work: Exploring non-linear calibration models and automated recalibration procedures to account for sensor aging and environmental changes.