6.1 Temperature and Humidity Sensors

6.1.1 Introduction
Temperature and humidity sensors are used to measure the environmental temperature and the amount of moisture present in the air.
Temperature is normally measured in degrees Celsius (°C), while relative humidity is expressed as a percentage (%RH).
Common temperature and humidity sensors used with Arduino include:
- DHT11
- DHT22
- DHT21
- SHT series sensors
- BME280
The DHT11 is commonly used for beginner Arduino projects because it is inexpensive and simple to interface.
6.1.2 Types of Temperature and Humidity Sensors
DHT11
The DHT11 can measure:
- Temperature
- Relative humidity
It is suitable for simple educational and monitoring applications.
DHT22
The DHT22 provides better accuracy and a wider measurement range than the DHT11.
BME280
The BME280 can measure:
- Temperature
- Humidity
- Atmospheric pressure
It is commonly used in weather stations and environmental monitoring systems.
6.1.3 Working Principle
A temperature and humidity sensor detects environmental conditions and converts them into electrical data.
For a DHT sensor:
- The Arduino sends a request to the sensor.
- The sensor measures temperature and humidity.
- The sensor converts the measurements into digital data.
- The data is transmitted to the Arduino.
- The Arduino processes and displays the values.
6.1.4 Arduino Interface and Connections
Example using DHT11:
|
DHT11 Pin |
Arduino |
|
VCC |
5V |
|
DATA |
Digital Pin 2 |
|
GND |
GND |
Some bare DHT sensors require a pull-up resistor on the data line, while many modules already include one.
6.1.5 Arduino Programming and Code
The DHT library must be installed before running the program.
#include <DHT.h>
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println(“Sensor reading failed”);
return;
}
Serial.print(“Temperature: “);
Serial.print(temperature);
Serial.println(” °C”);
Serial.print(“Humidity: “);
Serial.print(humidity);
Serial.println(” %”);
delay(2000);
}
6.1.6 Code Description
- #include <DHT.h> loads the DHT sensor library.
- DHTPIN 2 specifies Arduino digital pin 2.
- DHTTYPE DHT11 specifies the sensor type.
- dht.begin() initializes the sensor.
- readHumidity() obtains humidity.
- readTemperature() obtains temperature.
- Serial.print() displays the values.
- isnan() checks whether a valid measurement was received.
- delay(2000) waits two seconds before another reading.
6.1.7 Applications
Temperature and humidity sensors are used in:
- Weather stations
- Greenhouses
- Smart homes
- Server rooms
- Hospitals
- Agriculture
- Food storage
- Industrial monitoring
- IoT environmental monitoring


