Home ▸ Arduino ▸ Sensors Interfacing ▸ DHT11 & DHT22 Sensors

DHT11 and DHT22 Arduino Tutorial: Read Temperature and Humidity with Simple Wiring and Code

The DHT11 and DHT22 are low-cost digital sensors that measure temperature and humidity and send the result to the Arduino over a single data wire. This tutorial explains how the two sensors differ, how to wire them with the correct pull-up resistor, how to install the Adafruit library, and how to print the readings on the Serial Monitor. It also shows how to display the values on an LCD1602 with an I2C backpack.

The Serial Monitor output uses the settings from the Arduino UART tutorial, the display section builds on the I2C LCD1602 tutorial, and the Arduino UNO pinout guide lists the pins used in the wiring. Other ways to measure temperature and humidity are collected in the Arduino sensor interfacing tutorials.

How DHT11 and DHT22 Sensors Work

A DHT sensor combines a capacitive humidity sensor, a thermistor for the temperature and a small chip that converts both signals to digital data. The Arduino therefore receives finished values and needs no analog input or conversion formula.

DHT Single-Wire Protocol

Both sensors send their data over one wire, the DATA pin, with a proprietary single-bus protocol from the manufacturer. This protocol is not the Dallas 1-Wire protocol that the DS18B20 uses, so the OneWire library cannot read a DHT sensor and the DHT library cannot read a DS18B20.

A reading starts when the Arduino pulls the DATA line low. The sensor then answers with a frame of 40 bits, which is made of 16 bits of humidity, 16 bits of temperature and an 8-bit checksum. The library checks the checksum and returns NaN (not a number) when the data is damaged or the sensor does not answer.

Diagram of the 40-bit data frame of a DHT sensor with 16 bits of humidity, 16 bits of temperature and an 8-bit checksum

Where DHT Sensors Are Used

The low price and the simple interface make DHT sensors a common choice for room thermometers, greenhouse monitors, weather stations, humidifier control and small data loggers. They respond slowly and are not meant for fast changes, so they suit slowly changing conditions such as room air.


DHT11 vs DHT22 Comparison

The DHT22, which is also sold as the AM2302, measures over a wider range and with better accuracy than the DHT11. Both sensors use the same library and the same code, so the sensor can be changed later by editing one line.

DHT11 vs DHT22 Specifications

FeatureDHT11DHT22 (AM2302)
Temperature range0 to 50 °C-40 to 80 °C
Temperature accuracy±2 °C±0.5 °C
Humidity range20 to 90 % RH0 to 100 % RH
Humidity accuracy±5 % RH (±4 % RH at 25 °C)±2 % RH (maximum ±5 % RH)
Resolution1 °C and 1 % RH0.1 °C and 0.1 % RH
Sampling periodAt least 1 sAt least 2 s
Supply voltage3 to 5.5 V3.3 to 6 V
OutputDigital, single wireDigital, single wire
PriceLowerSlightly higher

The values come from the manufacturer datasheets, and individual modules can differ. The Adafruit library used in this tutorial waits at least 2 seconds between sensor reads for both types, so the DHT11 is read at the same pace as the DHT22 in practice.

 DHT11 and DHT22 temperature and humidity sensor breakout modules side by side

Choosing Between DHT11 and DHT22

The DHT11 is enough for simple indoor monitoring where whole-degree resolution is acceptable, for example a classroom demo or a basic room display. The DHT22 is the better choice when the temperature can fall below 0 °C, when the humidity can drop under 20 %, or when finer resolution and better accuracy matter.


Other Temperature and Humidity Sensors

Several I2C sensors are worth considering for new designs. The site covers the SHT21, the AHT20, the SHT30, SHT31 and SHT35 and the BME280, which also measures pressure. For temperature alone, the DS18B20 tutorial covers a sensor with a different protocol, and the BMP180 tutorial adds pressure and altitude.


DHT11 and DHT22 Pinout

The bare sensor has four pins, and most breakout modules expose only three of them. The pins of the bare sensor are numbered from left to right when the grid side faces the reader.

