

The DHT21 is a digital temperature and humidity sensor that provides accurate readings of environmental conditions. It is designed to measure relative humidity and temperature with high precision and stability. The sensor communicates via a single-wire interface, making it easy to integrate into microcontroller-based systems. Its compact design and reliable performance make it a popular choice for a variety of applications.








The DHT21 sensor is designed to deliver reliable and accurate measurements. Below are its key technical details:
| Parameter | Value |
|---|---|
| Supply Voltage | 3.3V to 5.5V |
| Operating Current | 0.3 mA (measuring), 60 µA (standby) |
| Humidity Range | 0% to 100% RH |
| Humidity Accuracy | ±2% RH |
| Temperature Range | -40°C to 80°C |
| Temperature Accuracy | ±0.5°C |
| Communication Interface | Single-wire (digital) |
| Sampling Period | 2 seconds |
| Dimensions | 27mm x 58mm x 13mm |
The DHT21 has four pins, as described in the table below:
| Pin Number | Name | Description |
|---|---|---|
| 1 | VCC | Power supply (3.3V to 5.5V) |
| 2 | DATA | Digital data output (single-wire communication) |
| 3 | NC | Not connected (leave unconnected) |
| 4 | GND | Ground |
To use the DHT21 sensor in a circuit, follow these steps:
Below is an example of how to use the DHT21 sensor with an Arduino UNO. This code uses the popular DHT library, which simplifies communication with the sensor.
#include <DHT.h>
// Define the pin connected to the DHT21 DATA pin
#define DHTPIN 2
// Define the DHT sensor type (DHT21)
#define DHTTYPE DHT21
// Initialize the DHT sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600); // Start serial communication
Serial.println("DHT21 Sensor Initialization");
dht.begin(); // Initialize the DHT sensor
}
void loop() {
delay(2000); // Wait 2 seconds between readings
// Read temperature and humidity
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if the readings are valid
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Print the results to the Serial Monitor
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
}
DHT library is installed in your Arduino IDE. You can install it via the Library Manager (Sketch > Include Library > Manage Libraries).DHTPIN constant specifies the digital pin connected to the sensor's DATA pin. Adjust it as needed for your setup.No Data or Incorrect Readings
"Failed to read from DHT sensor!" Error
DHTPIN).DHT library is properly installed and included in the code.Inconsistent or Fluctuating Readings
By following this documentation, you should be able to successfully integrate and use the DHT21 sensor in your projects.