Ardumotive Arduino Greek Playground
  • Home
    • About us
    • Contact
    • License
  • Arduino
    • Tutorials
    • Workshop
  • Raspberry Pi
  • DIY 3D Printer
  • News
    • Events >
      • Unboxing & Review
    • Blog
The 1st Arduino Playground in Greece - Open Source Hardware

Humidity and Temperature

Available Languages
Picture
Picture

Introduction 

Picture
The DHT-21 (also named as AM2301) is a digital-output relative humidity and temperature sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin.
In this tutorial you will learn how to use this sensor with Arduino uno. The room temperature & humidity will be printed to serial monitor.

About the  DHT-21 sensor

The DHT21 is a basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air, and spits out a digital signal on the data pin (no analog input pins needed). Its fairly simple to use, but requires careful timing to grab data. The only real downside of this sensor is you can only get new data from it once every 2 seconds, so when using our library, sensor readings can be up to 2 seconds old.
​

​Technical details:
  • Power: 3.3 - 5.2V
  • Humidity: 0-99.9%,  3%RH accuracy
  • Temperature: -40 to 80°C, ±0.5°C accuracy
Picture

What you will need - Hardware

For this tutorial you will need:
  • Arduino uno
  • Breadboard (optional)
  • DHT-21 (AM2301)
Picture

The Circuit

The connections are pretty easy:
  • Red cable to Arduino 3.3V or 5V pin 
  • Black cable to Arduino GND pin 
  • Yellow cable to Arduino 2 digital pin

The code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
/* How to use the DHT-21 sensor with Arduino uno
   Temperature and humidity sensor
   More info: http://www.ardumotive.com/how-to-use-dht-21-sensor-en.html
   Dev: Michalis Vasilakis // Date: 19/11/2016 // www.ardumotive.com */

//Libraries
#include <dht.h>
dht DHT;
//Constants
#define DHT21_PIN 2     // DHT 22  (AM2302) - what pin we're connected to

//Variables
float hum;  //Stores humidity value
float temp; //Stores temperature value

void setup()
{
    Serial.begin(9600);
}

void loop()
{
    int chk = DHT.read21(DHT21_PIN);
    //Read data and store it to variables hum and temp
    hum = DHT.humidity;
    temp= DHT.temperature;
    //Print temp and humidity values to serial monitor
    Serial.print("Humidity: ");
    Serial.print(hum);
    Serial.print(" %, Temp: ");
    Serial.print(temp);
    Serial.println(" Celsius");
    delay(2000); //Delay 2 sec.
}
Download the code from here and open it with Arduino IDE. Inside you will also find and the DHT.h library.
dht21_tutorial.zip
File Size: 11 kb
File Type: zip
Download File

Open the serial monitor from tools menu of Arduino IDE.

​The result:
Picture

Well done!

Picture
You have successfully completed one more Arduino "How to" tutorial and you learned how to use the DHT-21 sensor.
I hope you liked this, let me know in the comments.

Picture

Search Engine

Picture

Licence 

Picture

Help us to grow up!

Picture


Donate us
About us
License
Cookies policy

Visit the biggest Arduino Shop in Greece!

Picture
find us on dwrean.net
find us on Codebender
find us on Instructables
Developed and designed by Vasilakis Michalis Copyright © 2013 Ardumotive All Rights Reserved
All trademarks referenced herein are properties of their
Powered by Create your own unique website with customizable templates.
Design by DivTag Templates