DHT11 and DHT22 Pinout Table

PinNameFunction
1VCCPower supply (3 to 5.5 V for the DHT11, 3.3 to 6 V for the DHT22)
2DATADigital data line, connected to an Arduino pin
3NCNot connected
4GNDGround
 DHT11 and DHT22 pinout diagram showing the VCC, DATA, NC and GND pin positions

The pin order on a three-pin module differs between manufacturers, so the labels printed on the board should be checked before the wires are connected. A reversed supply can damage the sensor.


DHT11 and DHT22 Arduino Wiring

The sensor needs three connections to the Arduino: power, ground and one data line. A bare four-pin sensor also needs a pull-up resistor on the data line.

Required Components

  • Arduino UNO or Nano
  • DHT11 or DHT22 sensor, or a breakout module
  • Pull-up resistor of 4.7 kΩ to 10 kΩ (bare sensor only)
  • Breadboard and jumper wires

DHT11 and DHT22 to Arduino Connections

Sensor pinArduino pin
VCC5V
DATAD2
GNDGND

Any digital pin can be used for DATA, and this tutorial uses D2. A sensor that runs from 3.3 V can also be powered from the 3.3 V pin of a 3.3 V board, as long as the pull-up resistor is connected to the same 3.3 V rail.

DHT11 and DHT22 to Arduino Uno wiring diagram with DATA on digital pin 2, VCC on 5V and GND on GND

Pull-Up Resistor for the Bare Sensor

The DATA line of a bare sensor needs a pull-up resistor to VCC so that the line rests at a high level between transfers. A value between 4.7 kΩ and 10 kΩ is common, and the DHT11 datasheet recommends 5 kΩ for cables shorter than 20 m. Most three-pin breakout modules already carry this resistor, so no extra part is needed for them.

The Adafruit library switches on the internal pull-up of the Arduino pin in begin(), but that resistor is much weaker than the value in the datasheet, so the external resistor is still recommended for a bare sensor. Both datasheets also advise a 100 nF capacitor between VCC and GND close to the sensor, and the sensor must not receive commands during the first second after power-up.

Wiring schematic of a four-pin DHT sensor connected to an Arduino with a pull-up resistor between the VCC and DATA lines

Arduino DHT Library Setup

The sketches in this tutorial use the Adafruit DHT sensor library, which handles the timing of the single-wire protocol. The library depends on the Adafruit Unified Sensor library, which is installed together with it.

Install the Adafruit DHT Sensor Library

  1. Open the Arduino IDE and go to Sketch, then Include Library, then Manage Libraries.
  2. Search for DHT sensor library.
  3. Install the entry published by Adafruit.
  4. Accept the offer to install the missing dependencies, which includes Adafruit Unified Sensor.
Arduino IDE Library Manager showing the Adafruit DHT sensor library ready to install
Arduino IDE Library Manager showing the Adafruit Unified Sensor library that the DHT library needs

After the installation, the library is included in a sketch with the line below.

#include "DHT.h"

Arduino DHT11 and DHT22 Serial Monitor Code

The first sketch reads the humidity and the temperature every two seconds and prints both values on the Serial Monitor. The Serial Monitor must be set to 9600 baud.

Serial Monitor Sketch

#include "DHT.h"

#define DHTPIN 2         // Data pin connected to the sensor
#define DHTTYPE DHT11    // Change to DHT22 if you use that sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
  Serial.println("DHT sensor reading started...");
}

void loop() {
  delay(2000);   // The library returns the previous reading if it is called more often than every 2 seconds

  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();   // Celsius by default

  // NaN means the read failed because of a timeout or a checksum error
  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from the DHT sensor. Check the wiring and the pull-up resistor.");
    return;
  }

  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.print(" %\t");

  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");
}

