DHT11 with Arduino: Read Temperature and Humidity Easily

If you’re looking to monitor temperature and humidity using a microcontroller, DHT11 with Arduino is one of the easiest and most beginner-friendly combinations to start with. In this tutorial, you’ll learn how to interface the DHT11 with Arduino, read environmental data, and display it on the Serial Monitor.


What is the DHT11 Sensor?

DHT11 is a low-cost digital sensor used to measure temperature and humidity. When paired with an Arduino, it becomes a powerful tool for building DIY weather stations, automation systems, or learning basic electronics. Using DHT11 with Arduino requires just a single digital pin, making it efficient for compact projects.


DHT11 vs. DHT22: Key Differences

dht11-and-dht22

Here’s how DHT11 compares with DHT22:

FeatureDHT11DHT22
Temperature Range0-50 °C-40 to 80 °C
Temperature Accuracy±2 °C±0.5 °C
Humidity Range20-90% RH0-100% RH
Humidity Accuracy±5% RH±2-5% RH
Sampling Rate1 Hz (1 second)0.5 Hz (2 seconds)
CostCheaperSlightly more expensive

If you only need basic readings, using DHT11 with Arduino is sufficient. For higher accuracy or extreme ranges, consider DHT22 instead.


Required Components

To set up DHT11 sensor with Arduino, you’ll need:

  • Arduino Uno (or compatible board)
  • DHT11 sensor module
  • Breadboard and jumper wires
  • (Optional) 10kΩ pull-up resistor

DHT11 Pinout

The 3-pin DHT11 sensor typically has the following pinout:

PinDescription
VCCPower supply (3.3V–5V)
GNDGround
DATADigital signal output
DHT11 Pinout

When using DHT11 with Arduino, connect the DATA pin to any digital pin (we’ll use pin 2 in our example).


Circuit Diagram: DHT11 with Arduino

Wire the sensor as follows:

DHT11 PinArduino Pin
VCC5V
GNDGND
DATADigital Pin 2

If you’re experiencing signal issues, place a 10kΩ resistor between VCC and DATA.


Installing the DHT Library

Before using the DHT sensors, you need to install the DHT library in the Arduino IDE:

  1. Open the Arduino IDE and go to Sketch > Include Library > Manage Libraries…
  2. In the Library Manager, search for DHT Sensor Library by Adafruit.
  3. Click Install and wait for the installation to complete.

For more details or manual installation, you can visit the Adafruit DHT Sensor Library GitHub repository.


Arduino Code for DHT11

Upload the following code to your Arduino:

// Interfacing DHT11 Sensor with Arduino by ArduinoYard
#include "DHT.h"

#define DHTPIN 2        // DHT11 data pin connected to digital pin 2
#define DHTTYPE DHT11   // Define sensor type

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
  Serial.println("Reading from DHT11 sensor...");
}

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

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

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

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

  delay(2000); // Wait 2 seconds before the next reading
}

How the Code Works

  • Sensor Type Setup: We define the sensor type using #define DHTTYPE DHT11.
  • Pin Configuration: The sensor is connected to digital pin 2.
  • Reading Values: The readTemperature() and readHumidity() functions fetch the environmental data.
  • Error Handling: If data is invalid, a failure message is shown.

This basic sketch forms the foundation for any project using DHT11 with Arduino.


Output Example

Hardware:

DHT11-Arduino-Hardware

Serial Monitor:

dht11-sm

These values are updated every 2 seconds. You can modify the delay to suit your needs.


Error Handling and Troubleshooting Tips

  • Error: Failed to read from DHT sensor!
    Ensure the wiring is correct and stable. Check the sensor pinout and verify the pull-up resistor.
  • Random Values:
    • Avoid long jumper wires as they may cause signal interference.
    • Verify the power supply voltage.
  • No Output in Serial Monitor:
    Confirm that the Serial Monitor baud rate matches the code (9600 in this case).

Real-World Applications of DHT11 with Arduino

  • Mini Weather Stations: Monitor room temperature and humidity.
  • Fan Control Systems: Trigger fans based on environmental conditions.
  • IoT Projects: Send climate data to the cloud or display on LCD/OLED.
  • Greenhouse Monitoring: Keep your plants healthy by monitoring air conditions.

Conclusion

With this detailed guide, you’ve learned how to interface both DHT11 and DHT22 sensors with Arduino to measure temperature and humidity. We covered everything from installing the necessary library to troubleshooting common issues. This tutorial serves as a foundation for building more advanced projects, such as IoT weather stations. In future articles, we’ll explore additional sensors and how to integrate them with Arduino. Stay tuned and happy coding!


Explore More Arduino Sensor Guides

Leave a Comment