DHT11 Humidity and Temperature Sensor with Arduino

In many situations and for many application, in home or industrial factories, scientific and engineering laboratories, environmental humidity and temperature can affect the working condition there. It can be important to keep the working area within a certain range of humidity and temperature because for example the various types of equipment, plants etc might get affected. 

DHT11 which is a humidity and temperature sensor along with Arduino can be used keep the surrounding within a desired range or buzz alarm. For example, after reading in humidity and/or temperature from the DHT11 sensor, you can use speed control of DC motor with PWM using Arduino to control the environment or use heater/cooler(AC) etc.

Here we show simple example of how to use DHT11 humidity and temperature with Arduino. First we explain DHT11 sensor and demonstrate with video what we will be doing here so that you know what to expect. Then we will explain the wiring between DHT11 and Arduino, afterwards show how to install DHT library in order to write program and finally provide the programming code and explain the code.

Components & Hardware Required

1. Arduino
2. DHT11 sensor
3. Some Wires
4. USB and Power cable for Arduino


DHT11 Sensor
DHT11 Sensor

DHT11 is a sensor that can measure temperature and relative humidity. It can measure relative humidity in the range of 20% to 90% with +/- 5% accuracy. It can measures temperature in the range 0 to 50 degree Celsius with +/- 2 degree accuracy. It can work with 3V to 5.5V supply voltage. It has a sampling time of 1 second. We can request humidity and temperature data every second from the sensor.

Video demonstration and Working Mechanism

In the video below, you can see how the DHT11 sensor works with Arduino. You will see that the relative humidity and temperature gets displayed on the Arduino IDE serial monitor. Also you will see that after applying some heat near the sensor, the temperature will be elevated by 1 degree Celsius and temperature gets updated in the serial monitor display.



DHT11 Sensor & Arduino Schematic Diagram

The schematic diagram below shows interfacing of DHT11 sensor and Arduino. Wiring between DHT11 and Arduino is quite easy. The DHT11 sensor just got three pins(Vcc, Data and Ground) which can be connected directly to Arduino. In the DHT11 sensor circuit diagram, the Vcc pin and ground of DHT11 are connected to the  +5V pin and ground pin of the Arduino respectively. The middle data or sensor pin of DHT11 is connected to the digital pin 7 of the Arduino.


Installing DHT library for Arduino

Here we will be using DHT library for Arduino. Library makes coding easy so we will be utilizing it! First open Arduino IDE. In the IDE, go to the Tools tab then Manage Libraries as shown.

In the library manager window, use the search tool to find dht and install the DHT sensor library by Adafruit. This is as shown.



Finally restart your Arduino IDE.

DHT11 Temperature and Humidity Sensor Arduino Code

Here we write a program to read the humidity and temperature values from the DHT11 sensor using Arduino and send it serially to the serial monitor. Below pictures shows how the humidity and temperature are displayed in the serial monitor.


Arduino code for DHT11

// Arduino sketch for DHT11 humidity/temperature sensor
// By ee-diary, https://ee-diary.com

#include "DHT.h"    //include the DHT library

#define DHTTYPE DHT11 // define dhttype as dht11
#define DHTPIN 7     // Name dhtPin as pin 7

DHT dht(DHTPIN, DHTTYPE);   // Initialize DHT sensor

void setup() {
  Serial.begin(9600); 
  Serial.println("DH11 Humidity/Temperature:");
  Serial.println("--------------------------");
 
  dht.begin();
}

void loop() {
  
  delay(2000);  // Wait two seconds between measurements

  float H = dht.readHumidity();     //Read Humidity
  float T = dht.readTemperature();    // Read temperature as Celsius
  
  // Check if any reads failed and if exit
  if (isnan(H) || isnan(T)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Humidity: "); 
  Serial.print(H);
  Serial.print("%,\t");
  Serial.print("Temperature: "); 
  Serial.print(T);
  Serial.print("*C ");
  Serial.print("(");
  Serial.print(F);
  Serial.println("*F)\t");
}

In order to use the DHT library, we first include the DHT.h header file using the #include "DHT.h" statement. Then we will define the DHT type as DHT11. This is because the DHT library also supports DHT22 and DHT21 sensors. If you use any of these you should replace DHT11 in that statement. After this we will define the pin 7 as our DHTPIN for code readability and ease of future use code. Then we have to initialize the DHT library to use the various functions within the library using the DHT dht(DHTPIN, DHTTYPE); statement. In this the first argument to the dht() function is the pin where the sensor pin is connected and the type of the DHT sensor(either DHT11, DHT22 or DHT21). 

In the setup() function, we initialize the serial monitor using the serial.begin() function with the desired baud rate as the input parameter which in our case is 9600. We then simply put a string "DH11 Humidity/Temperature:" followed by "--------------" string. 

In the loop() function, dht object with it's readHumidity function in the "." notation dht.readHumidity() to read the humidity value from the sensor and store it in the float variable called H. Similarly using the dot notation we read the temperature sensor value in celcius and farenheit unit from the sensor using the dht.readTemperature() statement and store those value in float variable scalled T and F. Next we check if any data reading from the DHT11 sensor occurs using the  if (isnan(H) || isnan(T) || isnan(F))  statement. If error occurs we print out the string "Failed to read from DHT sensor!" using the Serial.println() function. If there is no error, the next statements are executed where we just send the values recorded in the float variables H, T and F to the serial monitor using the Serialprint() and Serialprintln() functions.

2 Comments

Previous Post Next Post