How the Serial Monitor Code Works

  • DHTPIN and DHTTYPE select the data pin and the sensor type, and they are the only lines that change between a DHT11 and a DHT22.
  • dht.begin() prepares the data pin and the library timing.
  • The two-second delay matches the minimum interval of the library. A call that comes sooner returns the previous reading instead of a new one.
  • readHumidity() and readTemperature() return NaN when the read fails, which the sketch tests with isnan() before it prints anything.
  • readTemperature() returns degrees Celsius, and readTemperature(true) returns degrees Fahrenheit.

DHT11 Output

The DHT11 resolves 1 °C and 1 % RH, so its readings change in whole steps.

 Arduino Serial Monitor showing DHT11 temperature and humidity readings that update every 2 seconds

DHT22 Output

To use a DHT22, only the sensor type changes.

#define DHTTYPE DHT22

The DHT22 resolves 0.1 °C and 0.1 % RH, so the readings show finer steps than those of the DHT11.

Arduino Serial Monitor showing DHT22 temperature and humidity readings with finer steps than the DHT11

Arduino DHT22 LCD1602 I2C Display

An LCD1602 with an I2C backpack shows the readings without a computer. The display needs only two signal wires, and its basics are covered in the I2C LCD1602 Arduino tutorial.

DHT22 and LCD1602 Wiring

PartPinArduino pin
DHT22VCC5V
DHT22DATAD2
DHT22GNDGND
LCD1602 I2CVCC5V
LCD1602 I2CGNDGND
LCD1602 I2CSDAA4 (UNO and Nano)
LCD1602 I2CSCLA5 (UNO and Nano)

The I2C pins are different on other boards, for example pins 20 (SDA) and 21 (SCL) on the Mega 2560. The Arduino I2C tutorial lists the pins of each board together with an address scanner.


LiquidCrystal_I2C Library Setup

  1. Open the Arduino IDE and go to Sketch, then Include Library, then Manage Libraries.
  2. Search for LiquidCrystal I2C.
  3. Install the version by Frank de Brabander.
 Arduino IDE Library Manager showing the LiquidCrystal I2C library by Frank de Brabander ready to install

The sketch calls lcd.init() and lcd.backlight(), which this library provides. Other libraries with the same name can use different function names.


LCD1602 I2C Address

The sketch uses the address 0x27. Backpacks with a PCF8574 chip use addresses from 0x20 to 0x27, and backpacks with a PCF8574A chip use addresses from 0x38 to 0x3F, so 0x3F is the other common value. The sketch checks the address at startup and prints a message on the Serial Monitor when the display does not answer.


DHT22 LCD1602 Sketch

The sketch reads the DHT22 every two seconds and rewrites both lines of the display. The text is padded to 16 characters, so a shorter value never leaves old digits behind, and the display is not cleared on every pass, which avoids flicker. To use a DHT11, change DHTTYPE to DHT11.

#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include "DHT.h"

#define DHTPIN 2         // Data pin connected to the sensor
#define DHTTYPE DHT22    // Change to DHT11 if you use that sensor

const uint8_t LCD_ADDRESS = 0x27;            // Use 0x3F if your backpack uses that address
const unsigned long READ_INTERVAL_MS = 2000; // Minimum interval of the library

DHT dht(DHTPIN, DHTTYPE);
LiquidCrystal_I2C lcd(LCD_ADDRESS, 16, 2);   // 16 columns, 2 rows

unsigned long lastRead = 0;

// Prints a text of up to 16 characters on one line and pads the rest with spaces
void printLine(uint8_t row, const char *text) {
  char line[17];
  snprintf(line, sizeof(line), "%-16s", text);
  lcd.setCursor(0, row);
  lcd.print(line);
}

void setup() {
  Serial.begin(9600);

  // Check that the LCD backpack answers on the I2C bus
  Wire.begin();
  Wire.beginTransmission(LCD_ADDRESS);
  if (Wire.endTransmission() != 0) {
    Serial.println("LCD not found. Check the wiring and the I2C address.");
  }

  lcd.init();
  lcd.backlight();
  dht.begin();

  printLine(0, "DHT sensor");
  printLine(1, "Starting...");
  lastRead = millis() - READ_INTERVAL_MS;    // Allows the first read after the 2 second start screen
  delay(2000);
}

