DHT11 / DHT22 with ESP8266 (NodeMCU / Wemos D1 Mini)

MuhammadMuhammadESP82663 months ago33 Views

Introduction

The DHT11 and DHT22 are widely used sensors to measure temperature and humidity.

  • DHT11 → Cheaper, less accurate, works between 0–50°C, 20–80% RH.
  • DHT22 → More accurate, wider range: -40–80°C, 0–100% RH.

In this tutorial, you’ll learn how to:

  1. Connect DHT11/DHT22 with ESP8266
  2. Read temperature and humidity values
  3. Print data on the Serial Monitor

Introduction

The DHT11 and DHT22 are widely used sensors to measure temperature and humidity.

  • DHT11 → Cheaper, less accurate, works between 0–50°C, 20–80% RH.
  • DHT22 → More accurate, wider range: -40–80°C, 0–100% RH.

In this tutorial, you’ll learn how to:

DHT11 vs DHT22 Comparison

FeatureDHT11DHT22 (AM2302)
Temperature Range0°C to 50°C-40°C to 80°C
Temperature Accuracy±2°C±0.5°C
Humidity Range20% to 80% RH0% to 100% RH
Humidity Accuracy±5% RH±2–3% RH
Sampling Rate1 reading per second (1 Hz)0.5 reading per second (0.5 Hz)
Resolution8-bit16-bit
Operating Voltage3.3V – 5V3.3V – 6V
Max Current2.5 mA2.5 mA
Data FormatSingle-wire digital signalSingle-wire digital signal
CostCheaper (low-cost sensor)More expensive (higher accuracy)
Best Use CasesBasic DIY projects, beginnersWeather stations, IoT, precision apps

Components Required

  • ESP8266 board (NodeMCU / Wemos D1 Mini)
  • DHT11 or DHT22 sensor module
  • Jumper wires and breadboard

Circuit Diagram

Connections (using D1 for data):

  • VCC → 3.3V
  • GND → GND
  • DATA → D1

👉 Some DHT modules have a built-in resistor; if not, use a 10kΩ pull-up resistor between DATA and VCC.

Installing the DHT Library

  1. Open Arduino IDE → Go to Sketch > Include Library > Manage Libraries.
  2. Search for “DHT sensor library” by Adafruit and install it.
  3. Also install “Adafruit Unified Sensor” library.

DHT11 / DHT22 with ESP8266

#include "DHT.h"

#define DHTPIN D1      // Pin where DHT is connected
#define DHTTYPE DHT22  // Change to DHT11 if using DHT11

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(115200);
  dht.begin();
  Serial.println("DHT Sensor Test");
}

void loop() {
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature(); // Celsius
  float temperatureF = dht.readTemperature(true); // Fahrenheit

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

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

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

  delay(2000); // Read every 2 seconds
}

Step-by-Step Code Explanation

  1. #include "DHT.h" → Includes Adafruit’s DHT library.
  2. #define DHTPIN D4 → Defines GPIO pin where sensor is connected.
  3. #define DHTTYPE DHT22 → Defines sensor type (change to DHT11 if using DHT11).
  4. DHT dht(DHTPIN, DHTTYPE); → Creates sensor object.
  5. dht.begin(); → Initializes sensor.
  6. float humidity = dht.readHumidity(); → Reads humidity in %.
  7. float temperature = dht.readTemperature(); → Reads temperature in °C.
  8. float temperatureF = dht.readTemperature(true); → Reads in °F.
  9. isnan() check ensures values are valid.
  10. Results are printed every 2 seconds.

Output on Serial Monitor

Humidity: 55.2 %   Temperature: 27.3 °C  81.1 °F
Humidity: 54.8 %   Temperature: 27.5 °C  81.5 °F

Real-Life Applications

  • Smart Home Automation (monitor indoor air quality)
  • Weather Stations
  • Greenhouse Monitoring
  • IoT Cloud Dashboards (send data to ThingSpeak, Blynk, or MQTT)

Troubleshooting

  • If readings show nan (Not a Number) → check wiring and resistor.
  • Use short wires; DHT sensors are sensitive to noise.
  • Try slowing down reading frequency (minimum 2 seconds delay).

Conclusion

You’ve learned how to:
✅ Connect DHT11/DHT22 with ESP8266
✅ Read temperature and humidity values
✅ Print results in Serial Monitor

This project is the foundation for IoT weather and smart agriculture projects 🚀.

What’s the difference between DHT11 and DHT22?

DHT11: Cheaper, less accurate, limited range.
DHT22: More accurate, wider range, slightly more expensive.

Can I power DHT from 5V with ESP8266?

Yes, but ensure the data pin is 3.3V compatible (use a logic level shifter if needed).

How often can I read values?

Every 2 seconds or more (faster reads may cause errors).

0 Votes: 0 Upvotes, 0 Downvotes (0 Points)

Loading Next Post...
Follow
Search Trending
Popular Now
Loading

Signing-in 3 seconds...

Signing-up 3 seconds...