Temperature and humidity, DHT22
One wire, forty bits and a checksum. Drag the two sliders for the weather it reads.
- 3 pins
- 5 properties
Every one of its 3 pins
What it does
The DHT22 and its cheaper sibling the DHT11 measure temperature and humidity and report both over one data wire, with no bus standard behind it at all. The board pulls the line low to ask, lets go, and the sensor answers with forty pulses whose widths are the bits, ending in a checksum. Mokxi decodes nothing on the sensor’s behalf: the part drives the pin with the datasheet’s timing and the sketch has to measure it, exactly as on a desk. It also keeps the two habits that confuse people most. A request in the first second after power-up is ignored, so the first reading fails; and the DHT22 only measures every two seconds, handing back its last reading in between, so a sketch that polls faster sees a number that seems stuck. The model property switches between the DHT22 and the DHT11, whose bytes mean different things on the same wire.
How to use the DHT22 with an Arduino
Wire VCC to 5 V, GND to GND and DATA to any digital pin; a three-pin module has its pull-up on board, and a bare four-pin sensor needs a 10 k resistor from DATA to VCC. Then ask for a reading no more than once every two seconds, and check every reading before you use it, because a failed one comes back as nan.
The Adafruit DHT library’s calls compile here as they stand. The floats it returns print normally and take arithmetic of your own; the built-in weather station works in whole tenths only to stay small on the Uno.
The DHT library compiles on every board here, the ESP32 and the Pico included, and the sensor is happy on 3.3 V.
#include "DHT.h"
DHT dht(2, DHT22);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float t = dht.readTemperature();
if (isnan(t)) return;
Serial.println(t);
}The 5 properties you can set
What is true about the Temperature and humidity, DHT22, here
The DHT11 and the DHT22
One wire, three pins, forty bits. The blue brick is the DHT11 and the white one is the DHT22 (also sold as the AM2302); the wire is identical and only the five bytes differ.
Not modeled
The accuracy: +/-2 %RH and +/-0.5 C on a DHT22, +/-5 and +/-2 on a DHT11. The number the slider says is the number that goes on the wire. Nor the seconds the capacitive element takes to follow a change in the air, nor self-heating.
From Signals you time, not read, in full.
See the Temperature and humidity, DHT22 in a project
Shown here on: Arduino Uno R3
Where it turns up in a lesson
In a learn article
The rest of the bench
Every one of these is drawn and simulated the same way.
Wire up the Temperature and humidity, DHT22
Open the editor and push it into the breadboard. It is free, and it runs on your own machine.