DHT22 with ESP32: Accurate Temperature and Humidity Monitoring

While building a small home automation setup recently, I needed accurate temperature and humidity readings. The basic DHT11 wasn’t cutting it, so I switched to the DHT22 with ESP32, and the results were far more reliable. The DHT22 offers better accuracy, a wider sensing range, and more consistent output, which makes it a great choice for smart home projects, greenhouse monitoring, or environmental logging.

In this guide, we’ll walk you through the full process of using the DHT22 sensor with an ESP32 board. From wiring and library setup to writing and uploading the code, we’ll cover everything step by step. Whether you’re building a weather station or just want to track indoor conditions, this tutorial has you covered.


What is the DHT22 Sensor?

The DHT22, also known as the AM2302, is a popular digital sensor used for measuring both temperature and relative humidity. It’s the upgraded version of the DHT11 sensor and is known for its higher accuracy, wider measurement range, and greater reliability.

Internally, the DHT22 uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and it outputs a calibrated digital signal via a single data pin. This makes it perfect for interfacing with microcontrollers like ESP32 and Arduino and that’s why DHT22 is a go-to choice for many electronics projects that require environmental sensing.

The sensor is compact, energy-efficient, and very easy to use in ESP32-based IoT projects. Its improved performance compared to the DHT11 makes DHT22 an excellent solution for projects that require more precise and stable environmental data — like indoor climate control systems, smart greenhouses, or long-term data logging applications.


DHT11 vs. DHT22: Key Differences

dht11-and-dht22

Here’s how DHT11 compares with DHT22:

FeatureDHT11DHT22
Temperature Range0-50 °C-40 to 80 °C
Temperature Accuracy±2 °C±0.5 °C
Humidity Range20-90% RH0-100% RH
Humidity Accuracy±5% RH±2-5% RH
Sampling Rate1 Hz (1 second)0.5 Hz (2 seconds)
CostCheaperSlightly more expensive

If you want better precision and a wider operating range, DHT22 with ESP32 is the preferred choice over DHT11.


Required Components

To build this, you’ll need the following components:

  • ESP32 Development Board
  • DHT22 Sensor Module
  • Breadboard and Jumper Wires
  • (Optional) 10kΩ Pull-up Resistor

DHT22 Pinout

DHT22 sensors usually have 3 or 4 pins depending on the module:

PinDescription
VCCPower supply (3.3V–5V)
GNDGround
DATADigital output signal
DHT22-pinout
DHT22 Pinout

When using DHT22 sensor with ESP32, the DATA pin can be connected to any digital pin. In this example, we’ll use GPIO 4.


Circuit Diagram: DHT22 with ESP32

Here’s how to connect the DHT22 sensor to ESP32:

DHT22 PinESP32 Pin
VCC3.3V
GNDGND
DATAGPIO 4
DHT22_with_ESP32_Wiring
DHT22 Sensor with ESP32 Wiring

📝 Note: A 10kΩ pull-up resistor between VCC and DATA is recommended for more stable readings.

📌 See ESP32 Pinout Reference:
If you’re unsure about the ESP32 pin numbers, check out our full ESP32 Pinout Guide for clarity.


Installing the DHT Library

Before using the DHT22 sensor, you need to install the DHT library in the Arduino IDE:

  1. Open the Arduino IDE and go to Sketch > Include Library > Manage Libraries…
  2. In the Library Manager, search for DHT Sensor Library by Adafruit.
  3. Click Install and wait for the installation to complete.

For more details or manual installation, you can visit the Adafruit DHT Sensor Library GitHub repository.


ESP32 Code for DHT22

Here’s a complete code example to read temperature and humidity using DHT22 with ESP32.

// Reading DHT22 with ESP32 by ArduinoYard
#include "DHT.h"

#define DHTPIN 4      // GPIO 4 connected to DHT22 DATA pin
#define DHTTYPE DHT22  // Define sensor type

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
  Serial.println("Reading from DHT22 sensor...");
}

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

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

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

  delay(2000); // 2-second delay to match sampling rate
}

Code Breakdown

  • Pin Definition: We use GPIO 4 for the DHT22 data line.
  • Sensor Initialization: dht.begin() starts communication with the sensor.
  • Reading Values: readTemperature() and readHumidity() fetch the data.
  • Validation: isnan() checks if readings are valid before printing.

This code gives real-time environmental data using DHT22 with ESP32.


Output Example

Hardware:

DHT22-ESP32-Hardware

Serial Monitor:

Serial-DHT22-ESP32

The values are updated every 2 seconds, which matches the sampling rate of the DHT22.


Troubleshooting DHT22 with ESP32

Problem: Serial Monitor says “Failed to read from DHT22 sensor!”
✅ Check connections and power supply
✅ Ensure you’ve selected DHT22, not DHT11 in the code
✅ Add 10kΩ pull-up resistor between VCC and DATA

Problem: Random or unstable values
✅ Use short jumper wires
✅ Try powering from 3.3V instead of 5V
✅ Ensure no moisture is affecting the sensor


Applications of DHT22 with ESP32

📦 Storage Monitoring – Maintain proper climate in storerooms

🌡️ Weather Stations – Log real-time weather data

🏠 Smart Home Projects – Control appliances based on climate

🌿 Greenhouse Automation – Monitor and adjust humidity

📊 IoT Dashboards – Send sensor data to online dashboards


Conclusion

Pairing the DHT22 sensor with an ESP32 board is a simple and cost-effective way to bring reliable temperature and humidity monitoring into your projects. Thanks to its higher accuracy and wider range compared to the DHT11, it’s well-suited for everything from beginner experiments to more advanced IoT systems.

Once you’ve mastered the basics, try expanding your project by displaying the data on an OLED screen, uploading it to a cloud dashboard, or even triggering smart devices based on the readings. The possibilities are wide open.


Explore More ESP32 Guides & Projects

Leave a Comment