Temperature and humidity, DHT22
One wire, forty bits and a checksum. Drag the two sliders for the weather it reads.
- 3 pins
- 5 settings
- In 4 projects
- In 8 lessons
The Temperature and humidity, DHT22 in brief
What it is, and every setting the properties panel gives it.
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.
- Family
- Sensors
- Pins
- 3
- model
- dht22 (or dht11)
- temperature
- 22
- humidity
- 50
- pullup
- true
- pullup_ohms
- 5100
A circuit built around it, running and explained: the DHT11 simulator.
3 pins
Named as the editor names them, so a wire lands where you expect it.
- VCC
- DATA
- GND
How to use the DHT11 or 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.
A DHT11 wires the same way. Change DHT22 to DHT11 in the constructor and set the part’s model property to DHT11: the two send their bytes differently, so code for one reads the other wrong. The DHT11 reads whole degrees from 0 to 50 and whole percent from 20 to 90.
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);
}What is true about the Temperature and humidity, DHT22, here
In its help article’s own words: what the simulation gets right, and where it stops.
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
4 built-in projects use it. Each opens in the editor, already wired.
Where it turns up in a lesson
8 lessons place it in their own circuits.
Learn articles
8 lessons
- Arduino DHT22 and DHT11: Temperature and Humidity
- Arduino sprintf and Float to String, With Examples
- Arduino No Matching Function for Call To: Fixes
- Arduino "stray '\302' in program": How to Fix
- Home Assistant and ESPHome Devices on an ESP32
- Arduino "No Such File or Directory": How to Fix
- Tinkercad ESP32 Simulator: A Free Alternative
- Wokwi vs Tinkercad: Which Arduino Simulator to Use
The boards it is shown on
Each of these 3 boards has a built-in project with the Temperature and humidity, DHT22 in it.
The Temperature and humidity, DHT22 on each board
The pins, the voltage and the code change from board to board. Each page has that board’s wiring and a circuit you can run.
Arduino libraries for the Temperature and humidity, DHT22
Built in, so the sketch compiles with nothing to install. Each page has the calls, the wiring and an example to run.
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.