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:
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:
Since the sensor is slow, so we need a delay (about 2 seconds) between measurements.
4. Result
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
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.
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
7 Comments
[ESP32 3.3V - DHT22 VCC]
[ESP32 IO14 - DHT22 DATA]
[ESP32 GND - DHT22 GND]
// 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