The DHT sensor family is the undisputed workhorse of DIY environmental monitoring, but its proprietary single-bus protocol and overlapping part numbers trip up even experienced makers. If you are wiring a sensor DHT module to an ESP32, Arduino, or Raspberry Pi, you need to understand that this is not an analog device, nor is it standard I2C. It is a strictly timed digital pulse interface.
This guide cuts through the generic library documentation to explain the exact microsecond timing, the raw-to-unit bitwise math, and the physical limitations of the sensor. By the end, you will know exactly which variant to buy and how to debug the inevitable timeout errors.
The Sensing Principle and Digital Output Signal
Inside the plastic housing of a DHT sensor, you will find two distinct sensing elements. Temperature is measured using an NTC (Negative Temperature Coefficient) thermistor, which drops in electrical resistance as ambient heat increases. Humidity is measured via a capacitive polymer sensing element; as water vapor is absorbed into the polymer, the dielectric constant changes, altering the capacitance between two electrodes.
An internal 8-bit microcontroller samples both elements, applies factory calibration coefficients stored in OTP (One-Time Programmable) memory, and outputs the result as a digital single-bus signal. The output is neither a varying voltage (analog) nor a standard serial protocol like I2C or SPI. Instead, the sensor communicates by pulling the data line LOW and HIGH for specific microsecond durations to represent binary 1s and 0s. No user calibration or scaling coefficients are required; the sensor outputs fully compensated physical units, provided your microcontroller correctly decodes the 40-bit data packet.
Wiring Pinout, Supply Range, and Pull-Up Rules
The bare DHT11 and DHT22 components have 4 pins, while the pre-wired AM2302 modules typically expose 3 wires. The most common point of failure in DIY builds is omitting the pull-up resistor or misidentifying the pinout on the bare component.
| Pin / Wire | Function | ESP32 / Arduino Connection | Electrical Notes & Supply Range |
|---|---|---|---|
| Pin 1 (Red) | VCC | 3.3V or 5V Pin | Supply range: 3.3V to 5.5V DC. Draw: ~1.5mA peak during conversion. |
| Pin 2 (Yellow/White) | DATA | Any GPIO (e.g., GPIO 4) | Requires 4.7kΩ pull-up to VCC. Max reliable wire length: 20 meters. |
| Pin 3 | NC (No Connect) | Leave Floating | Internally unconnected on DHT22/AM2302. |
| Pin 4 (Black) | GND | Common GND | Must share common ground with the microcontroller. |
Decoding the 40-Bit Protocol: Raw Reading to Physical Units
To interface with a sensor DHT without relying blindly on third-party libraries, you must understand the 40-bit data packet structure. The host microcontroller initiates communication by pulling the DATA line LOW for at least 18 milliseconds, then releases it. The sensor responds with an 80µs LOW pulse followed by an 80µs HIGH pulse to signal readiness.
Next, the sensor clocks out 40 bits of data. The timing of the HIGH pulse dictates the bit value:
- Logic 0: 50µs LOW followed by 26-28µs HIGH.
- Logic 1: 50µs LOW followed by 70µs HIGH.
The Raw-to-Unit Math
The 40 bits are divided into five 8-bit bytes: [Humidity High] [Humidity Low] [Temp High] [Temp Low] [Checksum]. Here is the exact math to convert the raw 16-bit integers into physical units for the DHT22 / AM2302:
- Humidity (% RH): Combine the first two bytes into a 16-bit integer. Divide by 10.0.
Humidity = ((Byte1 << 8) | Byte2) / 10.0
Example: Raw bytes 0x02 and 0x8C = 652. Divide by 10 = 65.2% RH. - Temperature (°C): Combine the third and fourth bytes. The most significant bit (MSB) of Byte 3 is the sign bit (1 = negative, 0 = positive). Mask out the sign bit, combine, and divide by 10.0.
Temp = ((Byte3 & 0x7F) << 8 | Byte4) / 10.0
If Byte3 MSB is 1, multiply the final result by -1. - Checksum: Add Bytes 1 through 4. The lowest 8 bits of the sum must equal Byte 5. If they do not match, discard the reading as corrupted.
Temp = Byte3 and Humidity = Byte1.
DHT11 vs DHT22 vs AM2302: The Decision Tree
Makers frequently waste time debating which sensor to use, or they buy the bare DHT22 and struggle with breadboard wiring. Use this decision path to select the exact part number for your workbench.
| Condition / Requirement | Recommended Part | Typical Cost (2026) |
|---|---|---|
| Budget is strictly under $2; indoor use only; 0°C to 50°C range is sufficient. | DHT11 | $1.20 - $1.80 |
| Need -40°C to 80°C range; 0.1°C resolution; bare PCB mounting. | DHT22 | $3.50 - $5.00 |
| Need DHT22 specs but want pre-attached wires and a built-in pull-up resistor for fast prototyping. | AM2302 | $4.50 - $6.50 |
| Require true I2C communication, fast sampling, or wire runs longer than 20 meters. | Sensirion SHT31 | $6.00 - $9.00 |
The Concrete Pick
For 95% of DIY home automation, greenhouse, and HVAC monitoring projects, buy the AM2302 (the wired version of the DHT22). It eliminates the need to source and solder a 4.7kΩ pull-up resistor, the color-coded wires prevent pinout reversal, and the internal NTC/polymer elements offer vastly superior accuracy over the DHT11. If your project requires wiring the sensor through walls or querying it more than once per second, abandon the DHT family entirely and upgrade to the Sensirion SHT31, which uses a robust I2C interface and supports much longer cable runs.
Common Interference Sources and Debugging Failures
When your ESP32 or Arduino throws a DHT_ERROR_TIMEOUT or returns NaN (Not a Number), the issue is almost always physical layer interference or timing violations. Here are the primary culprits and how to fix them.
1. Polling Too Fast (Timing Violations)
The internal microcontroller in the DHT family requires time to sample the analog elements and perform ADC conversions. The DHT11 requires a minimum of 1 second between reads (1 Hz). The DHT22/AM2302 requires a minimum of 2 seconds between reads (0.5 Hz). If your code loops without a delay, or if you use an RTOS task that queries the sensor every 500ms, the sensor will lock up or return stale data. Fix: Enforce a strict delay(2500) or use a millis()-based non-blocking timer set to 3000ms between read commands.
2. Cable Capacitance and Wire Length
Because the single-bus protocol relies on microsecond rise and fall times, the parasitic capacitance of long wires will smear the digital edges. A 50µs pulse can easily stretch into a 90µs pulse if you use 30 meters of thin, unshielded bell wire, causing the microcontroller to misread a Logic 0 as a Logic 1. Fix: Keep DHT data cables under 20 meters. If you must run further, use thicker wire (e.g., 20 AWG), lower the pull-up resistor to 2.2kΩ to increase the rise-time current, or switch to an I2C sensor like the SHT31 with an I2C bus extender (e.g., P82B715).
3. EMI from Motors and Relays
The single-wire protocol has no hardware error correction. If a nearby AC contactor, stepper motor, or relay coil switches while the sensor is clocking out its 40 bits, electromagnetic interference (EMI) can induce a voltage spike on the data line, flipping a bit and causing a checksum failure. Fix: Route DHT sensor wires away from AC mains and inductive loads. If checksum failures persist, add a 100nF ceramic decoupling capacitor directly across the VCC and GND pins at the sensor housing to filter high-frequency power rail noise.
By respecting the strict timing requirements and understanding the raw bitwise math, you can integrate the DHT sensor family into your embedded projects with rock-solid reliability, knowing exactly when to use it and when to upgrade to a more advanced silicon alternative.






