Interfacing DHT11 and DHT22 Sensors with Arduino

DHT11 and DHT22 are widely used sensors for measuring temperature and humidity. These sensors are popular due to their simplicity, affordability, and compatibility with Arduino. In this tutorial, we’ll cover everything you need to know about interfacing these sensors with Arduino, including example code for both DHT11 and DHT22.


What Are DHT11 and DHT22 Sensors?

DHT sensors are digital temperature and humidity sensors. While DHT11 is an entry-level sensor, DHT22 offers higher accuracy and a wider range. Both sensors are commonly used in weather monitoring systems, IoT projects, and home automation setups.


DHT11 vs. DHT22: Key Differences

dht11-and-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

For detailed technical specifications, refer to the DHT11 datasheet and DHT22 datasheet.


Required Components

  1. Arduino Uno (or any compatible board)
  2. DHT11 or DHT22 sensor
  3. 10kΩ pull-up resistor (optional)
  4. Breadboard and jumper wires

DHT11 and DHT22 Pinout

Both sensors have three or four pins. For this tutorial, we’ll focus on the three-pin variant:

PinDescription
VCCPower (3.3V or 5V)
GNDGround
DATAData Output

Note:

  • The VCC pin supports 3.3V or 5V for both DHT11 and DHT22 sensors.
  • Always check your sensor’s datasheet to confirm the pinout.
  • Observe your specific module carefully, as different manufacturers may use varying pin orientations and configurations.

Circuit Diagram

To interface the DHT sensor with Arduino, connect the pins as follows:

DHT PinConnection
VCC5V on Arduino
GNDGND on Arduino
DataDigital Pin 2

For DHT11:

dht11-wiring

For DHT22:

dht22-wiring

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.


Code Example: Reading Temperature and Humidity with DHT Sensors

Below is a simple and efficient code snippet for reading temperature and humidity using either the DHT11 or DHT22 sensor with Arduino.

// Interfacing DHT11 and DHT22 Sensors with Arduino by ArduinoYard
#include "DHT.h"

// Define DHT type and pin
#define DHTPIN 2       // Data pin connected to digital pin 2
#define DHTTYPE DHT11  // DHT11 or DHT22 (change according to the sensor you are using)

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  Serial.begin(9600);
  dht.begin();
  Serial.println("DHT Arduino Tutorial: Reading Temperature and Humidity");
}

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

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

  // Print values to Serial Monitor
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" °C");
  
  Serial.print("Humidity: ");
  Serial.print(humidity);
  Serial.println(" %");

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

How This Code Works

  1. Library Inclusion:
    The DHT.h library simplifies reading data from the sensor. Make sure to install it via the Arduino IDE Library Manager.
  2. Sensor Type and Pin Definition:
    Change DHTTYPE to DHT22 if you’re using a DHT22 sensor. The data pin is connected to digital pin 2 in this example.
  3. Reading Data:
    • dht.readTemperature() reads the temperature in Celsius.
    • dht.readHumidity() reads the relative humidity percentage.
    • The isnan() function ensures valid readings.
  4. Delay Between Readings:
    The delay of 2 seconds ensures proper sampling, matching the sensor’s specifications.

Output Example

When the code runs, the Arduino Serial Monitor will display:

DHT11:

dht11-sm

DHT22:

sht22-sm

As you can see the DHT22 output is more precise and stable compared to the DHT11 sensor.


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).

Practical Applications of DHT Sensors

  1. Weather Monitoring Systems:
    Measure temperature and humidity in real-time for weather forecasting.
  2. IoT Devices:
    Integrate with IoT platforms to monitor environmental conditions remotely.
  3. HVAC Systems:
    Use the data for smart heating, ventilation, and air conditioning systems.
  4. Greenhouses:
    Maintain optimal conditions for plant growth by monitoring humidity and temperature.

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!


Recommended Articles

  1. How to Toggle an LED Using a Webpage with ESP32
  2. Interfacing Ultrasonic Sensor with Arduino
  3. How to Use OLED Displays with Arduino

Leave a Comment