Raspberry Pi Pico - DHT11 Temperature Humidity Sensor

In this guide, we will learn to set up and use the DHT11 temperature and humidity sensor with the Raspberry Pi Pico. We will cover the following details:

Raspberry Pi Pico and DHT11 sensor module

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×DHT11 Module
1×10 kΩ resistor
1×Breadboard
1×Jumper Wires
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

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 Raspberry Pi Pico
  • 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 Raspberry Pi Pico.

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

Wiring Diagram

To connect the DHT11 sensor to the Raspberry Pi Pico, use a resistor ranging from 5K to 10K Ohms. This resistor keeps the data line high, ensuring good communication between the sensor and the Raspberry Pi Pico.

Raspberry Pi Pico - DHT11 Sensor Wiring

The wiring diagram between Raspberry Pi and Pico DHT11 Temperature and humidity Sensor

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico - DHT11 Module Wiring

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.

The wiring diagram between Raspberry Pi and Pico DHT11 Temperature and humidity Module

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code - DHT11

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-dht11-temperature-humidity-sensor """ import machine import time import dht DHT11_PIN = 0 # The Raspberry Pi Pico pin (GP0) 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

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Wire the components according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • 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: 22.00°C, Humidity: 55.00% Temperature: 22.00°C, Humidity: 54.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: 66.00% Temperature: 23.00°C, Humidity: 68.00% Temperature: 23.00°C, Humidity: 69.00% Temperature: 23.00°C, Humidity: 69.00% Temperature: 23.00°C, Humidity: 69.00% Temperature: 23.00°C, Humidity: 68.00%
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

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!