ESP32 - Temperature Humidity Sensor

In this tutorial, we are going to learn how to read temperature and humidity value from DHT11 or DHT22 sensors

DHT11 and DHT22 are sensors that we can use to measure temperature and humidity. They are similar to each other. You can choose one of them, depending on your needs

If you only want to get temperature, we recommend using a waterproof DS18B20 temperature sensor instead. It is an inexpensive and neat sensor. You can put it in hot or cold water.

Hardware Required

1×ESP-WROOM-32 Dev Module
1×Micro USB Cable
1×Temperature and Humidity Sensor DHT11
1×Temperature and Humidity Sensor DHT22
1×10 kΩ resistor
1×Breadboard
n×Jumper Wires
Please note: These are affiliate links. If you buy the components through these links, We may get a commission at no extra cost to you. We appreciate it.

About DHT11 and DHT22 Temperature and Humidity Sensor

The commons between DHT11 and DHT22

  • Pinouts are the same
  • Wiring to ESP32 is the same
  • Programming (with a library) is similar (only one line of code is different)

The differences between DHT11 and DHT22

DHT11 DHT22
Price ultra low cost low cost
Temperature Range 0°C to 50°C -40°C to 80°C
Temperature Accuracy ± 2°C ± 0.5°C
Humidity Range 20% to 80% 0% to 100%
Humidity Accuracy 5% ± 2 to 5%
Reading Rate 1Hz (once every second) 0.5Hz (once every 2 seconds)
Body size 15.5mm x 12mm x 5.5mm 15.1mm x 25mm x 7.7mm

As you can see, the DHT22 is a little more accurate, larger range but more expensive than DHT11.

Pinout

DHT11 and DHT22 sensor in original form have four pins:

  • GND pin needs to be connected to GND (0V)
  • VCC pin needs to be connected to VCC (5V)
  • DATA pin the pin is used to communicate between the sensor and ESP32
  • NC pin Not connected, we can ignore this pin
  • DHT11 and DHT22 temperature and humidity sensor Pinout

Some manufacturers provide DHT11 and DHT22 sensor in module form with three pins: GND, VCC and DATA pins.

※ NOTE THAT:

In module form, the order of the module's pins can vary between manufacturers. ALWAYS use the labels printed on the module. Look closely!

DHT11 and DHT22 temperature and humidity module Pinout

Wiring Diagram

Wiring to ESP32 is the same for both sensors. In orginal form, A resistor from 5K to 10K Ohms is required to keep the data line high and in order to enable the communication between the sensor and the ESP32

ESP32 - DHT11 Sensor Wiring

ESP32 DHT11 Temperature and humidity Sensor Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

ESP32 - DHT22 Sensor Wiring

ESP32 DHT22 Temperature and humidity Sensor Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

ESP32 - DHT11 Module Wiring

Most of DHT22 sensor modules have a built-in resistor, so you don't need to add it. it saves us some wiring or soldering works.

ESP32 DHT11 Temperature and humidity Module Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

ESP32 - DHT22 Module Wiring

Most of DHT22 sensor modules have a built-in resistor, so you don't need to add it. it saves us some wiring or soldering works.

ESP32 DHT22 Temperature and humidity Module Wiring Diagram

Image is developed using Fritzing. Click to enlarge image

How To Program For DHTxx Temperature Sensor

Programming for both sensors is similar. There is only one line of code different.

  • Include the library:
#include "DHT.h"
  • Define the ESP32 pin connected to DHT sensor, for example GIOP21:
#define DHTPIN 21
  • Define the sensor type: DHT11 or DHT22 (This is the different line of code)
#define DHTTYPE DHT11

or

#define DHTTYPE DHT22
  • Declare DHT object
DHT dht(DHTPIN, DHTTYPE);
  • Initialize the sensor:
dht.begin();
  • Read humidity:
float humi = dht.readHumidity();
  • Read temperature in Celsius:
float tempC = dht.readTemperature();
  • Read temperature in Fahrenheit:
float tempF = dht.readTemperature(true);

ESP32 Code - DHT11

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-temperature-humidity-sensor */ #include "DHT.h" #define DHTPIN 21 // GIOP21 #define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); // initialize the sensor } void loop() { // wait a few seconds between measurements. delay(2000); // read humidity float humi = dht.readHumidity(); // read temperature as Celsius float tempC = dht.readTemperature(); // read temperature as Fahrenheit float tempF = dht.readTemperature(true); // check if any reads failed if (isnan(humi) || isnan(tempC) || isnan(tempF)) { Serial.println("Failed to read from DHT sensor!"); } else { Serial.print("Humidity: "); Serial.print(humi); Serial.print("%"); Serial.print(" | "); Serial.print("Temperature: "); Serial.print(tempC); Serial.print("°C ~ "); Serial.print(tempF); Serial.println("°F"); } }

ESP32 Code - DHT22

/* * Created by esp32io.com * * This example code is in the public domain * * Tutorial page: https://esp32io.com/tutorials/esp32-temperature-humidity-sensor */ #include "DHT.h" #define DHTPIN 21 // GIOP21 #define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); // initialize the sensor } void loop() { // wait a few seconds between measurements. delay(2000); // read humidity float humi = dht.readHumidity(); // read temperature as Celsius float tempC = dht.readTemperature(); // read temperature as Fahrenheit float tempF = dht.readTemperature(true); // check if any reads failed if (isnan(humi) || isnan(tempC) || isnan(tempF)) { Serial.println("Failed to read from DHT sensor!"); } else { Serial.print("Humidity: "); Serial.print(humi); Serial.print("%"); Serial.print(" | "); Serial.print("Temperature: "); Serial.print(tempC); Serial.print("°C ~ "); Serial.print(tempF); Serial.println("°F"); } }

If you compare two above code, you realize that there is one line different (line 11)

Quick Steps

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE, select the right ESP32 board and COM port
  • On Arduino IDE, Go to Tools Manage Libraries
  • ESP32 add library
  • Search “DHT”, then find the DHT sensor library by Adafruit
  • Click Install button to install the library.
  • ESP32 DHT sensor library
  • You will be asked for intalling some other library dependencies
  • Click Install All button to install all library dependencies.
  • ESP32 Adafruit Unified sensor library
  • Copy the above code corresponding to the sensor you have and open with Arduino IDE
  • Click Upload button on Arduino IDE to compile and upload code to ESP32 board
  • Make enviroment around sensor hotter or colder
  • See the result on Serial Monitor. It looks like the below:.
  • COM6
    Send
    Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 27.00°C ~ 80.60°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 32.00% | Temperature: 28.00°C ~ 82.40°F Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F Humidity: 32.00% | Temperature: 29.00°C ~ 84.20°F Humidity: 31.00% | Temperature: 29.00°C ~ 84.20°F
    Autoscroll Show timestamp
    Clear output
    9600 baud  
    Newline  

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

We are considering to make the tutorial videos. If you think the tutorial videos are essential, please subscribe to our Youtube channel. It will give us lots of motivation for making the videos.

Alternative DHT11 and DHT22 sensors

※ NOTE THAT:

Note that this tutorial is incomplete. We will post on our Facebook Page when the tutorial is complete. Like it to get updated.

※ NOTICES

  • We are AVAILABLE for HIRE. See how to hire us to do your project
  • If this tutorial is useful for you, please support us to make more tutorials.
  • We spent a lot of time and effort to create the content for this tutorial, please respect our work! Please do not copy the content to share on other websites. Howerver, please feel free to share the link of this tutorial anywhere