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
DHT11orDHT22instead.- measure() None
Trigger a measurement on the sensor and read the 5-byte response into the internal buffer. Raises
Exceptionwith the message"checksum error"if the data checksum is invalid.Call this method before reading
temperature()orhumidity(). 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.
- 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.