Sub topic 10
Computing heat index and dew point
Temperature and relative humidity are the two values a DHT sensor measures. Two other numbers are often more useful for real decisions: how hot it feels, and the temperature at which condensation starts. This chapter shows how to compute both on the Arduino.
This chapter assumes you can already read a DHT11 or DHT22 with the Adafruit DHT library. It also assumes you check for NaN (not a number) on failed reads and know the sensor's accuracy limits. You should be comfortable with float math and calling functions from loop().
Why derive values at all
Relative humidity depends on temperature. The same water vapor gives a high percentage in cool air and a low percentage in warm air. Two readings such as "20 C, 80 %" and "30 C, 50 %" are hard to compare directly. Derived values fix this.
| Value | What it tells you | Typical use |
|---|---|---|
| Heat index | How hot the air feels to a person, combining temperature and humidity | Comfort and heat-safety alerts |
| Dew point | The temperature at which the air becomes saturated and water condenses | Mold risk, window condensation, frost prediction |
Heat index is a perception measure. Dew point is a physical measure: it tracks the actual amount of water vapor in the air and does not change when the air warms up or cools down (as long as no moisture is added or removed).
Heat index with the library
The Adafruit library includes computeHeatIndex(). It takes the temperature, the relative humidity, and a flag that says which unit the temperature is in.
float temperatureF = dht.readTemperature(true);
float temperatureC = dht.readTemperature();
float humidity = dht.readHumidity();
if (!isnan(temperatureF) && !isnan(temperatureC) && !isnan(humidity)) {
float heatIndexF = dht.computeHeatIndex(temperatureF, humidity);
float heatIndexC = dht.computeHeatIndex(temperatureC, humidity, false);
}The third argument is isFahrenheit and defaults to true. This is the most common mistake with this function. If you pass a Celsius value and leave the flag out, the library treats 25 as 25 F and returns a meaningless result. Pass false whenever the temperature is in Celsius, and the function returns Celsius.
How the library calculates it
The heat index formula comes from the US National Weather Service. It works in degrees Fahrenheit internally. The library first uses a simple linear approximation. If the result is cool (below about 80 F), it returns that value. Otherwise it applies the full Rothfusz regression, a polynomial in temperature and humidity, with small corrections for very dry or very humid air.
Because the formula was fitted to data from hot conditions, it has limits:
- Below roughly 27 C (80 F), the heat index is not meaningful. The result is close to the air temperature.
- Below about 40 % humidity, the heat index can come out lower than the air temperature. This is a known effect of the formula and not a bug in your sketch.
- The model assumes shade and light wind. In direct sun, the real "feels like" value is higher.
Dew point with the Magnus formula
The library has no dew point function, so you write one. The Magnus approximation is a short formula that is accurate to a fraction of a degree for temperatures from about 0 to 60 C.
float dewPointC(float tempC, float humidityPercent) {
const float a = 17.62;
const float b = 243.12;
float gamma = log(humidityPercent / 100.0) + (a * tempC) / (b + tempC);
return (b * gamma) / (a - gamma);
}The constants a and b are fixed values for the formula. They are valid over liquid water. Below 0 C, the formula gives a dew point over supercooled water. A true frost point would need different constants, so treat results below freezing as approximate.
In Arduino C++, log() is the natural logarithm, which is what the formula needs. Do not use log10() here.
A worked example
Take a reading of 30 C and 60 % humidity.
- Convert humidity to a fraction:
ln(0.60) = -0.5108. - Compute the temperature term:
17.62 * 30 / (243.12 + 30) = 1.9355. - Add them:
gamma = 1.4247. - Compute the dew point:
243.12 * 1.4247 / (17.62 - 1.4247) = 21.4.
The dew point is about 21.4 C. If a surface in that room cooled to 21.4 C, water would condense on it. For the same reading, the heat index is about 35 C (95 F), so the air feels noticeably hotter than the thermometer says.
Guarding the edge cases
The formula breaks for a humidity of 0 or less, because log(0) is negative infinity and the result becomes NaN. A DHT sensor should not report 0 % in normal use, but a calibration offset can push a low value to 0 or below. Add a guard:
float safeDewPointC(float tempC, float humidityPercent) {
if (isnan(tempC) || isnan(humidityPercent) || humidityPercent <= 0.0) {
return NAN;
}
return dewPointC(tempC, humidityPercent);
}Returning NAN keeps the failure signal consistent with the sensor read functions. The calling code can then use the same isnan check for every value.
A complete sketch
This sketch reads the sensor every two seconds, validates the values, and prints temperature, humidity, heat index, and dew point in Celsius. It uses a DHT22 on pin 2. For a DHT11, change DHTTYPE to DHT11.
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
float dewPointC(float tempC, float humidityPercent) {
const float a = 17.62;
const float b = 243.12;
float gamma = log(humidityPercent / 100.0) + (a * tempC) / (b + tempC);
return (b * gamma) / (a - gamma);
}
float safeDewPointC(float tempC, float humidityPercent) {
if (isnan(tempC) || isnan(humidityPercent) || humidityPercent <= 0.0) {
return NAN;
}
return dewPointC(tempC, humidityPercent);
}
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float temperatureC = dht.readTemperature();
if (isnan(humidity) || isnan(temperatureC)) {
Serial.println(F("Failed to read from DHT sensor!"));
return;
}
float heatIndexC = dht.computeHeatIndex(temperatureC, humidity, false);
float dewC = safeDewPointC(temperatureC, humidity);
Serial.print(F("T: "));
Serial.print(temperatureC);
Serial.print(F(" C RH: "));
Serial.print(humidity);
Serial.print(F(" % HI: "));
Serial.print(heatIndexC);
Serial.print(F(" C Dew: "));
Serial.print(dewC);
Serial.println(F(" C"));
}With the 30 C and 60 % example, expect a heat index near 35 C and a dew point near 21.4 C. Small differences of a few tenths of a degree are normal, because the sensor values fluctuate.
Order of operations with calibration
Apply any calibration offset before you compute derived values. A derived value built from an uncorrected reading carries the sensor's error forward, and the dew point formula amplifies a humidity error in particular.
float rawT = dht.readTemperature();
float rawH = dht.readHumidity();
if (!isnan(rawT) && !isnan(rawH)) {
float t = calibratedTemp(rawT);
float h = calibratedHumidity(rawH);
float dew = safeDewPointC(t, h);
}Here calibratedTemp() and calibratedHumidity() are the offset helpers from the accuracy chapter. Because calibratedHumidity() limits the result to 0 to 100 %, the dew point guard for zero humidity matters in practice.
How sensor error carries into the result
As a rough rule at moderate conditions, a 5 % error in relative humidity changes the dew point by about 1 C. A DHT11 with ±5 % humidity accuracy therefore gives a dew point that may be off by about a degree, plus the effect of its ±2 C temperature error. A DHT22 with ±2 to 5 % humidity accuracy does better, but the dew point is still only as good as the humidity reading. If a project needs a dew point you can trust to a fraction of a degree, a better sensor is the fix. No formula can add that precision.
Common errors
- Forgetting
falseincomputeHeatIndex. Celsius input with the default Fahrenheit flag gives a wrong, often absurdly low, value. - Computing before the
isnancheck. ANaNinput passes silently through the math and produces aNaNoutput that looks like a formula problem. - Mixing units.
dewPointC()expects Celsius. If you pass a Fahrenheit reading, the result is wrong without any error. - Trusting the heat index in cool air. Below about 27 C, report the air temperature instead. A simple check such as
if (temperatureC < 27.0)keeps your output honest. - Using integer division. Write
100.0, not100, so the humidity fraction stays a float. In the Magnus code above,humidityPercent / 100.0is already correct becausehumidityPercentis afloat.
Exercise
A DHT22 reports 25.0 C and 50 % humidity. You call dht.computeHeatIndex(25.0, 50.0) and get a result near 77. Explain what went wrong and show the corrected call.
Show answer
The third argument defaults to true, so the library treated 25.0 as 25 F and returned a Fahrenheit-based value that does not match the real conditions. Pass false to say the temperature is in Celsius: dht.computeHeatIndex(25.0, 50.0, false). The result is then in Celsius. At 25 C the heat index will also be close to the air temperature, because the formula is only meaningful above about 27 C.
Exercise
Your sketch applies a humidity offset of -3.0 and the sensor reports 2.0 %. What does dewPointC() do with the calibrated value, and how do you prevent the problem?
Show answer
The calibrated value is -1.0 %, so log(-0.01) is not a number and the dew point becomes NaN. Prevent it by limiting the calibrated humidity with constrain(value, 0.0, 100.0) and by using a guard like safeDewPointC(), which returns NAN when humidity is 0 or less. The sketch can then test the result with isnan before printing it.