PyRaspBeta

adafruit_dht

DHT11 and DHT22 sensors measure temperature and humidity over a single data wire. PyRasp ships an implementation of Adafruit's maintained CircuitPython adafruit_dht driver (4.0.4) and the legacy Adafruit_DHT module as well. The official library guide is at docs.circuitpython.org.

Wire the sensor's data pin to a GPIO, power it from 3.3 V, and add the 10 kΩ pull-up the data line needs (see the DHT22 part page):

circuit.py
from wiring import *

pi = Pi()
sensor = DHT22()
pi.V3_3 >> sensor.vcc
pi.V3_3 >> Resistor(10000) >> sensor.data
pi.GPIO4 >> sensor.data
sensor.gnd >> pi.GND

Read it like the real driver: properties raise RuntimeError on the occasional failed read, so retry after the sensor's minimum interval:

main.py
from adafruit_dht import DHT22
from time import sleep

sensor = DHT22(4)

while True:
    try:
        print(f"{sensor.temperature:.1f} C  {sensor.humidity:.1f} %")
    except RuntimeError as error:
        print("read failed:", error)
    sleep(2)

DHT11, DHT21 and DHT22 are all provided. The constructor takes a board pin, a microcontroller pin or a plain BCM number; use_pulseio is accepted and ignored.

Failures and the 2-second rule

The driver reads the sensor every time you touch .temperature or .humidity. Real DHT sensors cannot be sampled faster than once every two seconds, and the simulator reproduces that: a read within the minimum interval just returns the previous values instead of asking the sensor again.

Failed reads are normal — they are how the protocol reports a checksum mismatch — and raise RuntimeError with the library's own messages:

from adafruit_dht import DHT22
from time import sleep

sensor = DHT22(4)
for _ in range(5):
    try:
        print(sensor.temperature, sensor.humidity)
    except RuntimeError as error:
        print("skipping:", error)      # e.g. "Checksum did not validate. Try again."
    sleep(2)

The simulator can raise two failure messages, matching the library: Checksum did not validate. Try again. and A full buffer was not returned. Try again. When the sensor is not wired or not powered you get DHT sensor not found, check wiring.

The legacy Adafruit_DHT module

Code written against Adafruit's old library runs too. The pin is a BCM number, read() returns (None, None) on failure instead of raising, and read_retry() tries again:

import Adafruit_DHT

sensor = Adafruit_DHT.DHT22            # also DHT11 and AM2302
humidity, temperature = Adafruit_DHT.read_retry(sensor, 4, retries=5, delay_seconds=2)
if humidity is not None:
    print(f"{temperature:.1f} C  {humidity:.1f} %")

Declared constants: DHT11, DHT22 and AM2302 (the same sensor as DHT22). read_retry() waits delay_seconds of simulated time between attempts.

Going deeper

DHT22(4) and DHT22(board.D4) are equivalent. The simulator never touches the pin's GPIO state directly; it asks the simulation for a reading on that pin, and the part answers with values from its zone, rounded to the sensor's tenths (as a real frame would be).

The Adafruit_DHT module is provided for compatibility and runs on the same code, so both libraries give the same values in the same run.

Simulator vs real hardware

  • Values are fed, not measured. The DHT22 part takes temperature and humidity from the world's feed for its room (with the sensor's 0.1 resolution and 0.5 °C/2 % accuracy reflected in the noise). A cold room or a hot lamp in the Lab changes what it reports; on a real Pi the sensor would measure actual air.
  • No timing-critical bit stream. Real DHT drivers time microsecond pulses on the wire; the simulation answers the whole "read" as one request. That means reads never fail because your program was too slow or because interrupts were disabled — only the modelled 5 % failure rate (adjustable per part) produces checksum errors.
  • The minimum interval is enforced by the part. Reads inside 2 s return the cached frame, as the real library does; the sensor itself would also refuse to answer that quickly.
  • No pulseio, no bitbangio. On a Pi, CircuitPython's Blinka uses libgpiod/pulseio underneath; here the same public API is served directly by the simulation.
  • One data pin. Wire it to any GPIO; the simulator matches the request to whichever DHT part has its DATA pin on that net.