Project

Reading Temperature and Humidity with DHT22 on Raspberry Pi Pico

Overview

Learn how to read temperature and humidity from a DHT22 sensor using a Raspberry Pi Pico. This project is useful for weather stations, room monitoring, and simple climate displays.

Instructions

Step by step

1. Connect the DHT22 VCC pin to 3.3V. 2. Connect the DHT22 GND pin to GND. 3. Connect the DHT22 Data pin to GP15. 4. Run the code and open the Serial/console output. 5. Change the temperature and humidity values in the simulator. 6. Check if the printed temperature and humidity values change. 7. If the sensor is not wired correctly, the code will catch the error and print a warning instead of crashing.

Code

Source code

code
from machine import Pin
import dht
import utime

sensor = dht.DHT22(Pin(15))

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

        print("Temperature:", temperature, "C")
        print("Humidity:", humidity, "%")
    except OSError as e:
        print("DHT sensor error:", e)

    utime.sleep(2)

Related

More projects with this sensor