How to Use a DHT22 Sensor Module with ESP32 for Temperature and Humidity Monitoring

The DHT22 Sensor Module with ESP32 is a popular combination for temperature and humidity monitoring in IoT projects. The DHT22 offers high precision and a wide operating range, making it ideal for applications like weather stations, greenhouse automation, and smart home environments. When paired with the versatile ESP32 microcontroller, it allows for real-time data collection and remote monitoring.

However, since the DHT22 can operate on both 3.3V and 5V, special attention must be given to ensure compatibility with the ESP32’s 3.3V logic level. This article will guide you through wiring the DHT22 Sensor Module with ESP32, including using a voltage divider for safe interfacing. You’ll also learn how to read sensor data using MicroPython and troubleshoot common issues.


Table of Contents


Using the dht Module in MicroPython

The dht module is not part of the core MicroPython distribution but is included in MicroPython firmware specifically built for ESP8266 and ESP32 boards. This means you can use it without additional installations if you are working with these platforms and standard firmware builds. For most ESP32 and ESP8266 users, the dht module is pre-installed and ready to use.

If you are using a custom or minimal MicroPython build, you may need to verify whether the dht module is included. If not, you can integrate the necessary drivers by downloading them from the MicroPython GitHub repository or using an alternative Python library.

For platforms other than ESP8266 and ESP32, the dht module may not be available by default. In such cases, you will need to obtain a compatible DHT22 sensor driver and add it manually to your project. Always ensure the driver is compatible with your MicroPython version and board to avoid issues.


Materials Needed

  • DHT22 Sensor Module
  • ESP32 Development Board
  • Breadboard and jumper wires
  • Micro-USB cable
  • Computer with Thonny IDE installed
  • 10kΩ and 5.6kΩ resistors (for the voltage divider)

Download the DHT22 Data Sheet


Wiring the DHT22 Sensor Module with ESP32

Circuit diagram showing a DHT22 sensor module connected to the ESP32 with VCC at 3.3V, a 10kΩ pull-up resistor between the DATA pin and VCC, and the DATA pin connected directly to GPIO4.
Direct connection of a DHT22 Sensor Module to ESP32 powered by 3.3V.

The DHT22 sensor has three or four pins: VCC, GND, and DATA. Here are two wiring configurations based on the power supply voltage:

Direct Connection with 3.3V

If the DHT22 is powered by the ESP32’s 3.3V pin:

  1. VCC: Connect to the 3.3V pin on the ESP32.
  2. GND: Connect to one of the ESP32’s GND pins.
  3. DATA: Connect directly to GPIO4 on the ESP32.
  4. Pull-Up Resistor: Ensure a 10kΩ resistor is connected between the DATA pin and VCC.

Connection with 5V Using a Voltage Divider

Circuit diagram showing a DHT22 sensor module connected to the ESP32 with VCC at 5V, a 10kΩ pull-up resistor between the DATA pin and VCC, and a voltage divider using 10kΩ and 5.6kΩ resistors stepping down the DATA pin signal to GPIO4.
Connection of a DHT22 Sensor Module to ESP32 powered by 5V with a voltage divider.

If the DHT22 is powered by 5V:

  1. VCC: Connect to the 5V pin on the ESP32.
  2. GND: Connect to one of the ESP32’s GND pins.
  3. DATA: Connect to GPIO4 on the ESP32 through a voltage divider.
  4. Pull-Up Resistor: Ensure a 10kΩ resistor is connected between the DATA pin and VCC.

Voltage Divider Example

The DHT22’s data pin operates at the same voltage as its power supply. To step down the 5V signal to 3.3V:

  • Connect a 10kΩ resistor between the DATA pin and GND.
  • Connect a 5.6kΩ resistor between the DATA pin and GPIO4 on the ESP32.

This configuration reduces the 5V signal to approximately 3.2V, safe for the ESP32.


Reading Data with MicroPython

After wiring, upload the DHT22 driver to the ESP32 and use this script to read data:

from machine import Pin
import dht
import time

# Initialize the DHT22 sensor
dht_sensor = dht.DHT22(Pin(4))  # Replace 4 with your GPIO pin number

while True:
    try:
        dht_sensor.measure()
        temp = dht_sensor.temperature()  # Temperature in Celsius
        hum = dht_sensor.humidity()  # Humidity in percentage
        
        print(f"Temperature: {temp}°C")
        print(f"Humidity: {hum}%")
    except Exception as e:
        print(f"Error: {e}")
    time.sleep(2)  # Delay between readings

Upload this code to your ESP32 using the Thonny IDE and monitor the readings on the serial console.


Troubleshooting Tips

  • Incorrect Readings: Check wiring, ensure pull-up resistor is present, and verify power supply stability.
  • Timeout Errors: Inspect the voltage divider configuration and reduce noise from other components.
  • No Data: Ensure the DHT22 driver is correctly installed and the pin assignment matches your wiring.

Conclusion

Integrating the DHT22 Sensor Module with ESP32 is a straightforward process that provides precise environmental data for various applications. By implementing a voltage divider, you ensure compatibility and protect the ESP32 from potential damage. With the example code provided, you can quickly begin logging temperature and humidity data, paving the way for advanced IoT projects.

Whether you’re building a weather station, greenhouse monitor, or smart home system, the DHT22 Sensor Module with ESP32 offers a powerful solution. As you expand your project, consider integrating additional sensors or cloud-based data storage for enhanced functionality.

Leave a Comment