Temperature & Humidity
Interfacing DHT22 Temperature & Humidity with Raspberry Pi
What is the DHT22?
What is the DHT22 (AM2302)?
The DHT22 is what most people buy after the DHT11 has taught them what a digital temperature and humidity sensor can do, and what it cannot. It uses the same single-wire interface, the same four-pin layout and the same library, but it measures with finer resolution, over a wider range and with better accuracy.
In practical terms, that means you get readings such as 23.6 °C and 54.3 % RH instead of whole numbers, you can measure below freezing and all the way to 100 % humidity, and the numbers are more trustworthy. A greenhouse, a cellar, a grow tent, a cold room above freezing or an outdoor weather station all become realistic projects.
Inside the case, a capacitive polymer humidity element and a temperature sensor feed a small chip that applies the factory calibration and hands your board a finished digital reading. There is nothing to calibrate and nothing analog to read.
Tip
DHT22 and AM2302 are the same sensor family. Aosong lists AM2302 as another name for the DHT22, and the "AM2302" you buy from Adafruit and other shops is the wired version in a larger case, with three leads and the pull-up resistor already built in. The same code works for both: use DHTTYPE DHT22.
Specifications
- Sensing elements
- Capacitive polymer humidity element and a temperature sensor, read by a built-in chip
- Operating voltage
- 3.3 V – 6 V DC (DHT22 datasheet). Newer AM2302 revisions quote 3.3–5.5 V, so stay at or below 5.5 V
- Operating current
- 1–1.5 mA measuring, 40–50 µA standby
- Temperature range
- −40 °C to 80 °C (some sellers quote up to 125 °C, but the Aosong datasheet specifies 80 °C)
- Temperature accuracy
- ±0.5 °C
- Humidity range
- 0 % to 100 % RH
- Humidity accuracy
- ±2 % RH typical, ±5 % RH maximum
- Resolution
- 16-bit: 0.1 °C and 0.1 % RH steps
- Repeatability
- ±1 % RH and ±0.2 °C (DHT22 datasheet; newer AM2302 revisions quote tighter figures)
- Humidity hysteresis
- ±0.3 % RH
- Long-term stability
- About 0.5 % RH per year
- Sampling rate
- 0.5 Hz maximum: one reading every 2 seconds
- Interface
- Single-wire digital (proprietary: not I²C, not Dallas 1-Wire). 40-bit frame with a checksum
- Pull-up resistor
- 4.7–10 kΩ between DATA and VCC. The wired AM2302 and most 3-pin modules already include one (about 5.1 kΩ)
- Power-up wait
- Do not send commands for 1 second after power-up. A 100 nF capacitor between VCC and GND is recommended
- Cable length
- Aosong specifies signal transmission of more than 20 m at 5 V; keep it short on 3.3 V
- Size
- Bare DHT22 about 15 × 25 × 7.7 mm; the wired AM2302 comes in a larger case with 3 wires
Pinout
| Pin | Name | Description |
|---|---|---|
| 1 | VCC | Power, 3.3 V – 5.5 V DC. Use 3.3 V on 3.3 V boards such as the ESP32 and Raspberry Pi |
| 2 | DATA | Single-wire digital input/output. Needs a 4.7–10 kΩ pull-up to VCC |
| 3 | NC | Not connected. Leave it floating |
| 4 | GND | Ground |
Swipe sideways to see every column →
You will meet the DHT22 in three forms. The bare 4-pin sensor runs VCC, DATA, NC, GND from left to right with the grille facing you, and needs an external 4.7–10 kΩ pull-up between DATA and VCC. The 3-pin module drops the NC pin and adds the pull-up on its board, but pin order varies by maker, so read the silkscreen. The wired AM2302 has three leads: red is VCC, yellow is DATA and black is GND, with a pull-up resistor already inside. On a 3.3 V-only board such as the ESP32, Raspberry Pi or Blue Pill, power the sensor from 3.3 V so the data line never exceeds what the GPIO can tolerate. Longer cables behave better with a pull-up toward the 4.7 kΩ end of the range.
Variants
| Variant | Temp range | Hum range | Accuracy | Protocol | Price |
|---|---|---|---|---|---|
| DHT22 (AM2302) | -40–80°C | 0–100% RH | ±0.5°C / ±2% RH | Single-wire, 0.5 Hz | ~$3–5 |
| DHT11 | 0–50°C | 20–90% RH | ±2°C / ±5% RH | Single-wire, 1 Hz | ~$1–2 |
| SHT31 | -40–125°C | 0–100% RH | ±0.2°C / ±2% RH | I2C | ~$3–8 |
Swipe sideways to see every column →
The DHT22 is the accuracy and range upgrade over the DHT11, at roughly double the price and half the sample rate. Wiring and library calls are identical, so swapping is a one-word change to DHTTYPE. If you want tighter accuracy, faster response or an I²C interface, the SHT31 is the natural next step.
What you gain over the DHT11
The upgrade shows up in four places.
Accuracy. Temperature is accurate to ±0.5 °C instead of ±2 °C, and humidity to ±2 % RH instead of ±5 % RH. The worst-case humidity figure for the DHT22 is still ±5 % RH, so treat the ±2 % as a typical value under good conditions.
Range. The DHT22 covers −40 °C to 80 °C and 0–100 % RH, while the DHT11 only covers 0–50 °C and 20–90 % RH. If your project goes below freezing, above 50 °C or into very dry or very humid air, that range matters.
Resolution. Values come in 0.1 steps, so you can see small trends. With the DHT11, a temperature that drifts by half a degree is invisible.
Stability. The DHT22 is specified for about 0.5 % RH of drift per year and a humidity hysteresis of ±0.3 % RH, so readings stay consistent over time.
What you give up is speed and a little money. It delivers one reading every two seconds rather than one a second, and costs roughly twice as much.
How the DHT22 works inside
The humidity element is a capacitor. A thin layer of moisture-absorbing polymer sits between two electrodes, and as the air gets more humid, the polymer absorbs water vapour and its dielectric properties change. That changes the capacitance between the electrodes, and the amount of change tells the chip how humid the air is. It is a cleaner and more stable principle than the resistive approach used in the DHT11, which is a large part of why the DHT22 is more accurate.
The temperature sensor is a thermistor, a resistor whose resistance changes strongly with temperature. Its reading is also used to compensate the humidity value, since relative humidity depends on temperature.
The chip measures both signals, applies the calibration data stored in the sensor at the factory, converts the results to 16-bit numbers and sends them over the data wire whenever your board asks. In between requests it sleeps, drawing only tens of microamps.
Choosing your version and wiring the pull-up
Before you wire anything, check which form of DHT22 you have, because that decides whether you need a resistor.
The bare 4-pin sensor has VCC, DATA, NC and GND pins. It needs an external pull-up resistor of 4.7 kΩ to 10 kΩ between DATA and VCC. Without it, the data line floats and reads fail or return NaN.
The 3-pin module has the pull-up (and usually a decoupling capacitor) on its small PCB, so you can wire VCC, DATA and GND straight to your board. Check the silkscreen for pin order.
The wired AM2302 has three leads, usually red (VCC), yellow (DATA) and black (GND), and contains the pull-up resistor inside the case. It is the easiest to use outdoors or on an enclosure wall because you can extend the leads and mount the body wherever you like.
The datasheet also recommends a 100 nF capacitor between VCC and GND, close to the sensor, to filter supply noise. Modules usually include it. If you use a bare sensor on a long wire or a noisy supply, add one.
Power, voltage and cable length
The DHT22 is specified for 3.3 V to 6 V by its original datasheet, and newer AM2302 revisions say 3.3–5.5 V, so staying at or below 5.5 V is the safe choice.
On a 5 V board such as the Arduino Uno, power the sensor from 5 V. This also gives you the longest cable: the manufacturer states signal transmission of more than 20 metres. For long runs, use a pull-up toward 4.7 kΩ, shielded cable and keep the wire away from mains cables and motors.
On a 3.3 V board such as the ESP32, Raspberry Pi or Blue Pill, power the sensor from 3.3 V. Because the data line swings to the sensor's supply voltage, powering from 5 V would put 5 V on a GPIO that cannot handle it. Keep the cable short at 3.3 V, because voltage drop on a long lead can cause failed reads.
Whichever you choose, wait one second after power-up before talking to the sensor. The datasheet asks for this so it can get past its unstable start-up state.
- 1Open the Arduino IDE and go to Tools → Manage Libraries.
- 2Search for "DHT sensor library" and install the one published by Adafruit.
- 3Accept the prompt to also install "Adafruit Unified Sensor", which the library depends on.
- 4Wire VCC, DATA and GND, with a 4.7–10 kΩ pull-up between DATA and VCC if you are using the bare 4-pin sensor.
- 5Open File → Examples → DHT sensor library → DHTtester, set DHTPIN to your pin and DHTTYPE to DHT22, then upload.
- 6Open the Serial Monitor and wait about two seconds for the first reading. If it prints NaN, check the wiring and the pull-up first.
Reading the sensor and doing something useful with it
The basic sketch for the DHT22 matches the DHT11 one: create the sensor with DHT dht(DHTPIN, DHTTYPE), call dht.begin(), then use dht.readTemperature() and dht.readHumidity(), which return NaN if a read fails or the checksum does not match. The only change is #define DHTTYPE DHT22.
Two details are worth knowing. First, the DHT22 refreshes only once every two seconds, so reading more often just returns the previous values, and the library enforces this. Second, temperatures below zero work correctly, because the library decodes the sensor's sign bit for you. If you ever decode the data frame by hand, remember that the DHT22 stores negative temperatures as a sign bit plus a magnitude, not as a two's complement number.
Raw temperature and humidity are useful, but the combination tells you more. The sketch below calculates the dew point, the temperature at which the air would become saturated and moisture would condense. It is a practical number for deciding whether a cold surface, window or pipe is going to sweat, for judging mould risk, and for controlling a dehumidifier. It uses the Magnus formula with the Sonntag constants, which is accurate to a fraction of a degree across normal conditions.
How the DHT22 sends its data
You will never write this yourself, but it explains the sensor's quirks, such as its timing sensitivity, its dislike of long unshielded cables and why you get NaN rather than wrong numbers when something goes wrong.
The DHT22 uses one data wire for everything, shared in both directions. A reading begins when your board pulls the line low for at least a millisecond (the DHT11 needs 18 ms) and then releases it. The sensor responds with an 80 microsecond low pulse followed by an 80 microsecond high pulse, and then sends 40 bits. Each bit begins with a low pulse of about 50 microseconds, and the length of the following high pulse sets its value: about 26–28 microseconds is a 0 and about 70 microseconds is a 1.
The 40 bits are five bytes:
| Byte | Content on the DHT22 |
|---|---|
| 1 | Humidity, high byte |
| 2 | Humidity, low byte (value ÷ 10 gives % RH) |
| 3 | Temperature, high byte (its top bit is the sign) |
| 4 | Temperature, low byte (value ÷ 10 gives °C) |
| 5 | Checksum: the low 8 bits of the sum of bytes 1–4 |
So a humidity reading of 0x0268 is 616 in decimal, or 61.6 % RH. If the checksum does not match, the library discards the frame and returns NaN. Because the pulses are measured in microseconds, the library briefly disables interrupts while it reads, and that is why heavy interrupt activity on some boards can cause an occasional failed read.
Unlike I²C, the protocol has no addresses, so each DHT22 needs its own data pin.
Getting accurate readings
The DHT22's specification describes the sensor in a controlled setting. Your installation decides whether you reach it.
Keep it clear of heat sources. Regulators, relays, power supplies and even the microcontroller warm the air around the sensor, which raises the temperature reading and lowers the humidity reading. Mount it away from the board, on a short lead, in moving air.
Shield it outdoors. Direct sun, rain and wind make an outdoor sensor read wrongly. A ventilated radiation shield, such as a stack of white louvred plates, keeps rain and sunlight off while letting air through. The DHT22 is not waterproof, so never expose it to direct water.
Avoid condensation. If the element gets wet, readings will be high until it dries, and repeated wetting reduces its lifespan. If the sensor has been in saturated air for a long time, leave it in warm, dry air to recover.
Mind the response time. Like all humidity sensors, the DHT22 follows changes with a delay, especially when moving between very different environments. Do not judge it from the first few readings after moving it.
Check it against a reference. If you need better than a few percent of humidity accuracy, compare it with a trusted hygrometer and apply a fixed offset in code. Over years, expect a drift of around half a percent per year.
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
| NaN or "Failed to read" every time | Missing pull-up, wrong pin, wrong DHTTYPE, or a loose wire | Add a 4.7–10 kΩ resistor between DATA and VCC, check the pin and set DHTTYPE to DHT22 |
| Occasional NaN, otherwise fine | Timing or noise | Normal. Skip it and retry on the next cycle |
| Reads fine on the bench but fails with a long cable | Voltage drop and noise | Use 5 V where the board allows it, a pull-up toward 4.7 kΩ, shielded cable and a 100 nF capacitor |
| Humidity reads high and slowly falls | Sensor got wet or sat in saturated air | Leave it in warm, dry air to recover, and keep it out of condensation |
| Temperature is higher than a reference thermometer | Heat from nearby electronics, or sunlight | Move it away and shield it |
| Readings do not change | Reading faster than every 2 seconds returns cached values | Wait at least 2 seconds between reads |
| Reads fail while ESP32 Wi-Fi is busy | Timing disturbed by radio activity | Retry on the next loop, update the library, and power from 3.3 V |
| Raspberry Pi gives RuntimeError often | Linux is not real-time | Catch the error and retry, as in the example |
| 5 V board gives weird readings with a 3.3 V sensor | Level mismatch | Match the sensor supply to the board's logic level |
DHT22 or something else?
The DHT22 is a sound all-round choice, but a few other sensors are worth considering depending on what you need.
| Sensor | Measures | Interface | Accuracy | When to choose it |
|---|---|---|---|---|
| DHT22 | Temperature and humidity | Single-wire | ±0.5 °C, ±2 % RH | Cheap, simple, indoor and outdoor monitoring |
| DHT11 | Temperature and humidity | Single-wire | ±2 °C, ±5 % RH | Lowest cost, indoor use only |
| SHT31 | Temperature and humidity | I²C | ±0.2 °C, ±2 % RH | Better accuracy and faster response |
| AHT10 | Temperature and humidity | I²C | ±0.3 °C, ±2 % RH | Low-cost I²C alternative |
| BME280 | Temperature, humidity and pressure | I²C or SPI | ±1 °C, ±3 % RH | Weather stations that also need pressure |
| DS18B20 | Temperature only | 1-Wire | ±0.5 °C | Waterproof probes and many sensors on one wire |
If you only need temperature, the DS18B20 is more robust. If you need pressure as well, the BME280 gives you all three. If your project may grow, the I²C sensors let you share one bus with other devices.
Project ideas
ESP32 weather station. Read the DHT22 every few minutes, publish to MQTT and view it in Home Assistant. Between readings, put the ESP32 into deep sleep, so a small battery and solar panel can run it for a long time. Add a BME280 for pressure if you want a complete station.
Greenhouse or grow-tent controller. Switch a fan, heater or humidifier through relays based on temperature and humidity. Use hysteresis, for example on at 65 % and off at 60 %, to stop relays chattering. The dew-point sketch above helps you avoid condensation on the plants.
Cellar, loft or crawl-space monitor. These spaces often develop damp problems unnoticed. Log temperature and humidity and send an alert when humidity stays above about 60 %, which is where mould tends to become a problem.
Server cabinet or workshop monitor. Track temperature and humidity for electronics that dislike heat or damp, and combine it with an MQ-135 to watch general air quality.
Smart thermostat input. Use the DHT22 for room temperature and humidity in a heating or air-conditioning controller, and use the heat-index value to decide how aggressively to cool.
Frequently asked questions
What is the difference between the DHT22 and the AM2302? They are the same sensor family. DHT22 usually refers to the bare 4-pin part, and AM2302 to the wired version in a larger case, which has three leads and a built-in pull-up resistor. The code is identical.
Can the DHT22 measure below freezing? Yes, down to −40 °C, and the library handles negative temperatures correctly. Be careful with condensation and ice on the sensor, since it is not designed to be wet.
How often can I read it? Once every two seconds at most. The library returns the previous values if you ask sooner.
Do I need a pull-up resistor? On the bare 4-pin sensor, yes: 4.7–10 kΩ between DATA and VCC. On the AM2302 and most 3-pin modules, no, since it is built in.
Can I use it on a 3.3 V board? Yes. Power it from 3.3 V so the data line stays within the GPIO's limits.
Can I use it outdoors? Yes, with a ventilated radiation shield that keeps rain and direct sun away. The sensor itself is not waterproof.
Why is the DHT22 humidity different from my other hygrometer? Consumer humidity sensors are rarely better than ±3–5 % RH, and each has its own offset. Compare against a reference you trust, or use a salt test to check your sensors against each other.
How long does it last? Typically for years in clean, non-condensing conditions. Expect about 0.5 % RH of drift per year, and replace it if readings become unrealistic.
Setting Up DHT22 with Raspberry Pi
Library
Adafruit CircuitPython DHT by Adafruit
pip3 install adafruit-circuitpython-dht sudo apt install libgpiod2
Wiring
| Component pin | Raspberry Pi pin | Note |
|---|---|---|
| VCC | Pin 1 (3.3V) | Power from 3.3 V, not 5 V. The DATA line swings to VCC, and Pi GPIOs are not 5 V-tolerant, so 5 V risks damaging the board. |
| DATA | GPIO4 (Pin 7) | Add a 4.7–10 kΩ pull-up to 3.3 V if you are using the bare 4-pin sensor. |
| GND | Pin 6 (GND) |
Swipe sideways to see every column →
Power the sensor from the Pi's 3.3 V pin, never from 5 V. The data line swings to the supply voltage, and the Pi's GPIO pins cannot tolerate 5 V.
The Pi is not a real-time system, so the bit-banged protocol occasionally times out. An occasional RuntimeError is expected and not a sign of a bad sensor. Catch it and retry, as the example does. Only worry if reads fail continuously.
If you get a permissions error, run the script with sudo or add your user to the gpio group. adafruit-circuitpython-dht replaces the older, archived Adafruit_Python_DHT library, so use it for anything you build today.
Related Components
AHT10
A tiny I2C sensor that packs temperature and humidity sensing into the smallest footprint possible.
BME280
Combined pressure, humidity, and temperature sensing — the go-to all-in-one environmental sensor.
DHT11
The classic beginner temperature and humidity sensor — cheap, factory-calibrated and supported on every board.
DS18B20
1-Wire waterproof probe for liquids, soil, and outdoor use.
SHT31
Lab-grade Sensirion sensor for precision climate logging.
Related Guides & Projects

Raspberry Pi GPIO Pinout: The Ultimate Reference
A comprehensive map of the 40-pin GPIO header on Raspberry Pi 4, 5, and Zero.

Building a Python Flask Server on Raspberry Pi: A Complete Beginner-Friendly Walkthrough
Step-by-step: run a Flask web server on Raspberry Pi to control GPIO pins from any browser — with autostart on boot.

Raspberry Pi GPIO Complete Guide: Pinout, PWM, I2C, and Real Examples
The Raspberry Pi GPIO header has 40 pins — but they're not all the same. This guide covers the pinout, programming in Python, PWM, I2C, SPI, and common mistakes beginners make.