DHT11 & DHT22 Temperature Sensor – Complete Detailed Guide with Arduino (Working, Pinout, Wiring, Code & Troubleshooting)

Introduction

Temperature and humidity measurement is one of the most common requirements in electronics, IoT, and automation projects. Whether you are building a weather monitoring system, smart home, greenhouse controller, or Arduino-based learning project, sensors like DHT11 and DHT22 are widely used due to their simplicity, affordability, and reliability.

In this complete guide, we will explore everything about DHT11 and DHT22 temperature sensors in a step-by-step and professional manner. You will learn how these sensors work internally, their pin configuration, what parameters they can measure, and how to interface them with an Arduino Uno.
By the end of this article, you will be able to read temperature, humidity, and heat index values correctly using the Arduino IDE.

This guide is written for beginners as well as intermediate makers, and the same process applies if you are using Arduino Nano, ESP8266, ESP32, or other microcontrollers.


Step 1: What is DHT11 / DHT22 Temperature Sensor?

The DHT series (Digital Humidity and Temperature sensors) are single-wire digital sensors designed to measure:

  • 🌡 Temperature
  • 💧 Relative Humidity

Both DHT11 and DHT22 contain:

  • A humidity sensing component
  • A thermistor (temperature sensor)
  • An 8-bit microcontroller inside the module

This internal microcontroller converts analog signals into digital data, making it very easy to interface with microcontrollers like Arduino.


Step 2: Difference Between DHT11 and DHT22

Feature      DHT11       DHT22 (AM2302)
Temperature Range0°C to 50°C         -40°C to 80°C
Temperature Accuracy±2°C          ±0.5°C
Humidity Range20% to 90% RH0% to 100% RH
Humidity Accuracy±5% RH±2% RH
Resolution1°C0.1°C
CostLowSlightly higher

👉 DHT11 is best for learning and basic projects
👉 DHT22 is preferred for accurate and professional applications


Step 3: How Does DHT11 / DHT22 Work?

The working principle of DHT sensors is based on capacitive humidity sensing and thermistor temperature sensing.

🔸 Humidity Measurement

  • The sensor has a moisture-holding substrate
  • As humidity changes, capacitance changes
  • Internal ADC converts it into a digital value

🔸 Temperature Measurement

  • Uses an NTC thermistor
  • Resistance changes with temperature
  • Converted into calibrated digital output

🔸 Data Transmission

  • Uses a single-wire digital protocol
  • Sensor sends 40-bit data frame

    • 16 bits humidity
    • 16 bits temperature
    • 8 bits checksum

This makes the sensor noise-resistant and stable.


Step 4: Pin Configuration (Pinout)

Most DHT modules come in 3-pin or 4-pin versions.

🧷 DHT Sensor Pin Description

PinNameFunction
1VCCPower supply (3.3V – 5V)
2DATADigital data output
3NCNot connected (in 4-pin version)
4GNDGround

📌 If you are using a DHT sensor module, it already includes a pull-up resistor, so no external resistor is needed.


Step 5: What Can We Measure Using This Module?

Using DHT11 or DHT22, we can measure:

  • 🌡 Ambient Temperature (°C / °F)
  • 💧 Relative Humidity (%)
  • 🔥 Heat Index
    • Combination of temperature and humidity
    • Indicates how hot it actually feels

These values are useful for:

  • Weather stations
  • Smart fans & AC automation
  • Greenhouse monitoring
  • IoT dashboards
  • Home automation systems


Step 6: Required Components

To measure temperature using Arduino, you need:

  • Arduino Uno (Nano can also be used)
  • DHT11 or DHT22 Sensor Module
  • USB Type-B Cable
  • Laptop / PC with Internet
  • Arduino IDE Software
  • Jumper wires


Step 7: Wiring DHT Sensor with Arduino Uno

Now let us connect the sensor to the Arduino.

🔌 Connections

  • VCC → Arduino 5V (or 3.3V)
  • GND → Arduino GND
  • DATA → Arduino D2

✔ These connections are safe and recommended for both DHT11 and DHT22.

Now connect the Type-B USB cable to Arduino and plug the other end into your laptop.
Make sure your laptop internet is ON for library installation.


Step 8: Installing DHT Sensor Library in Arduino IDE

  1. Open Arduino IDE
  2. Go to Sketch → Include Library → Manage Libraries
  3. A new window will open
  4. In the search box, type DHT sensor
  5. Select “DHT sensor library by Adafruit”
  6. Install the latest version
  7. Also install Adafruit Unified Sensor Library if prompted

✅ Now the environment is ready.


Step 9: Selecting Board and COM Port

  1. Go to Tools → Board
  2. Select Arduino Uno (or Nano if you are using it)
  3. Go to Tools → Port
  4. Select the correct COM Port
  5. Keep all other settings default


Step 10: Selecting Example Code

  1. Go to File → Examples
  2. Scroll down to DHT sensor library
  3. Click on DHT_Unified_Sensor

This example supports both DHT11 and DHT22.


Step 11: Selecting Sensor Type in Code

Inside the code, you will see a define section.

  • If you are using DHT11, enable:
#define DHTTYPE DHT11

  • If you are using DHT22, enable:
#define DHTTYPE DHT22

⚠️ Make sure only one sensor type is active.


Step 12: Uploading the Code

  1. Click on the Upload button
  2. Wait for compilation and upload to finish
  3. You will see “Done Uploading” message


Step 13: Reading Output on Serial Monitor

  1. Go to Tools → Serial Monitor
  2. Set Baud Rate to 9600
  3. You will see:

    • Temperature (°C)
    • Humidity (%)
    • Heat Index

If the values look incorrect, re-check baud rate and wiring.


Step 14: Common Troubleshooting

❌ Showing NaN Values

  • Wrong sensor type selected
  • Loose data wire
  • Wrong pin number

❌ No Output

  • Incorrect COM port
  • Library not installed properly

❌ Fluctuating Values

  • Place sensor away from heat sources
  • Avoid touching sensor directly


✅ Conclusion

The DHT11 and DHT22 temperature sensors are excellent choices for learning and real-world projects. With minimal wiring and simple code, you can accurately measure temperature, humidity, and heat index using Arduino.

This complete step-by-step guide ensures that even beginners can successfully interface the sensor without confusion. Once you master this, you can easily integrate the sensor into IoT dashboards, LCD displays, Wi-Fi modules, and automation systems.


💬 Final Note

If you have any doubts, comments, or need advanced projects using DHT sensors, feel free to let me know.
Happy learning & happy making!

Post a Comment

0 Comments