Arduino MicroPython DHT11 Temperature Humidity Sensor

This guide shows you how to use the DHT11 temperature and humidity sensor with Arduino and MicroPython. You will learn:

Arduino MicroPython and DHT11 sensor module

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×DHT11 Module
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

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 types of DHT11: a sensor and a module.

DHT11 temperature and humidity sensor Pinout

The DHT11 sensor has four 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 Arduino
  • NC pin: Not used, you can ignore it

The DHT11 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 communication between the sensor and Arduino

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

Wiring Diagram

Arduino - DHT11 Sensor Wiring Diagram

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

The wiring diagram between Arduino MicroPython DHT11 Temperature and humidity Sensor

This image is created using Fritzing. Click to enlarge image

Arduino - 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 Arduino and DHT11 temperature humidity module
The wiring diagram between Arduino MicroPython DHT11 Temperature and humidity Module

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code - DHT11

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-dht11-temperature-humidity-sensor """ import machine import time import dht DHT11_PIN = 'D2' # The Arduino Giga WiFi pin D2 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 run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the DHT11 sensor/module to Arduino board according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny\'s editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • Change the temperature near the DHT11 sensor to make it 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: 22.00°C, Humidity: 44.00% Temperature: 22.00°C, Humidity: 51.00% Temperature: 22.00°C, Humidity: 54.00% Temperature: 22.00°C, Humidity: 63.00% Temperature: 23.00°C, Humidity: 64.00% Temperature: 23.00°C, Humidity: 65.00% Temperature: 23.00°C, Humidity: 71.00% Temperature: 23.00°C, Humidity: 72.00% Temperature: 23.00°C, Humidity: 74.00% Temperature: 24.00°C, Humidity: 74.00% Temperature: 24.00°C, Humidity: 75.00% Temperature: 26.00°C, Humidity: 77.00%
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

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!