DHT22 temp & humidity
The DHT22 (Aosong AM2302) is a temperature and humidity sensor on a single data wire, for $5. It is a four-pin part: 3.3–6 V power, ground, one not-connected pin, and the data line, which needs a 10 kΩ pull-up to the supply. It is neither I²C nor true 1-Wire — it speaks its own single-wire protocol — but from Python you only see the adafruit_dht driver, which reports whole frames with 0.1 °C and 0.1 %RH resolution.
Pins
| Pin | What it is | Type |
|---|---|---|
| VCC | Supply 3.3–6 V (pin 1) | power |
| DATA | Data, open drain (pin 2) | io |
| NC | Not connected (pin 3) | nc |
| GND | Ground (pin 4) | ground |
In circuit.py:
DHT22()— DHT22 temp & humidity — $5- Pins: 4
Pin 3 is marked NC — leave it alone. The DHT22 module kit is the same sensor with the pull-up already fitted.
Wiring it in circuit.py
from wiring import *
pi = Pi()
sensor = DHT22()
pi.V3_3 >> sensor.vcc
pi.V3_3 >> Resistor(10000) >> sensor.data # the data pull-up
pi.GPIO4 >> sensor.data
sensor.gnd >> pi.GNDAny GPIO works for DATA — the driver finds the sensor by the pin it is asked about, not by a fixed number. The important part is the pull-up: without it the open-drain data line floats and every read returns the DHT sensor not found, check wiring error.
Driving it in 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: # checksum or timeout: just read again
print("read failed:", error)
sleep(2)Failed reads are normal — they are how the protocol reports a bad frame — so real code always catches RuntimeError and retries after the sensor's two-second minimum interval. Reading temperature and humidity separately triggers two requests, and the second one inside 2 s returns the cached frame. The legacy Adafruit_DHT module and the full driver surface are covered on the adafruit_dht page.
Electrical notes
- Supply is 3.3–6 V; the sensor draws about 1.5 mA while measuring and 50 µA standing by, so the Pi's 3V3 rail is comfortable.
DATAis open drain and needs the 10 kΩ pull-up to the same supply. Three-pin modules include it.- Minimum sample interval is 2 s. Faster reads return the previous frame rather than asking the sensor again.
- Accuracy is ±0.5 °C and ±2 %RH, with a 0.1 °C / 0.1 %RH frame resolution. Temperature range is −40 to 80 °C.
- On a real Pi roughly one read in ten fails because Linux scheduling jitter corrupts the microsecond timing. Code that does not handle
RuntimeErrorwill crash in the field.
Simulator vs real hardware
- Temperature and humidity come from the datafeed for the sensor's zone, with the datasheet's accuracy and the 0.1 frame rounding applied. Greenhouse or cold-room settings change what it reports.
- The failure rate is a knob (
readFailurePct, 5 % by default). The two messages are the library's own:Checksum did not validate. Try again.andA full buffer was not returned. Try again.Unpowered or unwired, the read raisesDHT sensor not found, check wiring. - There is no bit timing to get wrong: the simulation answers a complete "read" as one request, so a slow program cannot corrupt the frame. Only the modelled failure rate produces errors.
- Reads closer together than 2 s return the cached value, exactly as the real driver does.
Going deeper
- The Python side: adafruit_dht library notes, including the legacy
Adafruit_DHTmodule. - Primer: pull-up and pull-down resistors — why the 10 kΩ is mandatory.
- The pull-up is a resistor; the pre-wired version is the DHT22 module.
- Recipe: the LCD weather station pairs one with a BME280.
Further reading
- AM2302 datasheet (Aosong) — the 40-bit frame, timings and accuracy the model follows.
- adafruit_dht (CircuitPython) documentation — the driver's public API.
- Adafruit: DHT humidity sensing — wiring a bare sensor and the retry pattern.