Arduino – Measure temperature and humidity (DHT22)

1. Hardware

Wire as shown below :
DHT22_bb

DHT22_schema

2. Software

2.1 DHT22 library installation

An open-source library is available on github : https://github.com/adafruit/DHT-sensor-library

1 – Download ZIP :
github-download-zip

2 – Import the new library :
arduino-import-library

2.2 Program


//include library
#include "DHT.h"
//Define the input pin (pin #2)
#define I_DHT22 2
//Define the sensor type (here DHT22)
#define DHTTYPE DHT22
//Object declaration
DHT dht(I_DHT22, DHTTYPE);

void setup() {
//Serial port communication
Serial.begin(9600);
Serial.println("DHT22 - Test");
//Initialization
dht.begin();
}

void loop() {
//2 seconds delay between measurements
delay(2000);

//Temperature (Celcius)
float temperature = dht.readTemperature();
if (isnan(temperature)) {
Serial.println("Failed to read temperature from DHT sensor.");
}
else {
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" *C\r\n");
}

//Humidity
float humidity = dht.readHumidity();
if (isnan(humidity)) {
Serial.println("Failed to read humidity from DHT sensor.");
}
else {
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\r\n");
}

//Heat index
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Unable to compute heat index (missing parameters).");
}
else {
float heatIndex = dht.computeHeatIndex(temperature, humidity, false);
if (isnan(heatIndex)) {
Serial.println("Failed to compute heat index.");
}
else {
Serial.print("Heat index: ");
Serial.print(heatIndex);
Serial.print(" *C\r\n");
}
}
}

2.3 Result

DHT22-result

One thought on “Arduino – Measure temperature and humidity (DHT22)”

  1. Hi dear I got this issue
    WARNING: Spurious .github folder in ‘DHT sensor library’ library

Leave a Reply

Your email address will not be published. Required fields are marked *