dht — DHT11 and DHT22 temperature/humidity sensors

The dht module provides drivers for the DHT11 and DHT22 (also known as AM2302) low-cost temperature and humidity sensors. The single-wire protocol is implemented in C by the underlying port (machine.dht_readinto, esp.dht_readinto or pyb.dht_readinto depending on platform).

Example:

from machine import Pin
from dht import DHT22

d = DHT22(Pin(4))
d.measure()
print(d.temperature(), d.humidity())

Classes

class dht.DHTBase(pin: machine.Pin)

Base class for DHT sensors. Not normally instantiated directly — use DHT11 or DHT22 instead.

measure() None

Trigger a measurement on the sensor and read the 5-byte response into the internal buffer. Raises Exception with the message "checksum error" if the data checksum is invalid.

Call this method before reading temperature() or humidity(). The DHT sensors require at least 1 second (DHT11) or 2 seconds (DHT22) between consecutive measurements.

class dht.DHT11(pin: machine.Pin)

Driver for the DHT11 sensor. Connect the sensor’s data line to pin (a machine.Pin). The DHT11 reports integer values with 1 percent relative humidity and 1 degree Celsius resolution.

humidity() int

Return the relative humidity from the most recent measure() call, as an integer percentage (0–100).

temperature() int

Return the temperature from the most recent measure() call, as an integer in degrees Celsius.

class dht.DHT22(pin: machine.Pin)

Driver for the DHT22 / AM2302 sensor. Connect the sensor’s data line to pin (a machine.Pin). The DHT22 reports values with 0.1 percent relative humidity and 0.1 degree Celsius resolution, and supports negative temperatures.

humidity() float

Return the relative humidity from the most recent measure() call, as a float percentage (0.0–100.0).

temperature() float

Return the temperature from the most recent measure() call, as a float in degrees Celsius. Negative values are supported.