Sub topic 6
Reading Temperature and Humidity with a Sensor
Prerequisites and the reading process
This chapter assumes that you can write setup() and loop(), upload a sketch, open the Serial Monitor, and connect components on a breadboard. You also need an Arduino, a DHT11 or DHT22 sensor, and the Adafruit DHT library.
A DHT sensor reports two related measurements:
- Temperature is how hot or cold the air is.
- Relative humidity is the amount of water vapor in the air compared with the maximum amount the air could hold at that temperature. It is expressed as a percentage.
Relative humidity depends on temperature. Warm air can hold more water vapor than cold air. The same amount of vapor can therefore produce different humidity readings when the temperature changes.
The library communicates with the sensor and converts its response into float values. A float stores a number that can contain a fractional part.
Install the library and read both values
In the Arduino IDE, open Library Manager and install “DHT sensor library” by Adafruit. Install any dependency that the IDE requests, such as Adafruit Unified Sensor.
The following sketch uses a DHT22 connected to digital pin 2. Change DHTTYPE to DHT11 if you use a DHT11.
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
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;
}
Serial.print(F("Humidity: "));
Serial.print(humidity);
Serial.print(F(" % Temperature: "));
Serial.print(temperatureC);
Serial.println(F(" C"));
}The call to readHumidity() returns relative humidity in percent. The call to readTemperature() returns Celsius by default. isnan() checks whether a value is “not a number.” The library returns this special value when communication fails or the sensor response is unusable.
The return statement exits the current pass through loop(). It does not stop the Arduino. On the next pass, the sketch waits and tries again.
Sampling interval and common errors
DHT sensors are slow. A DHT22 normally needs at least two seconds between readings. Reading faster can produce repeated, stale, or invalid results. The delay(2000) call enforces this interval, but it also pauses every other task in the sketch.
Check these causes when readings fail:
- The sensor’s power and ground are reversed.
- The data wire is connected to a different pin than
DHTPIN. - A bare four-pin sensor lacks a pull-up resistor, commonly about 10 kilohms, between data and power.
- The selected type does not match the physical sensor.
- The sensor is being sampled too quickly.
- The Serial Monitor baud rate is not 9600.
A sensor breakout board may already include the pull-up resistor. Check its documentation before adding another one.
Fahrenheit output
Pass true to readTemperature() to request Fahrenheit:
float temperatureF = dht.readTemperature(true);
if (!isnan(temperatureF)) {
Serial.print(F("Temperature: "));
Serial.print(temperatureF);
Serial.println(F(" F"));
}Check the value with isnan() before printing or using it in calculations.
Exercise
Why should the sketch check both humidity and temperature with isnan() before using either value?
Show answer
A failed sensor transaction can make either result invalid. Checking both prevents the sketch from displaying or calculating with unusable data.