0
0
AutocadHow-ToBeginner · 3 min read

How to Use DHT11 Sensor with Arduino: Simple Guide

To use a DHT11 sensor with Arduino, connect its data pin to a digital pin on the Arduino, and use a library like DHT.h to read temperature and humidity values. Initialize the sensor in your code, then call functions to get the readings and print them to the Serial Monitor.
📐

Syntax

The basic syntax to use the DHT11 sensor involves including the DHT.h library, defining the sensor type and pin, initializing the sensor, and then reading temperature and humidity values.

  • #include <DHT.h>: Includes the DHT sensor library.
  • #define DHTPIN 2: Sets the Arduino pin connected to the sensor data pin.
  • #define DHTTYPE DHT11: Defines the sensor model.
  • DHT dht(DHTPIN, DHTTYPE);: Creates a sensor object.
  • dht.begin();: Starts communication with the sensor.
  • dht.readHumidity() and dht.readTemperature(): Functions to get humidity and temperature.
arduino
#include <DHT.h>

#define DHTPIN 2     // Digital pin connected to the DHT11 sensor
#define DHTTYPE DHT11   // DHT 11 sensor type

DHT dht(DHTPIN, DHTTYPE);

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

void loop() {
  float humidity = dht.readHumidity();
  float temperature = dht.readTemperature();
  // Use the readings here
}
💻

Example

This example shows how to read temperature and humidity from the DHT11 sensor and print the values to the Serial Monitor every 2 seconds.

arduino
#include <DHT.h>

#define DHTPIN 2
#define DHTTYPE DHT11

DHT dht(DHTPIN, DHTTYPE);

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

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(" %\t");
  Serial.print("Temperature: ");
  Serial.print(temperature);
  Serial.println(" *C");

  delay(2000);
}
Output
DHT11 sensor reading example Humidity: 45.00 % Temperature: 23.00 *C Humidity: 46.00 % Temperature: 23.00 *C Humidity: 45.00 % Temperature: 22.00 *C ...
⚠️

Common Pitfalls

Common mistakes when using the DHT11 sensor include:

  • Not connecting the sensor's power and ground pins correctly.
  • Forgetting to use a pull-up resistor (usually 10k ohm) on the data line.
  • Using the wrong sensor type in code (e.g., DHT22 instead of DHT11).
  • Reading sensor data too frequently; the DHT11 needs about 1-2 seconds between reads.
  • Not checking if the sensor reading is valid (NaN values).
arduino
// Wrong: No delay between reads, may cause errors
void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  Serial.print(h);
  Serial.print(" %");
  Serial.print(t);
  Serial.println(" *C");
}

// Right: Add delay and check for valid readings
void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from sensor");
  } else {
    Serial.print("Humidity: ");
    Serial.print(h);
    Serial.print(" %");
    Serial.print(" Temperature: ");
    Serial.print(t);
    Serial.println(" *C");
  }
  delay(2000);
}
📊

Quick Reference

Tips for using DHT11 sensor with Arduino:

  • Use a 10k ohm pull-up resistor on the data pin.
  • Wait at least 2 seconds between sensor reads.
  • Always check if readings are valid before using them.
  • Connect sensor VCC to 5V and GND to ground.
  • Use the DHT.h library for easy sensor handling.

Key Takeaways

Connect the DHT11 sensor data pin to a digital Arduino pin with a 10k pull-up resistor.
Use the DHT library to initialize and read temperature and humidity values.
Wait at least 2 seconds between sensor readings to get accurate data.
Always check if sensor readings are valid (not NaN) before using them.
Power the sensor with 5V and ground it properly for stable operation.