0
0
AutocadHow-ToBeginner · 3 min read

How to Use DHT22 Sensor with Arduino: Simple Guide

To use a DHT22 sensor with Arduino, connect its power, ground, and data pin to the Arduino board, then use the DHT library to read temperature and humidity values. Initialize the sensor in your code and call its read functions inside loop() to get updated data.
📐

Syntax

The basic syntax to use the DHT22 sensor involves including the DHT.h library, defining the sensor type and data pin, initializing the sensor object, 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 DHT22: Defines the sensor model.
  • DHT dht(DHTPIN, DHTTYPE);: Creates a sensor object.
  • dht.begin();: Starts the sensor.
  • dht.readTemperature() and dht.readHumidity(): Read temperature and humidity.
arduino
#include <DHT.h>

#define DHTPIN 2      // Pin connected to the data pin of DHT22
#define DHTTYPE DHT22 // Define 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 values here
  delay(2000); // Wait 2 seconds before next read
}
💻

Example

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

arduino
#include <DHT.h>

#define DHTPIN 2
#define DHTTYPE DHT22

DHT dht(DHTPIN, DHTTYPE);

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

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

  if (isnan(humidity) || isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    Serial.print("Humidity: ");
    Serial.print(humidity);
    Serial.print(" %\t");
    Serial.print("Temperature: ");
    Serial.print(temperature);
    Serial.println(" *C");
  }
  delay(2000);
}
Output
DHT22 sensor reading started Humidity: 45.00 % Temperature: 23.50 *C Humidity: 44.80 % Temperature: 23.60 *C Humidity: 45.10 % Temperature: 23.55 *C ...
⚠️

Common Pitfalls

  • Wrong wiring: Make sure the sensor's power (VCC) is connected to 5V or 3.3V (check your sensor specs), ground to GND, and data pin to the correct Arduino pin.
  • Missing pull-up resistor: The data line usually needs a 10kΩ pull-up resistor between data and power to work reliably.
  • Not waiting between reads: The DHT22 sensor needs about 2 seconds between readings; reading faster causes errors.
  • Not checking for failed reads: Always check if the sensor returns valid data using isnan() before using values.
arduino
// Wrong way (no check for failed read):
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
Serial.print(humidity); // May print 'nan' if read fails

// Right way:
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
  Serial.println("Failed to read from DHT sensor!");
} else {
  Serial.print("Humidity: ");
  Serial.println(humidity);
  Serial.print("Temperature: ");
  Serial.println(temperature);
}
📊

Quick Reference

Remember these quick tips when using the DHT22 sensor with Arduino:

  • Use the DHT library for easy sensor handling.
  • Connect sensor data pin to a digital pin with a 10kΩ pull-up resistor.
  • Wait at least 2 seconds between sensor reads.
  • Always check for nan values to avoid errors.
  • Print results to Serial Monitor for debugging.

Key Takeaways

Connect DHT22 sensor data pin to Arduino digital 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 readings are valid using isnan() before using the data.
Print sensor values to Serial Monitor to verify correct operation.