void loop() {
  if (millis() - lastRead < READ_INTERVAL_MS) {
    return;                                  // Not time for a new reading yet
  }
  lastRead = millis();

  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();

  // NaN means the read failed because of a timeout or a checksum error
  if (isnan(humidity) || isnan(temperature)) {
    printLine(0, "Sensor error");
    printLine(1, "Check wiring");
    return;
  }

  char temperatureText[17];
  char humidityText[17];
  char valueBuffer[8];

  dtostrf(temperature, 4, 1, valueBuffer);   // Converts the float to text with 1 decimal
  snprintf(temperatureText, sizeof(temperatureText), "Temp: %s C", valueBuffer);

  dtostrf(humidity, 4, 1, valueBuffer);
  snprintf(humidityText, sizeof(humidityText), "Hum:  %s %%", valueBuffer);

  printLine(0, temperatureText);
  printLine(1, humidityText);
}

The sketch uses dtostrf() because the snprintf() of the AVR core does not print float values. The timing uses millis() and does not block the loop, so the sketch can also handle other tasks between two readings.


LCD1602 Output

The display shows the temperature in the first line and the humidity in the second line.

LCD1602 I2C display showing the DHT22 temperature in line 1 and the humidity in line 2, connected to an Arduino Uno

DHT11 and DHT22 Troubleshooting

The table lists the most common problems with DHT sensors and the usual fix for each one.

ProblemLikely causeFix
The Serial Monitor always prints the failure messageWrong data pin, missing pull-up resistor, or a wrong DHTTYPEConfirm that DATA goes to D2, add a 4.7 kΩ to 10 kΩ pull-up between VCC and DATA on a bare sensor, and match DHTTYPE to the sensor
Readings fail now and thenNoise on a long data cable, or an unstable supplyShorten the wires, add a 100 nF capacitor between VCC and GND near the sensor, and use a stable 5 V supply
The values repeat and do not changeThe sketch reads more often than every 2 seconds, so the library returns the previous resultKeep at least 2000 ms between two reads
The reading is 0 % or 100 % humidity, or shows a wrong temperatureThe sensor is wet or damaged, or the DHTTYPE is wrongDry the sensor, check DHTTYPE, and replace the sensor if the values stay wrong
The LCD stays blankWrong I2C address, SDA and SCL swapped, or the contrast set too lowCheck the address (0x27 or 0x3F), confirm A4 and A5, and adjust the contrast potentiometer on the backpack
The first reading after power-up failsThe sensor needs about 1 second after power-up before it accepts commandsAdd a short delay after power-up before the first read
The temperature reads a little too highHeat from the Arduino, the LCD backlight or a nearby componentPlace the sensor away from heat sources and test it against a reference thermometer

For serial output problems, the Arduino UART tutorial explains the Serial Monitor settings.


Conclusion

The DHT11 and DHT22 add temperature and humidity sensing to an Arduino with three wires and one library. This tutorial covered the differences between the two sensors, the pinout, the pull-up resistor, the library setup, the Serial Monitor sketch and a non-blocking LCD1602 sketch that handles read errors.

For better accuracy or an I2C interface, the BME280 tutorial is a good next step, because it adds pressure to the temperature and humidity readings. Readings can also be stored with the SD card logging tutorial or time-stamped with the DS3231 RTC tutorial.


Download Project Here

Download the CubeMX project files and HAL source code — all tested on real hardware.

If you find this useful, you can support us with a Donation as well.

Browse More Arduino Sensor Tutorials

About the Author
Arun Rawat
Arun Rawat
Embedded Systems Engineer · Founder, ControllersTech

Arun is an embedded systems engineer with 10+ years of experience in STM32, ESP32, and AVR microcontrollers. He created ControllersTech to share practical tutorials on embedded software, HAL drivers, RTOS, and hardware design — grounded in real industrial automation experience.

Subscribe
Notify of

0 Comments
Newest
Oldest Most Voted
×

Don’t Miss Future STM32 Tutorials

Join thousands of developers getting free guides, code examples, and updates.