Skip to content

Learn DHT11 & DHT22 Interfacing with Arduino Nano – Step-by-Step Guide

DHT 11 / 22

Table of Contents

Abstract

In this article, we will learn how to interface the DHT11 / DHT22 temperature and humidity sensor with the Arduino Nano. These sensors are widely used in IoT, weather monitoring, HVAC systems, and smart agriculture projects.

We will cover the hardware connections, understand how the sensor works, write the Arduino program, and test the setup using real hardware or Wokwi simulation.

🧭 Pre-Request

  • OS : Windows / Linux / Mac / Chrome
  • Arduino IDE

Hardware Required

🛠️
Get the right hardware kit
Arduino boards, sensors, and maker essential Kits—perfectly matched for your learning.
Explore Hardware →
  • Arduino Nano.
  • DHT11 or DHT22 sensor
  • BreadBoard.
  • Mini USB Cable.
  • Connecting wires.
  • 5V DC power supply (Optional)
Components Purchase Link
Arduino Nano link
DHT 11 link
DHT 22 link
Mini USB Cable link
BreadBoard large : small
Connecting Wires link
5V DC Adaptor link

Don't own a hardware 😢

No worries,

💡Still you can learn using simulation. check out simulation part 😃.

💡Power your mission with reliable Arduino Kits. Explore Hardware →

  • Arduino Nano

  • Arduino Nano

Understanding the DHT11 / DHT22 Sensor

The DHT sensors measure:

  • Temperature
  • Relative Humidity

They use a single-wire digital communication protocol, making them very easy to interface with Arduino.

Differences

Feature DHT11 DHT22
Temperature Range 0–50 °C −40 to 80 °C
Humidity Range 20–80 % 0–100 %
Accuracy Moderate High
Cost Low Slightly higher

Sensor Pinout

Most DHT modules have 3 pins:

Pin Function
VCC Power (3.3 V / 5 V)
DATA Data Output
GND Ground

Connection Table

DHT Sensor → Arduino Nano

DHT Pin Arduino Nano
VCC 5V
DATA D2
GND GND

Note

💡 If using a bare DHT sensor, connect a 10 kΩ pull-up resistor between VCC and DATA.

Circuit Diagram

fig-Connection Diagram

📂 Code

Info

Install the DHT Sensor Library from Arduino Library Manager.

#include <DHT.h>

#define DHTPIN 2        // Data pin connected to Arduino Nano
#define DHTTYPE DHT22  // Change to DHT11 if using DHT11

DHT dht(DHTPIN, DHTTYPE);

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

void loop() {
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();

if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
}

Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %  |  ");

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

delay(2000);
}

Code Explanation

Let’s break down the Arduino sketch line by line to clearly understand how the DHT22 works with the Arduino Nano.

👉 Imports

#include <DHT.h>
  • DHT.h This library provides all the functions needed to communicate with DHT sensors, such as reading temperature and humidity values.
  • 💡 These libraries hide the low-level DHT22 complexity and make sensor interface simple.

👉 Define Sensor details

3
4
5
6
#define DHTPIN 2        // Data pin connected to Arduino Nano
#define DHTTYPE DHT22  // Change to DHT11 if using DHT11

DHT dht(DHTPIN, DHTTYPE);
  • Sensor type connected is DHT22
  • D2 → connected to DHT22 Signal pin
  • Create a DHT sensor object named dht, using the defined pin and sensor type. All sensor readings will be taken using this object.
void setup() {
    Serial.begin(9600);
    dht.begin();
}

void loop() {
    float humidity = dht.readHumidity();
    float temperature = dht.readTemperature();

    if (isnan(humidity) || isnan(temperature)) {
        Serial.println("Failed to read from DHT sensor!");
        return;
    }

    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.print(" %  |  ");

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

    delay(2000);
}
  • dht.begin() starts sensor communication.
  • readHumidity() and readTemperature() get real-time data.
  • Data is displayed on the Serial Monitor every 2 seconds.

Common Issues & Troubleshooting

Issue Solution
No readings Check DATA pin number
NaN values Check sensor type
Sensor not detected Ensure pull-up resistor
Wrong values Use correct DHT type

Simulation

Not able to view the simulation

  • Desktop or Laptop : Reload this page ( Ctrl+R )
  • Mobile : Use Landscape Mode and reload the page
🛠️
Get the right hardware kit
Arduino boards, sensors, and maker essential Kits—perfectly matched for your learning.
Explore Hardware →
  • Arduino Nano

  • Arduino Nano


Extras

Components details

Modules / Libraries Used


Conclusion

Interfacing a DHT11 or DHT22 sensor with Arduino Nano is one of the easiest ways to build a temperature and humidity monitoring system. With only one data wire, the sensor provides reliable environmental data that can be used in IoT dashboards, weather stations, greenhouses, and smart homes.

Using Wokwi simulation, you can test and debug your project even without physical hardware, making this setup perfect for learning, teaching, and prototyping.