Introduction
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.
What you will need - Hardware
For this tutorial you will need:
|
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.
|
|
Open the serial monitor from tools menu of Arduino IDE.
The result:
The result:
Well done!
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.
I hope you liked this, let me know in the comments.