Arduino Projects

DHT21 AM2301 Temperature & Humidity Sensor with Arduino

DHT21 AM2301 Sensor with Arduino:

DHT21 AM2301 Temperature & Humidity Sensor with Arduino- Whenever we talk about Temperature and Humidity Monitoring, the very first sensor that pops up in everyone’s mind is the DHT11 Temperature and Humidity Sensor. Despite of the fact that it’s extremely slow and it has a low temperature and humidity detection’s still one of the most commonly used sensors throughout the world. The main reason behind its popularity is that; because everyone including me is only using the DHT11 Temperature and Humidity sensor for testing IoT platforms, displays, and HMI screens, etc.

Why only DHT11 Sensor? We can also try other sensors in the same series like for example DHT21 and DHT22which can also be used for measuring the temperature and Humidity. DHT11, DHT21, and DHT22 all these three sensors have got different specs and features while their use is exactly the same.

So, in today’s article, you will learn how to make a simple temperature and humidity monitoring system using DHT21 or AM2301 Sensor, Arduino Nano, SSD1306 Oled Display Module, and a 5v Buzzer. Since this is a getting started tutorial, I will try my level best to explain its technical specs, features, it’s interfacing with Arduino, and programming. So, without any further delay let’s get started!!!

Amazon Links:

Arduino Nano

SSD1306 Oled display Module

DHT21/AM2301 Sensor

Other Tools and Components:

Top Arduino Sensors:

Super Starter kit for Beginners

Digital Oscilloscopes

Variable Supply

Digital Multimeter

Soldering iron kits

PCB small portable drill machines

*Please Note: These are affiliate links. I may make a commission if you buy the components through these links. I would appreciate your support in this way!

DHT21or AM2301:

DHT21 AM2301

DHT21 Temperature and Humidity sensor is also known as AM2301. DHT21 is a digital temperature and humidity sensor module made using a capacitive humidity sensor, a high precision temperature sensor, and an 8-bit microcontroller. This module provides extremely accurate outputs as it is digitally calibrated using the coefficients stored in the microcontroller. It is able to measure temperature between -40 and 80 degrees Celsius with ±1 degree of error margin. Likewise, it is able to measure humidity between 0% RH to 100% RH with ±5% RH of error margin. DHT21 has a built-in pull-up resistor while in the DHT11 and DHT22 we have to externally connect pull-up resistors.

DHT21 AM2301

DHT21 is quite larger than the DHT11 and its also slightly expensive than the DHT11 sensor. The slightly higher price is not the downside as you are paying for the better specs. DHT21 outshines the DHT11 in every aspect from temperature range, temperature accuracy, humidity range, and humidity accuracy.

DHT11 Vs DHT21 Vs DHT22:


  • 3 to 5V power and I/O
  • 5mA max current use during conversion (while requesting data)
  • Good for 20-80% humidity readings with 5% accuracy
  • Good for 0-50°C temperature readings ±2°C accuracy
  • No more than 1 Hz sampling rate (once every second)
  • Body size 15.5mm x 12mm x 5.5mm
  • 4 pins with 0.1″ spacing
  • from $0.70 USD without PCB/pull-up
  • from $0.80 USD with PCB/pull-up resistor

DHT22 (AM3202):

  • 3 to 5V power and I/O
  • 5mA max current use during conversion (while requesting data)
  • Good for 0-100% humidity readings with 2-5% accuracy
  • Good for -40 to 80°C temperature readings ±0.5°C accuracy
  • No more than 0.5 Hz sampling rate (once every 2 seconds)
  • Body size 15.1mm x 25mm x 7.7mm
  • 4 pins with 0.1″ spacing
  • from $2.35 USD without PCB/pull-up
  • from $2.55 USD with PCB/pull-up resistor

DHT21 (AM2301):

  • Humidity sensing range: 0 to 99.9% RH.
  • Humidity measurement accuracy: ±3% RH.
  • Temperature measurement range: -40 to 80 °C.
  • Temperature measurement accuracy: ±0.5 °C.
  • Supply voltage: 3.3 to 5.2 V.
  • from $2.40 USD with PCB/pull-up resistor

DHT21 accepts a wide range of input voltages from 3.3V to 5.2V. Due to such a wide range of input voltages, it can be used with 3.3V and 5V compatible controller boards like ESP32, ESP8266, Arduino boards, Raspberry Pi, STM32, and so on.

  • The Red wire is the VCC and it should be connected with 3.3V or 5V.
  • The Yellow wire is the output wire, which is the Data wire. It outputs a digital signal via Single-bus.
  • The Black wire is the GND wire.


  • Ultra low power
  • Fast response
  • Relative humidity and temperature measurement
  • Full range temperature compensation
  • Excellent long term stability
  • Long transmission distance
  • High accuracy
  • No need of extra components
  • Excellent long term stability
  • Calibrated digital output

