Skip to main content

Temperature and Humidity Measurement with DHT11 and Arduino Uno

DHT11 is a low-cost digital output basic temperature and humidity sensor. The DHT11 detects humidity by measuring the resistance between two electrodes. The humidity sensing component resistance changes with the water vapor present in the surrounding air. The DHT11 measures temperature with an NTC temperature sensor.

The DHT11 sends data on a single line in digital format, so there is no need for ADC. The signal line needs to be pulled up by 10Kohm resistor to keep the signal line high by default.

Here are the ranges of DHT11
  •  Humidity Range: 20-90% RH
  • Humidity Accuracy: ±5% RH
  • Temperature Range: 0-50 °C
  • Temperature Accuracy: ±2 °C
  • Operating Voltage: 3V to 5.5V

Required Hardware:
  • Arduino UNO
  • DHT11 or DHT22


How to connect DHT11 with Arduino Uno:
Here this schematic shows how to connect DHT11 with Arduino, if you are using DHT11 module then you can directly connect the signal pin to the Arduino, a pull-up resistor is already present on the module.

Reading Temperature & Humidity:
To use the DHT11 sensor with Arduino we first, need to add DHT library. Download the library from here and add it to the Arduino by going to Sketch à Include Library à Add.ZIP Library

After installing the library into Arduino IDE, use this program to display temperature and humidity on serial monitor.
/* How to use the DHT-11 sensor with Arduino uno
   Temperature and humidity sensor
*/
//Libraries
#include <DHT.h>;
//Constants
#define DHTPIN 4     // what pin we're connected to
#define DHTTYPE DHT11 // comment this line to use DHT22
// #define DHTTYPE DHT22 // Uncomment this line to use DHT22
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino

//Variables
float hum;  //Stores humidity value
float temp; //Stores temperature in Celsius
float farh; //Stores temperature in Fahrenheit 
float heatIndex1; //Stores heat Index in Fahrenheit
float heatIndex2; //Stores heat Index in Celsius

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

void loop()
{
    //Read data and store it to variables hum and temp
    hum = dht.readHumidity();
    temp = dht.readTemperature();
    farh = dht.readTemperature(true);
    heatIndex1 = dht.computeHeatIndex(farh, hum);
    heatIndex2 = dht.computeHeatIndex(temp, hum, false);
    //Print temp and humidity values to serial monitor
    Serial.print("Humidity: ");
    Serial.print(hum);
    Serial.println(" %");
    Serial.print("Temperature: ");
    Serial.print(temp);
    Serial.print(" °C\t");
    Serial.print(farh);
    Serial.println(" °F");
    Serial.print("Heat Index: ");
    Serial.print(heatIndex2);
    Serial.print(" °C\t");
    Serial.print(heatIndex1);
    Serial.println(" °F");
    delay(2000); //Delay 2 sec.
}


Upload this code to Arduino and open serial monitor and set the baud rate to 9600. You can see the humidity and temperature every 2 seconds. You can use the same code to use the DHT22 sensor. Just Comment #define DHTTYPE DHT11 and Uncomment #define DHTTYPE DHT22.

How the code works?
First, we need to include the DHT library. You can install the library from Tools à Manage Libraries and searching for DHT. Install the library from Adafruit. You can also get the library from here.

#include <DHT.h>;

Now we define the pin to which the sensor is attached to
#define DHTPIN 4     // what pin we're connected to

Next, we define which type of DHT sensor we are using. In this case, we are using DHT11 sensor. In order to use DHT22, replace DHT11 with DHT22
#define DHTTYPE DHT11 // comment this line to use DHT22
// #define DHTTYPE DHT22 // Uncomment this line to use DHT22

Next we initialize the sensor
DHT dht(DHTPIN, DHTTYPE);

Here we define some variables to store the data getting from the sensor
float hum;  //Stores humidity value
float temp; //Stores temperature in Celsius
float farh; //Stores temperature in Fahrenheit
float heatIndex1; //Stores heat Index in Fahrenheit
float heatIndex2; //Stores heat Index in Celsius

In void setup, we initialize the serial monitor and DHT11 sensor
void setup()
{
  Serial.begin(9600);
  dht.begin();
}

Next, in the void loop we define different functions to get the data continuously

hum = dht.readHumidity(); This function will take the reading of the Humidity and save it in the variable hum

temp = dht.readTemperature(); This function will take the reading of the temperature in Celsius and store the value in the variable temp. To get the temperature reading in Fahrenheit we write the function as dht.readTemperature(true);

This DHT library also gives us to calculate the heat index, the heat index is the actual heat we feel when relative humidity is factored with air temperature. To calculate this we use dht.computeHeatIndex(temp, hum, false); to get it in Celsius or dht.computeHeatIndex(farh, hum); (here the temperature should be in Fahrenheit, in our example code above we used farh as a variable to store Fahrenheit value) to get it in Fahrenheit.

Next we print this information on the serial monitor. A delay of 2 seconds is provided since the DHT11 sensor takes samples for every 2 seconds.
    Serial.print("Humidity: ");
    Serial.print(hum);
    Serial.println(" %");
    Serial.print("Temperature: ");
    Serial.print(temp);
    Serial.print(" °C\t");
    Serial.print(farh);
    Serial.println(" °F");
    Serial.print("Heat Index: ");
    Serial.print(heatIndex2);
    Serial.print(" °C\t");
    Serial.print(heatIndex1);
    Serial.println(" °F");
    delay(2000); //Delay 2 sec.

Comments

Post a Comment

Popular posts from this blog

Interfacing 16x2 Character LCD with Arduino

LCD (Liquid Crystal Display) are widely used display modules we find in a wide range of applications. A 16x2 character LCD display is mostly used in many applications since it is easily and cheaply available. It can display 16 characters per line and we have 2 such lines.  Pin Diagram: VCC is connected to supply voltage of 5V (4.7V to 5.3V) Contrast / VEE is used to adjust the contrast of the display. A variable resistor can be used to adjust the contrast of the display by connecting the wiper pin of the variable resistor to this pin and fixed pins to the supply voltage and Ground. RS (Register Select) A 16x2 LCD has two registers, a command register, and a data register. When it is pulled LOW command register is selected and when it is HIGH data register is selected. The command register will have all the instructions given to the LCD like clearing the screen, position of the cursor, initializing the LCD etc. The data register stores all the data we want t...