

The DHT22 is a digital temperature and humidity sensor that provides accurate readings of temperature in Celsius and humidity in percentage. It is widely used in applications requiring environmental monitoring, such as weather stations, HVAC systems, greenhouses, and IoT projects. The DHT22 is known for its high accuracy and reliability, making it a popular choice for both hobbyists and professionals.








The DHT22 has four pins, as described in the table below:
| Pin Number | Name | Description |
|---|---|---|
| 1 | VCC | Power supply pin (3.3V to 6V). Connect to the positive terminal of the power source. |
| 2 | DATA | Digital data output pin. Connect to a microcontroller's GPIO pin with a pull-up resistor. |
| 3 | NC (Not Connected) | This pin is not used and should be left unconnected. |
| 4 | GND | Ground pin. Connect to the negative terminal of the power source. |
Below is an example of how to use the DHT22 with an Arduino UNO:
#include "DHT.h"
// Define the pin where the DHT22 is connected
#define DHTPIN 2
// Define the type of DHT sensor (DHT22 in this case)
#define DHTTYPE DHT22
// Initialize the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600); // Start the serial communication
dht.begin(); // Initialize the DHT sensor
Serial.println("DHT22 Sensor Initialized");
}
void loop() {
delay(2000); // Wait 2 seconds between readings
// Read temperature in Celsius
float temperature = dht.readTemperature();
// Read humidity in percentage
float humidity = dht.readHumidity();
// Check if the readings are valid
if (isnan(temperature) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the readings to the Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}
Problem: The sensor is not providing any data.
Problem: The readings are inconsistent or incorrect.
Problem: The Arduino Serial Monitor shows "Failed to read from DHT sensor!"
Q: Can the DHT22 be used outdoors?
A: Yes, but it should be placed in a protective enclosure to shield it from direct sunlight, rain, and condensation.
Q: What is the difference between the DHT22 and DHT11?
A: The DHT22 offers higher accuracy, a wider temperature and humidity range, and better resolution compared to the DHT11.
Q: Can I use the DHT22 with a 3.3V microcontroller?
A: Yes, the DHT22 operates within a voltage range of 3.3V to 6V, making it compatible with 3.3V systems.
Q: How long is the maximum cable length for the DHT22?
A: The maximum cable length depends on the pull-up resistor value and the environment, but typically it should not exceed 20 meters. Use shielded cables for longer distances.