Demo 3: How to use Arduino ESP32 to read temperature/humidity from DHT11/DHT22

1. Introduction
The sensor module DHT11/DHT22 is used for measuring temperature/humidity. In this tutorial, we will learn how to use Arduino ESP32 to communicate with DHT11/DHT22 (this can also be applied for DHT11) to read value of temperature and humidity.
2. Hardware
Connect the pins of ESP32 to the pins of DHT22:
[ESP32 IO14 - DHT22 DATA]
[ESP32 3.3V - DHT22 VCC]
[ESP32 GND - DHT22 GND]
or follow the picture below to connect:
Figure: Connection between ESP32 and DHT22

3. Software
We will use the DHT22 library sensor which is supplied by Adafruit. You can download it here:
https://github.com/adafruit/DHT-sensor-library/archive/master.zip
After downloading, unzip it and copy unzipped folder to libraries folder under Arduino folder: C:/Users/[YOUR_USER_NAME]/Documents/Arduino/libraries
Note: If you already opened Arduino IDE, you need to restart Arduino IDE to get effect.
The library supplied some functions so that you can use them to read the data from DHT22 such as:
  • readHumidity(): to read humidity from sensor
  • readTemperature(bool flag): If argument flag is true then the temperature read from sensor is Fahrenheit value. If argument flag is false then the temperature read from sensor is Celsius value.
In order to use these function you must create an instance (variable) type DHT. E.g.: DHT dht(DHTPIN, DHTTYPE) . This will create an instance of DHT with 2 inputs (this is C++ constructor) are DHTPIN (the pin of ESP32 which connect to Data pin of DHT22) and DHTTYPE (this is DHT sensor type (DHT11 or DHT22))
Since the sensor is slow, so we need a delay (about 2 seconds) between measurements.
Now create a Arduino project and save it as esp32dht22. Here is the code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "DHT.h"
//here we use pin IO14 of ESP32 to read data
#define DHTPIN 14
//our sensor is DHT22 type
#define DHTTYPE DHT22
//create an instance of DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
  Serial.begin(115200);
  Serial.println("DHT22 sensor!");
  //call begin to start sensor
  dht.begin();
}

void loop() {
  //use the functions which are supplied by library.
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  // print the result to Terminal
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print(" %\t");
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" *C ");
  //we delay a little bit for next read
  delay(2000);
}

4. Result

Figure: output of DHT22

Post a Comment

7 Comments

Ruslan said…
You have a small error in the Hardware section. You have defined ground twice, and missing VCC/3.3V.

[ESP32 3.3V - DHT22 VCC]
[ESP32 IO14 - DHT22 DATA]
[ESP32 GND - DHT22 GND]
investment guru said…
Thanks for this tutorial.
Technonimus said…
Hello, Your post was very helpful, but when I upload the sketch to the ESP32, I just get a constant "Failed to read from DHT sensor" message. The pins are connected properly. Any advice?
Anonymous said…
// NOTE: For working with a faster chip, like an Arduino Due or Teensy, you
// might need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold. It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value. The default for a 16mhz AVR is a value of 6. For an
// Arduino Due that runs at 84mhz a value of 30 works.
// Example to initialize DHT sensor for Arduino Due:
//DHT dht(DHTPIN, DHTTYPE, 30);

A value of 30 worked for my 80Mhz Esp32 Wemos
Victor John said…
Thanks for sharing the tutorial of DHT11/DHT22, anyone can understand easily about installation and usages. I am looking for best External Temperature Sensor, which can help me everywhere.
Anonymous said…
Just to be clear. Is the server running async? can you make a request, leave the mcu running a task (for example, turn on the motor for 10 minutes, then turn it off), but send back 200 after assign the task (a few seconds after the request?