Pin Out

  • Red VDD Power (3.3V ~ 5.2V)
  • Yellow SDA Serial Data, Dual Port
  • Black GND Ground


  • HVAC (Heating, Ventilating and Air Conditioning)
  • Dehumidifier
  • Testing and inspection equipment
  • Home appliances
  • Data loggers
  • Humidity regulator
  • Weather stations

Altium Designer + Altium 365 + Octopart:

Arduino LoRa Free SMS

Altium 365 lets you hold the fastest design reviews ever. Share your designs from anywhere and with anyone with a single click. it’s easy, leave a comment tagging your teammate and they’ll instantly receive an email with a link to the design. Anyone you invite can open the design using a web browser. Using the browser interface, you’re able to comment, markup, cross probe, inspect, and more. Comments are attached directly to the project, making them viewable within Altium designer as well as through the browser interface. Design, share, and manufacture, all in the same space with nothing extra to install or configure. Connect to the platform directly from Altium Designer without changing how you already design electronics. Altium 365 requires no additional licenses and comes included with your subscription plan.

Get real-time component insights as you design with Octopart built into Altium 365. Octopart is the fastest search engine for electronic parts and gives you the most up-to-date part data like specs, datasheets, cad models, and how much the part costs at different amounts etc. Right in the design environment so you can focus on your designs. Start with Altium Designer and Activate Altium 365. Search for electronic parts on Octopart.

DHT21/AM2301 Interfacing with Arduino:

DHT21 AM2301

The VCC and GND wires of the DH21 or AM2301 Temperature and Humidity sensor are connected with the 5V and GND. While the Data wire is connected with the Arduino pin D2.

The RED wire of the buzzer is connected with the 5v. The ground wire of the 5V buzzer is connected with the Collector of the 2n2222 NPN transistor. The Emitter of the transistor is connected with the ground. While the base of the transistor is connected with the Arduino D4 pin through a 10km resistor.

The SSD1306 Oled display module SDA and SCL/SCK pins are connected with the A4 and A5 pins while the VDD/VCC and GND pins are connected with the Arduino’s 3.3V and GND pins.

On the left side is the 5v regulated power supply based on the LM7805 Linear voltage regulator.

Required Libraries:

As usual before you start the programming, first of all, make sure you download all the necessaries libraries.




To install the DHT library, simply click on the Sketch menu, go to include library, and then click on the manage libraries. In the search box type DHT.

DHT21 AM2301

Select the desired DHT library and install it. As you can see I have already installed this library.

DHT21/AM2301 Arduino Programming:

#include <DHT.h>;
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define DHTPIN 2            //what pin we're connected to
#define DHTTYPE DHT21       //DHT 21  (AM2301)
DHT dht(DHTPIN, DHTTYPE);   //Initialize DHT sensor for normal 16mhz Arduino

int buzzer=4;
float hum;  //Stores humidity value
float temp; //Stores temperature value

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)

void setup()
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

void loop()
  //Read data and store it to variables hum and temp
  hum = dht.readHumidity();
  temp= dht.readTemperature();
  //Print temp and humidity values to serial monitor
  Serial.print("Humidity: ");
  Serial.print("%,  Temperature: ");
  Serial.println(" Celsius");
  display.setCursor(0, 10);
  display.setCursor(0, 30);


  delay(1000); //Delay 2 sec.


This is the same exact program that I wrote for the DHT11 sensor. The only change which I made is; I replaced the DHT11 with DHT21. So, any program which is written for the DHT11 will also work with the DHT21 Temperature and Humidity Sensor.

Inside the loop() function, we simply read the Humidity and Temperature. And then display these values on the Oled display Module. Besides this, I have also added if condition which checks if the temperature has increased above the predefined value. If the temperature has increased then the buzzer will turn ON else the buzzer will remain OFF. So, that’s all about the programming.  I have already uploaded this program and now let’s watch the DHT21 or AM2301 Temperature and Humidity Sensor in Action.

DHT21 AM2301

Initially, the temperature was 30.40 Degrees Celsius. After I applied some heat, the temperature and humidity started to change as you can see in the image below.

DHT21 AM2301

Watch Video Tutorial:

Engr Fahad

My name is Shahzada Fahad and I am an Electrical Engineer. I have been doing Job in UAE as a site engineer in an Electrical Construction Company. Currently, I am running my own YouTube channel "Electronic Clinic", and managing this Website. My Hobbies are * Watching Movies * Music * Martial Arts * Photography * Travelling * Make Sketches and so on...

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button