DHT11/DHT22 (AM2302) — Initialization (dht_init)¶
What it does: Initializes the DHT11/DHT22 digital temperature/humidity sensor (AM2302) on the selected pin, creates a dhts object, and takes the first test measurement.

Inputs¶
pin: Sensor data line GPIO pin.- Field
DHT_TYPE: sensor type (DHT11,DHT22,AM2302).
Output¶
- None (statement block).
When to use it¶
- Once at program start, before blocks
dht_measure,dht_read_temp,dht_read_humidity.
Tips and pitfalls¶
- DHT11 typically has a smaller range and accuracy (approx. 0 to 50 °C, 20 to 90 %RH).
- DHT22/AM2302 has a wider range and better accuracy (approx. -40 to 80 °C, 0 to 100 %RH).
- DHT sensors are slow. For DHT22/AM2302, expect a measurement period of around 2 s.
Usage example¶

Generated Python example¶
from machine import Pin
import dht
import time
dhts = dht.DHT22(Pin(4))
time.sleep(1)
try:
dhts.measure()
time.sleep(1)
except OSError as e:
print("Error reading DHT")