DHT21 / AM2301A Temperature and Humidity Sensor

View on Amazon
Overview
About DHT21 / AM2301A Temperature and Humidity Sensor
The DHT21, also known as AM2301, is a digital temperature and humidity sensor that provides accurate environmental measurements. It uses a capacitive humidity sensor and a thermistor to ensure reliable data collection.
⚡ Key Features
- High Accuracy – Delivers precise temperature and humidity readings.
- Single-Wire Digital Communication – Easy integration with ESP32, Arduino, and other microcontrollers.
- Capacitive Humidity Sensing – Provides stable and long-term performance.
- Similar to DHT22 – Slight differences in casing and specifications.
With its ease of use and reliable performance, the DHT21 is ideal for climate monitoring, smart home systems, and industrial automation. 🚀
Get Your DHT21 / AM2301A
💡 Prices are subject to change. We earn from qualifying purchases as an Amazon Associate.
DHT21 / AM2301A Specifications
Complete technical specification details for DHT21 / AM2301A Temperature and Humidity Sensor
📊 Technical Parameters
DHT21 / AM2301A Pinout
The DHT21/AM2301A features the same 4-pin layout as DHT22, with nearly identical specifications and performance.
Visual Pinout Diagram

Pin Types
Quick Tips
Uses same single-wire protocol as DHT22,⚠️ Requires 5kΩ to 10kΩ pull-up resistor on DATA pin
Nearly identical to DHT22 in performance,⏱️ Read interval of 2 seconds minimum
Wide temperature range (-40°C to 80°C)
Pin Descriptions
| Pin Name | Type | Description | Notes |
|---|---|---|---|
1 VCC | Power | Power supply input (3.3V to 6V) | Compatible with both 3.3V and 5V systems |
2 DATA | Communication | Digital signal output | Single-wire digital interface for data |
3 NC | Control | Not connected | Leave this pin unconnected |
4 GND | Power | Ground connection | Connect to ESP32 ground |
Wiring DHT21 / AM2301A to ESP32
Connect the DHT21/AM2301A using the single-wire digital interface, identical to DHT22 wiring.
Visual Wiring Diagram

