ESP32 MicroPython DHT11 Temperature Humidity Sensor

This tutorial instructs you how to use the DHT11 temperature and humidity sensor with the ESP32 and MicroPython. In detail, we will learn:

ESP32 MicroPython and DHT11 sensor module

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×DHT11 Temperature Humidity Sensor Module
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Expansion Board for ESP32
1×(Recommended) Power Splitter 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 DHT11 Temperature and Humidity Sensor

DHT11
Operating Voltage3 to 5V
Temperature Range 0°C to 50°C
Temperature Accuracy ± 2°C
Humidity Range 20% to 80%
Humidity Accuracy 5%
Reading Rate 1Hz (once every second)

Pinout

There are two versions of the DHT11: a sensor and a module.

DHT11 temperature and humidity sensor Pinout

The DHT11 sensor comes with four 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
  • NC pin: not used, you can ignore it

The DHT11 module comes with three pins.

  • GND pin: connect to GND (0 volts).
  • VCC pin: connect to VCC (5 volts or 3.3 volts).
  • DATA pin: used for communication between the sensor and ESP32.

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

Wiring Diagram

ESP32 - DHT11 Sensor Wiring Diagram

To connect the DHT11 sensor to the ESP32, use a resistor ranging from 5K to 10K Ohms. This resistor keeps the data line high, ensuring good communication between the sensor and the ESP32. We need to use a breadboard in this case.

The wiring diagram between ESP32 MicroPython DHT11 Temperature and humidity Sensor

This image is created using Fritzing. Click to enlarge image

ESP32 - DHT11 Module Wiring Diagram

Most DHT11 sensor modules have a resistor already included, so you don't need to add another one. This simplifies the process of wiring or soldering.

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

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and dht11 temperature humidity sensor

ESP32 MicroPython Code - DHT11

""" 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-dht11-temperature-humidity-sensor """ import machine import time import dht DHT11_PIN = 21 # The ESP32 pin GPIO21 connected to the DHT11 sensor # Initialize the DHT11 sensor DHT11 = dht.DHT11(machine.Pin(DHT11_PIN)) # Read data from the sensor every 2 seconds while True: try: DHT11.measure() temp = DHT11.temperature() # Gets the temperature in Celsius humidity = DHT11.humidity() # Gets the relative humidity in % print("Temperature: {:.2f}°C, Humidity: {:.2f}%".format(temp, humidity)) except OSError as e: print("Failed to read from DHT11 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 DHT11 sensor/module to ESP32 board 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 DHT11 sensor to be warmer or cooler.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Temperature: 24.00°C, Humidity: 55.00% Temperature: 24.00°C, Humidity: 63.00% Temperature: 24.00°C, Humidity: 64.00% Temperature: 24.00°C, Humidity: 73.00% Temperature: 25.00°C, Humidity: 74.00% Temperature: 25.00°C, Humidity: 75.00% Temperature: 25.00°C, Humidity: 76.00% Temperature: 25.00°C, Humidity: 78.00% Temperature: 25.00°C, Humidity: 79.00% Temperature: 25.00°C, Humidity: 79.00% Temperature: 25.00°C, Humidity: 79.00% Temperature: 26.00°C, Humidity: 78.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!