Skip to content

Getting Started with DHT11/22 on Raspberry Pi Pico using MicroPython

16x2 I2C lcd

Table of Contents

Abstract

In this article, a comprehensive step-by-step guide to interface DHT 11/22 temperature and humidity sensor with Raspberry Pi Pico board using MicroPython. Raspberry Pi Pico has 4 different variants (Pico, Pico 2, Pico W, Pico 2W) supporting micro-python. This articles lays the foundation for more advanced embedded system and IoT projects.

🧭 Pre-Request

  • OS : Windows / Linux / Mac / Chrome
  • Thonny IDE.
  • MicroPython firmware in Raspberry Pi Pico / Pico 2 / Pico W / Pico 2W.

Hardware Required

  • Raspberry Pi Pico / Pico 2 / Pico W / Pico 2W.
  • DHT 11/22 sensor.
  • BreadBoard.
  • Micro USB Cable.
  • Connecting wires.
  • 5V DC power supply (Optional)
Components Purchase Link
Raspberry Pi Pico link
Raspberry Pi Pico 2 link
Raspberry Pi Pico W link
Raspberry Pi Pico 2W link
DHT 11/22 sensor link
BreadBoard large : small
Connecting Wires link
Micro USB Cable link
5V DC Adaptor link

Don't own a hardware 😢

No worries,

Still you can learn using simulation. check out simulation part 😃.

Connection Table

DHT 11/22 GPIO Remarks
VCC (1) +5 V +5V of Board or External 5V DC Supply
Data (2) 9 Any GPIO pin can be used
NC (3) - No Connection
GND (4) GND Ground

Note

  • VCC (1) can be connected to external +5 V DC supply or to the VBUS pin number 40 of Raspberry Pi Pico Board.
  • While using External 5 V supply for DHT 11/22 sensor, make sure ground pin of External Supply and Pico board is connected.

Circuit Diagram

fig-Connection Diagram

📂 Code

from machine import Pin
import dht
import time

time.sleep_ms(1000) # Wait for USB to become ready

sensor = dht.DHT22(Pin(9))

while True:
    sensor.measure()
    temperature = sensor.temperature()
    humidity = sensor.humidity()

    print(f"Temp: {temperature:0.2f} C")
    print(f"Hum: {humidity:0.2f}")
    time.sleep_ms(2000)

Code Explanation

👉 Imports

1
2
3
from machine import Pin
import dht
import time
  • time module for creating delay.
  • dht module for interacting with dht 11 or 22 sensor.

👉 Initialize DHT 22 sensor.

sensor = dht.DHT22(Pin(9))
  • GPIO 9 is connected to DHT 22 sensor Data pin 2.

👉 Continuous looping & measurement.

while True:
    sensor.measure()
    temperature = sensor.temperature()
    humidity = sensor.humidity()

    print(f"Temp: {temperature:0.2f} C")
    print(f"Hum: {humidity:0.2f}")
    time.sleep_ms(2000)
  • while True used for continuous looping.
  • print statement to display the measured temperature and humidity data on the terminal.
  • sleep_ms(2000) 2 second delay.

👉 Read temperature and humidity data.

sensor.measure()
temperature = sensor.temperature()
humidity = sensor.humidity()
  • sensor.measure() used to initialize the sensor module to measure the parameters.
  • sensor.temperature() reads temperature data from sensor.
  • sensor.humidity() reads humidity data from sensor.

Simulation

Not able to view the simulation

  • Desktop or Laptop : Reload this page ( Ctrl+R )
  • Mobile : Use Landscape Mode and reload the page

Extras

Components details

Modules / Libraries Used

  • time
    • time module provides functions related to date & time, measuring time intervals and generating delays.
    • More Details
  • dht
    • To interact with DHT 11/22 sensor.
    • It is a built in library.
    • More Details