Sub topic 11
Accuracy Limits and Calibration Offsets
This chapter assumes you can wire a DHT11 or DHT22, read values with the Adafruit DHT library, and check for NaN. It covers how far you can trust those readings and how to correct a consistent error with a simple offset.
Accuracy, precision, and resolution
Three terms are easy to confuse, and datasheets use all of them.
- Resolution is the smallest step the sensor reports. A DHT22 reports 0.1 °C and 0.1 % relative humidity.
- Precision (repeatability) is how closely repeated readings agree when the conditions do not change.
- Accuracy is how close a reading is to the true value.
A sensor can have fine resolution and poor accuracy. A DHT22 printing 23.4 looks exact, but the true temperature may be 22.9 or 23.9. The extra digit tells you about the display format, not about the room.
Published limits of the DHT family
The figures below are typical datasheet values. Individual parts and suppliers vary, so check the datasheet for the module you bought.
| Property | DHT11 | DHT22 |
|---|---|---|
| Temperature range | 0 to 50 °C | -40 to 80 °C |
| Temperature accuracy | about ±2 °C | about ±0.5 °C |
| Temperature resolution | 1 °C | 0.1 °C |
| Humidity range | 20 to 80 % | 0 to 100 % |
| Humidity accuracy | about ±5 % | about ±2 to 5 % |
| Humidity resolution | 1 % | 0.1 % |
| Minimum read interval | 1 s | 2 s |
The humidity error of a DHT22 depends on conditions. It is usually best in mid-range humidity and worse near the extremes. Treat the quoted figure as a rough bound, not a guarantee.
Sources of error
Not every error comes from the sensor chip. Several causes are common in hobby builds.
- Self-heating from the board. A sensor mounted next to a voltage regulator, a display, or a microcontroller reads warm. Keep it a few centimeters away from heat sources, or on a short cable.
- Airflow. Enclosed sensors respond slowly. A sensor in a sealed box lags behind the room by many minutes.
- Sunlight and drafts. Direct sun on the plastic body raises the temperature reading. A draft from a window lowers it.
- Condensation and saturation. After exposure to very high humidity, a sensor can read high for hours while it dries. Repeated condensation shortens its life.
- Supply voltage and wiring. A weak supply or a long data wire can cause failed readings or corrupt values. Failed readings show up as
NaN, covered in the earlier chapter on error handling. - Drift. Humidity elements age. Expect a slow shift of around 0.5 % per year or more in harsh environments.
Because relative humidity depends on temperature, a temperature error also corrupts humidity. If the sensor reads 1 °C warm, the air around the sensor is not the air in the room, and the humidity value describes the warmer air. Fix thermal problems first, then calibrate.
What a calibration offset can and cannot fix
A calibration offset is a fixed number added to every reading. It corrects a systematic error, one that is the same each time. If your sensor always reads 0.5 °C high, subtracting 0.5 °C removes the error.
An offset cannot fix a random error (noise from one reading to the next). It also cannot fix an error that changes with the measurement, for example a sensor that is 0.2 °C off at 10 °C and 1.0 °C off at 40 °C. For that case you need a gain correction or a multi-point calibration, described below.
Measuring the offset
You need a reference instrument that is more accurate than the sensor. A good calibrated lab thermometer or hygrometer works. Cheap reference devices are often no better than the DHT22, so a comparison against one only tells you how the two disagree.
For humidity there is a well-known reference that needs no instrument: a saturated salt solution. A sealed container holding table salt (sodium chloride) mixed with a little water, with the sensor suspended above the liquid, settles at about 75 % relative humidity at room temperature. This takes many hours and must stay at a stable temperature. Treat it as a check of a single point, not a full calibration.
A practical procedure:
- Place the sensor and the reference side by side in a stable spot, away from heat sources, for at least 30 minutes.
- Log both readings for several minutes.
- Average the sensor readings and the reference readings.
- Subtract:
offset = reference average - sensor average.
Averaging matters. A single reading includes noise, and an offset based on it bakes the noise into every later value.
The following sketch prints a running average of 30 valid readings, so you can compare it to your reference. It assumes a DHT22 on pin 2.
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
const int SAMPLES = 30;
float tempSum = 0;
float humSum = 0;
int count = 0;
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
tempSum += t;
humSum += h;
count++;
if (count == SAMPLES) {
Serial.print(F("Average temperature: "));
Serial.print(tempSum / SAMPLES);
Serial.print(F(" C Average humidity: "));
Serial.print(humSum / SAMPLES);
Serial.println(F(" %"));
tempSum = 0;
humSum = 0;
count = 0;
}
}Each average takes about a minute, because the loop waits two seconds per reading. Failed readings are skipped and do not count toward the 30.
Applying the offset in code
Keep the offsets in named constants at the top of the sketch. Then the values are easy to find and change when you recalibrate.
const float TEMP_OFFSET_C = -0.5;
const float HUMIDITY_OFFSET = 1.0;
float calibratedTemp(float rawC) {
return rawC + TEMP_OFFSET_C;
}
float calibratedHumidity(float rawPercent) {
return constrain(rawPercent + HUMIDITY_OFFSET, 0.0, 100.0);
}The constrain call keeps humidity between 0 and 100 %. Without it, a raw reading of 99.8 % plus an offset of 1.0 would report an impossible 100.8 %.
Apply the correction to the raw value once, right after the read, and use the corrected value everywhere afterward. That includes derived values such as dew point and heat index. If you compute dew point from raw values and display corrected ones, the numbers will disagree.
float rawT = dht.readTemperature();
float rawH = dht.readHumidity();
if (!isnan(rawT) && !isnan(rawH)) {
float t = calibratedTemp(rawT);
float h = calibratedHumidity(rawH);
// Use t and h from here on.
}Check for NaN before applying the offset. Adding a number to NaN gives NaN, so the check would still work afterward, but constrain on NaN has undefined behavior in practice and should be avoided.
Offsets for Fahrenheit
If you read Fahrenheit with readTemperature(true), the offset must be in Fahrenheit too. A 0.5 °C offset is a 0.9 °F offset, because a temperature difference converts by the factor 1.8 without the 32 added. The safest approach is to read Celsius, apply the Celsius offset, and convert at the end.
Two-point calibration
When the error changes across the range, measure the offset at two points and fit a straight line. Suppose the sensor reads 10.4 °C when the reference reads 10.0 °C, and reads 40.9 °C when the reference reads 40.0 °C. Then:
gain = (refHigh - refLow) / (rawHigh - rawLow)corrected = refLow + (raw - rawLow) * gain
const float RAW_LOW = 10.4;
const float RAW_HIGH = 40.9;
const float REF_LOW = 10.0;
const float REF_HIGH = 40.0;
float twoPointCorrect(float raw) {
float gain = (REF_HIGH - REF_LOW) / (RAW_HIGH - RAW_LOW);
return REF_LOW + (raw - RAW_LOW) * gain;
}For most home projects, a single offset near the range you care about is enough. A second point is worth the effort only when the sensor works over a wide span, such as an outdoor logger that sees both winter and summer.
Humidity is harder to calibrate than temperature
Temperature is easy to compare against a reference. Humidity is not, for three reasons.
- Good humidity references are expensive and drift too.
- The error often varies across the range, so one offset helps at one humidity and hurts at another.
- The sensor needs time to reach equilibrium after any change, and the reading lags.
For these reasons, apply a humidity offset only when you have checked it at a humidity close to the one you will actually measure. Otherwise leave humidity uncorrected and note the uncertainty when you report it.
Reporting readings honestly
Match the displayed precision to the accuracy. A DHT22 with ±0.5 °C accuracy should usually be shown to one decimal place at most, and a DHT11 to whole degrees. Printing 23.4567 suggests a certainty the sensor does not have.
Choosing when to calibrate at all
Calibration is not always worth the time. A rough guide:
- If the project only needs "warm or cold" or "humid or dry", skip it.
- If you log trends over time, a consistent offset matters little, because the shape of the curve is the same.
- If you compare two sensors, or compare against a published figure, calibrate.
- If the accuracy you need is better than the datasheet figure, a calibration offset cannot get you there. Choose a better sensor, such as one of the I2C parts covered in a later chapter.
Exercise
A DHT22 reads an average of 24.3 °C over five minutes while a trusted reference thermometer beside it reads 23.8 °C. What offset constant should you use, and what is the corrected value for a raw reading of 25.0 °C? Why should the offset not be turned into Fahrenheit by adding 32?
Show answer
The offset is reference minus sensor: 23.8 - 24.3 = -0.5 °C, so TEMPOFFSETC = -0.5. A raw reading of 25.0 °C corrects to 25.0 + (-0.5) = 24.5 °C. The 32 in the Celsius to Fahrenheit formula accounts for the different zero points of the two scales. An offset is a difference between two temperatures, and the zero points cancel in a difference, so it converts with only the factor 1.8 (-0.5 °C is -0.9 °F).
Exercise
Your logger sits inside a closed plastic box that also holds a voltage regulator. After calibrating against a reference in open air, the readings are 1.5 °C too high whenever the box is powered for an hour. Will a larger offset solve this? What would you change instead?
Show answer
A larger offset is a poor fix. The error comes from self-heating, which grows with the time the box has been running and with the load, so it is not constant. A fixed offset would be right only at one moment. Move the sensor out of the box or away from the regulator on a short cable, add ventilation, and then repeat the calibration.