• Pooja Velavan

Measuring Temperature & Humidity using DHT11 Sensor and Arduino Uno

Relative Humidity and temperature are very important for a comfortable living environment. Both the parameters change with the climatic condition. To predict the climatic condition many costly instruments are used by the meteorological centers to measure the temperature and humidity. The instruments used in the meteorological centers are very costly to afford.


In this article, let us learn to implement a simple project to measure the temperature and humidity of the room by using a low-cost DHT11 sensor interfacing with an Arduino board.

Let us first start with the requirements for the implementation of the project.


Hardware Requirements:


1. Breadboard


Breadboard
Breadboard

A breadboard is an essential component that is in a rectangular shape with small holes in it that are used to insert the components to test the circuits. The connections are not fixed and can be varied. In a breadboard, the holes in one horizontal line are in series connection with each other. Breadboards are made from plastic or wood and come in all shapes, sizes, and even different colors, the most commonly used sizes for designing the circuits are "full-size," "half-size," and "mini" breadboards.


Buy Breadboard from here.


2. Arduino UNO


Arduino UNO
Arduino UNO

#Arduino board is a microcontroller that is used to accept inputs from sensors connected and provide an output action on the desired device connected to it. The sensor inputs can be from light-detecting sensors, motion sensors (Ultrasonic or IR), temperature sensors, etc. The output from this device can be received through other output devices such as LED, Buzzer, Serial monitor, etc.


Buy Arduino UNO from here.


3. DHT11 Sensor Module


DHT11 Sensor Module
DHT11 Sensor Module

The DHT11 #sensor is a low-cost digital sensor used to measure temperature and humidity levels. The sensor contains a capacitive humidity sensor for detecting the humidity level. For measuring the temperature this sensor employs a thermistor that has a negative temperature coefficient. It is small-sized and has an operating voltage from 3 to 5 volts.


The sensor has four pins namely - supply pin (Vcc), Output pin (Data), No connection (NC), and Ground pin (Gnd). The DHT11 has a module consists of three pins. They are the supply pin (Vcc), the output pin (Data), and the Ground pin (Gnd).


Buy DHT11 Sensor Module from here


4. Jumper Wires


Jumper Wires
Jumper Wires

These are the main components that are used to establish the connections between different devices of the circuit.


Buy Jumper Wires from here.


5. USB Cable


USB Cable
USB Cable

This cable is used to connect the Arduino board to the power supply. It also helps in the transmission of data.


Buy USB Cable from here.


Software Requirements:

1. Arduino IDE


Arduino IDE LOGO
Arduino IDE LOGO

Arduino #IDE (Integrated Development Platform) which is used to interface the boards like NodeMCU to run the desired code designed to #interface hardware devices.


Circuit Connections:


Circuit Connection
Circuit Connection

The connection between the Arduino board and the DHT11 sensor by using jumper wires involves simple steps:

  • The sensor has four pins in which only three pins are used. They are the supply pin (Vcc), the output pin (Data), and the Ground pin (Gnd). Insert the sensor in the holes of the breadboard

  • The Supply pin (Vcc) is connected to the 5V of the Arduino board using a Red jumper wire.

  • The output pin (Data) is connected to the digital pin 2 of the Arduino board using a blue jumper wire.

  • Finally, the ground pin (GND) is connected to the ground terminal of the Arduino board.

Coding:

Thus in this project, there are two codings part to measure the Temperature and Humidity separately. The codes are uploaded to the board separately for the specific output to be displayed.


To measure Temperature:

First to measure Temperature enter the following code in the Arduino IDE and uploaded it to the Arduino board.

#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);

Include the DHT header file from the libraries and install it to display the data from the sensor. Initialize the output terminal of the sensor to which it is connected to the Arduino board to fetch the data and also the type of sensor used for the project.

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

In the setup function, the baud rate is set to 9600 bits per second and the sensor is ready to fetch the signal.

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

In the loop function, it takes 2 milliseconds to update the temperature.

 Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

The sensor reading of the temperature in Celsius is stored in "t" and if the temperature is to be displayed in Fahrenheit scale the value is stored in "f".

  // Check if any reads failed and exit early (to try again).
  if (isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

If the sensor is failed to read the values in integer from the sensor then it prints "Failed to read from the DHT sensor!" and returns the statements.

 Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
}

If the #Temperature is detected then the output is printed in the serial monitor in both celsius and Fahrenheit scale.


Complete Code to Measure Temperature:


#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);

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

void loop() {
  // Wait a few seconds between measurements.
  delay(2000);

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
}

For the complete code to measure temperature, download the following document.

Temperature
.txt
TXT • 795B

Note: Please change the file extension to .ino(Arduino IDE) file format before you upload the code to the Arduino board.



To measure Humidity:

To measure the Humidity of the room enter the following code in the Arduino IDE and uploaded it to the Arduino board.

#include "DHT.h"
#define DHTPIN 2
#define DHTTYPE DHT11 
DHT dht(DHTPIN, DHTTYPE);

Include the DHT header file from the libraries and install it to display the data from the sensor. Initialize the output terminal of the sensor to which it is connected to the Arduino board to fetch the data and also the type of sensor used for the project.

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

In the setup function, the baud rate is set to 9600 bits per second and the sensor is ready to fetch the signal.

void loop() {
  delay(2000);
  float h = dht.readHumidity();
  if (isnan(h) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

The sensor reads the #humidity with a delay of 2 milliseconds and stores the value in "h". If the sensor is failed to read the integer value then it is printed as "Failed to read from the DHT sensor!" and the loop is closed.

// Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

The above lines are the extra part that includes the Heat index value in both the Celsius and Fahrenheit scale with the humidity value. The heat index on the celsius scale is stored in "hic" and in Fahrenheit scale is stored in "hif".

Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));
}

If the sensor detects the humidity and #heat index values it is printed in the serial monitor by Serial.print function.


Complete Code to Measure Humidity:


#include "DHT.h"
#define DHTPIN 2     
#define DHTTYPE DHT11  
DHT dht(DHTPIN, DHTTYPE);

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

void loop() {
  delay(2000);
  float h = dht.readHumidity();
  if (isnan(h) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));
}

For the complete code to measure Humidity, download the following document.

Humidity
.txt
TXT • 678B

Note: Please change the file extension to .ino(Arduino IDE) file format before you upload the code to the Arduino board.


Working:

The Working of the #DHT11 Sensor to measure the temperature and humidity in the room involves simple steps. The below image shows the picture of the circuit when the code is not applied.

Circuit before uploading code
Circuit before uploading code

After uploading the code based on the parameter to be measured, the sensor is ready to fetch the data which is indicated with the glowing of the #LED light.

Circuit after uploading code
Circuit after uploading code

If the coding to measure temperature is uploaded to the board it displays the temperature of the room in both Celsius and Fahrenheit scale. When the coding for Humidity is uploaded it displays the real-time Humidity of the room. For further understanding, Video explanations with the practical implementation, are given in these two videos to measure temperature and humidity using the DHT11 sensor.


Video By - Abhishek Goud


Take the initial step to design the circuit, learn to interface the DHT11 sensor with Arduino, and develop your own coding with creative combinations. With this basic tutorial, we can also develop several advanced projects in numerous applications include #IoT weather stations, condition monitoring, etc.

See also:

  1. Interfacing Soil/Moisture Sensor with Arduino

  2. Interfacing Stepper Motor with Arduino

  3. Interfacing a 7-Segment Display with Arduino