ESP32 MicroPython DHT22 Temperature Humidity Sensor

This guide will teach you how to use a DHT22 temperature and humidity sensor with an ESP32 and MicroPython. We'll cover the following:

ESP32 MicroPython and DHT22 sensor module

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×DHT22 Module
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of DHT22 Temperature and Humidity Sensor

DHT22
Temperature Range -40°C to 80°CW
Temperature Accuracy ± 0.5°C
Humidity Range 0% to 100%
Humidity Accuracy ± 2 to 5%
Reading Rate 0.5Hz (once every 2 seconds)
Operating Voltage3 to 5V

Pinout

There are two forms of DHT22: one is just the sensor, and the other is a complete module.

DHT22 temperature and humidity sensor Pinout

The DHT22 sensor has four pins.

  • GND pin: Connect this to GND (0 volts).
  • VCC pin: Connect this to VCC (5 volts or 3.3 volts).
  • DATA pin: Use this pin for the sensor to talk with the ESP32.
  • NC pin: This is not used and you can ignore it.

The DHT22 module has three pins:

  • GND pin: connect to GND (0 volts)
  • VCC pin: connect to VCC (5 volts or 3.3 volts)
  • DATA pin: used for sending information to and from the sensor and ESP32

Some manufacturers provide the DHT22 sensor as a module with three marked pins: GND, VCC, and DATA (or sometimes labeled as -, +, and OUT).

Wiring Diagram

ESP32 - DHT22 Sensor Wiring

Connect a resistor ranging from 5K to 10K Ohms between the DHT22 sensor and the ESP32 to keep the data line high and enable communication. We need to use a breadboard

The wiring diagram between ESP32 MicroPython DHT22 Temperature and humidity Sensor

This image is created using Fritzing. Click to enlarge image

ESP32 - DHT22 Module Wiring

Most DHT22 sensor modules include a built-in resistor, so you don't need to add another one. This makes the wiring or soldering process easier.

  • How to connect ESP32 and DHT22 temperature humidity module using breadboard
The wiring diagram between ESP32 MicroPython DHT22 Temperature and humidity Module

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and dht22 temperature humidity module

ESP32 MicroPython Code - DHT22

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-dht22-temperature-humidity-sensor """ import machine import time import dht DHT22_PIN = 21 # The ESP32 pin GPIO21 connected to the DHT22 sensor # Initialize the DHT22 sensor DHT22 = dht.DHT22(machine.Pin(DHT22_PIN)) # Read data from the sensor every 2 seconds while True: try: DHT22.measure() temp = DHT22.temperature() # Gets the temperature in Celsius humidity = DHT22.humidity() # Gets the relative humidity in % print("Temperature: {:.2f}°C, Humidity: {:.2f}%".format(temp, humidity)) except OSError as e: print("Failed to read from DHT22 sensor:", e) time.sleep(2)

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the ESP32 board to the DHT22 sensor/module according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny\'s editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Adjust the temperature near the DHT22 sensor by increasing or decreasing the heat.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Temperature: 21.00°C, Humidity: 42.00% Temperature: 21.00°C, Humidity: 44.00% Temperature: 21.00°C, Humidity: 44.00% Temperature: 21.00°C, Humidity: 53.00% Temperature: 22.00°C, Humidity: 54.00% Temperature: 22.00°C, Humidity: 55.00% Temperature: 22.00°C, Humidity: 56.00% Temperature: 22.00°C, Humidity: 58.00% Temperature: 22.00°C, Humidity: 59.00% Temperature: 22.00°C, Humidity: 59.00% Temperature: 22.00°C, Humidity: 59.00% Temperature: 23.00°C, Humidity: 58.00%
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

Video Tutorial

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!