Measure Temperature and Humidity with DHT22

Measure Temperature and Humidity

Objective

Use the DHT22 sensor to measure temperature and humidity levels in an environment and display the results using an Arduino board.

Required Components

Working Principle

The DHT22 sensor uses a capacitive humidity sensor and a thermistor to measure temperature and humidity. It outputs data in a digital signal to the Arduino for processing.

Circuit Diagram

DHT22 Circuit Diagram

Connect the sensor as follows:

Arduino Code


#include 

#define DHTPIN 2  // DHT22 connected to digital pin 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);

void setup() {
    Serial.begin(9600);
    dht.begin();
}

void loop() {
    float temperature = dht.readTemperature();
    float humidity = dht.readHumidity();

    if (isnan(temperature) || isnan(humidity)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
    }

    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.println(" °C");
    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.println(" %");
    delay(2000);
}
            

Results

The serial monitor will display the temperature and humidity values, refreshed every 2 seconds.

Applications

Conclusion

The DHT22 sensor is an excellent tool for accurately measuring temperature and humidity, suitable for a wide range of applications.