Connection Status
Protocol
Pin Connections
| DHT21 / AM2301A Pin | Connection | ESP32 Pin | Description |
|---|---|---|---|
1 VCC Required | 3.3V | Power supply (3.3V to 6V supported) | |
2 GND Required | GND | Ground connection | |
3 DATA Required | GPIO4 | Digital data line | |
4 DATA (Pull-up) Required | 5kΩ to VCC | Pull-up resistor for reliable data transfer |
Any GPIO pin works - GPIO4 is commonly used
5kΩ pull-up resistor recommended (10kΩ also works)
Use 3.3V for ESP32 compatibility
Minimum 2 second delay between readings
Compatible with DHT22 libraries and code
DHT21 / AM2301A Troubleshooting
Common issues and solutions to help you get your sensor working
Common Issues
Issue: The DHT21 (AM2301A) sensor returns incorrect temperature values, such as 225°C.
Possible causes include incorrect sensor initialization or improper data handling in the code.
Solution: Ensure that the sensor is properly initialized in the code and that the correct data type is specified. Verify that the data reading and conversion logic accurately interpret the sensor's output. Refer to the sensor's datasheet for the correct data format and adjust the code accordingly.
Issue: The DHT21 sensor consistently reports a humidity value of 99.9%, regardless of the actual environmental conditions.
Possible causes include sensor malfunction or environmental factors causing sensor saturation.
Solution: Ensure that the sensor is not exposed to conditions beyond its operational limits, such as high humidity or condensation. If the issue persists, consider replacing the sensor, as it may be defective.
Issue: The sensor intermittently returns NaN values for temperature or humidity readings.
Possible causes include unstable connections, lack of a pull-up resistor on the data line, or timing issues in data communication.
Solution: Verify that all connections are secure and that a pull-up resistor (typically 5kΩ) is connected between the data line and VCC. Ensure that the code accounts for the sensor's timing requirements, including appropriate delays between readings.
Issue: Compilation errors occur when using the DHT library with the DHT21 sensor.
Possible causes include incorrect library version or improper library installation.
Solution: Ensure that the latest version of the DHT library is installed and correctly included in the project. Verify that the library supports the DHT21 sensor and that the correct sensor type is defined in the code.
Debugging Tips
Use the Serial Monitor to check for error messages and verify the sensor's output. Add debug prints in your code to track the sensor's state.
Use a multimeter to verify voltage levels and check for continuity in your connections. Ensure the power supply is stable and within the sensor's requirements.
Additional Resources
DHT21 / AM2301A Programming Examples
Ready-to-use code examples for different platforms and frameworks
#include "DHT.h"
#define DHTPIN 4 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println("DHT21 Sensor Example");
dht.begin();
}
void loop() {
// Wait a few seconds between measurements
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
float humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" % ");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
}#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"
#include "dht.h"
#define DHT_PIN GPIO_NUM_4
void app_main() {
setDHTgpio(DHT_PIN);
while (1) {
printf("Reading DHT21 sensor...\n");
int ret = readDHT();
errorHandler(ret);
printf("Humidity: %.1f%%\n", getHumidity());
printf("Temperature: %.1f°C\n", getTemperature());
vTaskDelay(2000 / portTICK_PERIOD_MS);
}
}sensor:
- platform: dht
pin: GPIO4
model: DHT21
temperature:
name: "Living Room Temperature"
humidity:
name: "Living Room Humidity"
update_interval: 60splatformio.ini
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
adafruit/DHT sensor library @ ^1.4.2
monitor_speed = 115200main.cpp
#include <DHT.h>
#define DHTPIN 4
#define DHTTYPE DHT21
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println("DHT21 Sensor Example");
dht.begin();
}
void loop() {
// Wait a few seconds between measurements
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
float humidity = dht.readHumidity();
// Read temperature as Celsius (the default)
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" % ");
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
}from machine import Pin
from time import sleep
import dht
# Initialize the DHT21 sensor
sensor = dht.DHT21(Pin(4))
print('DHT21 Sensor Example')
while True:
sensor.measure()
temperature = sensor.temperature() # Temperature in Celsius
humidity = sensor.humidity() # Relative Humidity in %
print('Temperature: {} °C'.format(temperature))
print('Humidity: {} %'.format(humidity))
sleep(2)Wrapping Up DHT21 / AM2301A
The ESP32 DHT21 / AM2301A Temperature and Humidity Sensor is a powerful environment sensor that offers excellent performance and reliability. With support for multiple development platforms including Arduino, ESP-IDF, ESPHome, PlatformIO, and MicroPython, it's a versatile choice for your IoT projects.
Best Practices
For optimal performance, ensure proper wiring and follow the recommended configuration for your chosen development platform.
Safety First
Always verify power supply requirements and pin connections before powering up your project to avoid potential damage.
Ready to Start Building?
Now that you have all the information you need, it's time to integrate the DHT21 / AM2301A into your ESP32 project and bring your ideas to life!
Explore Alternative Sensors
Looking for alternatives to the DHT21 / AM2301A? Check out these similar sensors that might fit your project needs.

SHT25 Temperature and Humidity Sensor
The SHT25 sensor is a high-accuracy digital temperature and humidity sensor that utilizes Sensirion's CMOSens® technology. It provides...

DHT11 Temperature and Humidity Sensor
The DHT11 is a low-cost digital sensor for measuring temperature and humidity. It provides calibrated digital outputs and is easy to...

DHT22 / AM2302 Temperature and Humidity Sensor
The DHT22 is a versatile and affordable sensor for measuring temperature and humidity. It provides calibrated digital output and is easy to...





