Sub topic 9
Celsius and Fahrenheit
This chapter assumes you can wire a DHT11 or DHT22 sensor, install the Adafruit DHT library, and read values in an Arduino sketch. It also assumes you know the basic setup() and loop() structure and can view messages in the Serial Monitor.
Two temperature scales
A DHT sensor measures temperature internally and reports it as a number. The Adafruit DHT library returns Celsius by default.
- Celsius (°C) is commonly used in science and most countries.
- Fahrenheit (°F) is commonly used in the United States.
The scales use different zero points and different-sized degrees. Water freezes at 0 °C, which is 32 °F. Water boils at 100 °C, which is 212 °F at standard pressure.
Use these formulas to convert between the scales:
| Conversion | Formula |
|---|---|
| Celsius to Fahrenheit | `°F = °C × 9 / 5 + 32` |
| Fahrenheit to Celsius | `°C = (°F - 32) × 5 / 9` |
For example, 25 °C is:
25 × 9 / 5 + 32 = 77 °FRequesting Fahrenheit from the library
Call readTemperature() with no argument for Celsius. Pass true to request Fahrenheit.
float temperatureC = dht.readTemperature();
float temperatureF = dht.readTemperature(true);
float humidity = dht.readHumidity();
if (!isnan(temperatureC) &&
!isnan(temperatureF) &&
!isnan(humidity)) {
Serial.print(F("Temperature: "));
Serial.print(temperatureC);
Serial.print(F(" C / "));
Serial.print(temperatureF);
Serial.print(F(" F, Humidity: "));
Serial.print(humidity);
Serial.println(F(" %"));
}Both temperature values come from the same sensor. The library performs the conversion when you pass true. You can also convert a Celsius value yourself:
float temperatureF = temperatureC * 9.0 / 5.0 + 32.0;Using decimal constants such as 9.0 and 5.0 makes the calculation clearly use floating-point arithmetic.
Passing the correct unit to calculations
Some library functions need to know which temperature scale you are using. computeHeatIndex() expects Fahrenheit by default. If you pass Celsius, provide false as the third argument.
float heatIndexF = dht.computeHeatIndex(temperatureF, humidity);
float heatIndexC = dht.computeHeatIndex(temperatureC, humidity, false);The heat index estimates how hot the air feels when humidity affects the body's cooling. It is useful mainly in warm conditions. It is not simply another temperature reading, so do not display it as the actual air temperature.
Follow the unit through your program
A useful data flow is:
Choose one primary unit for your program. Convert only at the boundaries, such as when printing a user-facing value. This reduces mistakes when comparing readings or applying thresholds.
For example, if an alarm should trigger at 30 °C, compare the Celsius value directly:
if (temperatureC >= 30.0) {
Serial.println(F("Temperature is high"));
}Do not compare that value with 86 unless you have first converted it to Fahrenheit.
Exercise
What Fahrenheit value is equivalent to 20 °C, and how would you request that unit from the Adafruit DHT library?
Show answer
20 °C is 68 °F. Call dht.readTemperature(true) to request Fahrenheit.