Arduino: Read Temperature From DHT11 Module

Introduction

This is our first real implementation of Arduino using a DHT11 (Temperature) module, where we are reading the temperature and humidity data from the environment and displaying it to the Serial Monitor of your Arduino.



Online Market

You can buy a DHT11 from any online shopping website like eBay, Amazon or Flipkart.

Prerequisites

  • Arduino UNO
  • DHT 11
  • 1k Ohm Resistor
  • Jumper Wires

Library File

Download the library file from adafruit's GitHub and save it to the Arduino directory of your computer. Most of the time it is in My Documents.

Downloading Link: DHT-sensor-library

How to Add Library File in Your Arduiono IDE

Step 1

Download the DHT library file.

Step 2

Extract it to the Arduino's Library Folder. By default, your Arduino should be in My Documents of your computer. And, there you will find a folder called libraries. (Like mine is, C:\Users\gr33n\Documents\Arduino\libraries)



Step 3

Restart your Arduino IDE and then navigate to Sketch > Import Library.

Here you will get something like this:



Click on DHT and you will successfully import the DHT library into your project.

Connection Layout



First, mark your DHT to indicate which PIN belongs to what. The first PIN will always be GND (ground), then the second will be NAN (No Connection), the third will be the Digital PI that intacts with Arduino and the last PIN will be the Vcc (5 volt) power supply.

Rest, 1 Ohm resistor will be there to balance the potential difference.



Sketch

  1. #include < DHT.h >  
  2. #define PIN 12#define TYPE DHT11  
  3. // Prototype Declartion  
  4. DHT dht(PIN, TYPE);  
  5. void setup() {  
  6.     Serial.begin(9600);  
  7.     //Start  
  8.     dht.begin();  
  9. }  
  10. void loop() {  
  11.     // Maintain a difference of 2 seconds  
  12.     delay(2000);  
  13.     float humidity = dht.readHumidity();  
  14.     float temp_cel = dht.readTemperature(); // in Clecius  
  15.     float temp_farh = dht.readTemperature(true);  
  16.     // Id Failed to Read Data  
  17.     if (isnan(humidity) || isnan(temp_cel) || isnan(temp_farh)) {  
  18.         //isnan = is NOT A NUMBER which return true when it is not a number  
  19.         Serial.println("# Sorry, Failed to Read Data From DHT Module");  
  20.         return;  
  21.     } else {  
  22.         // For Humidity  
  23.         Serial.print("Humidity : ");  
  24.         Serial.println(humidity);  
  25.         // For Temprature   
  26.         Serial.print("Temparture : ");  
  27.         Serial.print(temp_cel);  
  28.         Serial.print(" C - ");  
  29.         Serial.print(temp_farh);  
  30.         Serial.println(" F ");  
  31.     }  
  32. }  
Explanation

After successfully importing the DHT.h library into your project. In the first few lines, we declared the type of DHT and connected the digital PIN of DHT using #define.

Since I am using DHT11, my DHT TYPE is DHT11. Next, we tried to define that dht() method where we are ing Digital PIN (in other words 12) and DHT TYPE as arguments.

Then, inside setup() we are setting Serial.begin() for monitoring and then calling dht.begin() to start the DHT module. Yes, after initiating the DHT sensor, now we will determine the temperature and humidity of the environment.

So, inside loop() we have the first line as delay(2000) to create a 2 second delay between readings.  And then we define three float variables, one is for humidity and the other two are for temperature in Celsius & Fahrenheit.

Next, we have an if() condition that checks whether or not the DHT sensor returned a value. If not, then it will return null and continue the loop, else it will go to display data in the Serial Monitor.

Here, we have a new function, isnan(), that returns true when there is not a number in its argument. So, we are checking whether DHT is responding or not.

In the else block, we are just printing to the Serial Monitor. On execution, we have something like the following.



And, a data error exists if we compare it to Google Weather.



Conclusion

In this article, we tried to implement a DHT11 module to read the environment temperature and its humidity. For more accurate data you can buy a DHT22. Next time, we will try to improvise this sketch and put the data in a LCD display, so it can be an Internet of Things even better.

Up Next
    Ebook Download
    View all
    Learn
    View all