Table of Contents
DHT stands for Digital Humidity and Temperature. The DHT sensor is a low-cost digital sensor for sensing temperature and humidity. This sensor can be easily interfaced with any micro-controller such as Arduino, Raspberry Pi to measure humidity and temperature instantaneously. It can be used for a number of applications as we will see later.
In this tutorial I will show you how to use the DHT11 and the DHT22 sensor for measuring temperature and humidity with the Arduino Uno.
We have two versions of the DHT sensors they are:
1. DHT11
2. DHT22
They look a bit similar and have the same pinout, but have different characteristics. Here are the details of these two sensors:
Characteristics of DHT Sensor
The DHT22 is bit more expensive as it has better specifications like it is more precise, more accurate and works in a bigger range of temperature & humidity. Its temperature measuring range is from -40°C to +125°C with +-0.5 degrees accuracy, while the DHT11 temperature range is from 0°C to 50°C with +-2 degrees accuracy. Also the DHT22 sensor has better humidity measuring range, from 0 to 100% with 2-5% accuracy, while the DHT11 humidity range is from 20 to 80% with 5% accuracy.
There are few things where DHT11 sensor can be a better choice than DHT22 sensor.As it is less expensive, smaller in size and has higher sampling rate. The sampling rate of the DHT11 is 1Hz i.e. one reading every second, while the sampling rate of DHT22 is 0.5Hz i.e. one reading for every two seconds.
Working Principle of DHT Sensor
Let’s teardown both the DHT11 and DHT22 sensors and see what’s inside.
DHT sensor consists of a capacitive humidity sensing element and a thermistor for sensing temperature. The humidity sensing capacitor has two electrodes with a moisture holding substrate as a dielectric between them. Change in the capacitance value occurs with the change in humidity levels..
For measuring temperature this sensor uses a NTC thermistor, The term “NTC” means “Negative Temperature Coefficient”, which means that the resistance decreases with increase of the temperature as shown in the graph below. To get larger resistance value even for the smallest change in temperature, this sensor is usually made up of semiconductor ceramics or polymers.
On the other side, there is a small PCB with an 8-bit SOIC-14 packaged IC. This IC measures and processes the analog signal with stored calibration coefficients, does analog to digital conversion and gives out a digital signal with the temperature and humidity.
DHT11 and DHT22 Pinout
Both DHT11 and DHT22 Sensors have 4 Pins,they are:
- VCC pin supplies power for the sensor. Although supply voltage ranges from 3.3V to 5.5V, 5V supply is recommended.
- Data pin is used to communication between the sensor and the microcontroller.
- NC Not connected.
- Ground should be connected to the ground of the microcontroller that you are using.
DHT11 and DHT22 Sensor Description
Both the DHT11 and DHT22 Sensor consists of 3 main components. A humidity sensor, an NTC (negative temperature coefficient) thermistor and an 8-bit microcontroller, which converts the analog signals from both the sensors and sends out single digital signal.
The data from the DHT11 and DHT22 sensor consists of 40 bits and the format is as follows:
- 8 – Bit data for integral part of RH value,
- 8 – Bit data for decimal part of RH value,
- 8 – Bit data for integral part of Temperature value,
- 8 – Bit data for decimal part of Temperature value and
- 8 – Bit data for checksum.
If the data transmission is right, the check-sum should be the last 8 Bit of “8 Bit integral RH data + 8 Bit decimal RH data + 8 Bit integral T data + 8 Bit decimal T data”.
Example
Consider the data received from the DHT Sensor is
0011010100000000000110000000000001001101
This data can be separated based on the above mentioned structure as follows
In order to check whether the received data is correct or not, we need to perform a small calculation. Add all the integral and decimals values of RH and Temperature and check whether the sum is equal to the checksum value i.e. the last 8 Bit data.
This value is same as checksum and hence the received data is valid. Now to get the RH and Temperature values, just convert the binary data to decimal data.
Humidity: 0011 0101 = 35%
Temperature:0001 1000 = 24℃
If the received data is not correct then receive the data again.
You can go through the Datasheet of DHT11 and DHT22 sensor .
Interfacing DHT11 and DHT22 Sensors with Arduino Uno
Now we have complete understanding of the working principle of this sensor.Now its time to integrate the sensor with Arduino to measure the Temperature and Humidity.So, lets do it.
Connect the Sensor with Arduino as shown in figure.
We need to place a pull-up resistor of 10KΩ between VCC and data line of the sensor to keep it HIGH for proper communication between sensor and Microcontroller.
When done with the connections we are ready to upload the program on Arduino to see the readings of Temperature and Humidity on Liquid Crystal Display.
Now i will show you how to write the code for measuring the temperature and humidity with and without using DHT library and note that in both the cases the circuit connection remains same.
Interfacing DHT Sensor without using DHT library
Although there is a special library for the DHT sensor called “DHT” .In this i will show you how to write the program that measures Humidity and Temperature from DHT sensor without using DHT library.
To Understand and Write the program without using DHT library it is mandatory that you should go through the Datasheet of the sensor. As the program written is based on the data timing diagrams provided in the datasheet.
Initially MCU sends a start signal in low state, DHT11 changes from the low-power-consumption mode to the running-mode, this process must take at least 18ms to ensure DHT’s detection of MCU’s signal,then MCU will pull up voltage and wait 20-40us for DHT’s response.
Code:
pinMode(datapin,OUTPUT); //declaring datapin as the output pin digitalWrite(datapin,LOW); //Low signal from the arduino to the Sensor to start the process delay(20); //Low retention time of 20 µs digitalWrite(datapin,HIGH); //Setting datapin to high state pinMode(datapin,INPUT_PULLUP); //by default it will become high due to internal pull up
Once DHT detects the start signal, it will send out a low-voltage-level response signal, which lasts 80us. Then the programme of DHT sets Data Single-bus voltage level from low to high and keeps it for 80us for DHT’s preparation for sending data.
When DATA Single-Bus is at the low voltage level, this means that DHT is sending the response signal. Once DHT sent out the response signal, it pulls up voltage and keeps it for 80us and prepares for data transmission.
When DHT is sending data to MCU, every bit of data begins with the 50us low-voltage-level and the length of the following high-voltage-level signal determines whether data bit is “0” or “1”.i.e if the duration of high voltage level form the sensor is in between 26µs to 28µs then bit “0” is received, and if the duration of high voltage level form the sensor is 70µs then bit “1” is received as shown in the figure below..
Code:
duration=pulseIn(datapin, LOW); //duration variable starts timing when sensor sends low voltage level response if(duration <= 84 && duration >= 72) { while(1) { duration=pulseIn(datapin, HIGH); //duration variable starts timing when sensor sends High voltage level response if(duration <= 26 && duration >= 20) //if duration is in between 26µs and 20µs then bit 0 is transmitted by sensor { value=0; //Bit 0 is received } else if(duration <;= 74 && duration >= 65) //if duration is in between 74µs and 65µs then bit 1 is transmitted by sensor { value=1; //Bit 1 is received } else if(z==40) //Come out of the loop when 40 bis of data is received { break; } i[z/8]| = value<<(7- (z%8)); //leftshift each bit of data j[z]=value; //store the data in the array variable j z++; //increment length } }
Overall Communication Process
Now we have complete understanding of communication process ,now we are ready to upload the program given below and measure the temperature and humidity of the surrounding.
Code:
#include <LiquidCrystal.h> //lcd library LiquidCrystal lcd(2,3,4,5,6,7); //<span class="wikiword">LiquidCrystal</span>(rs, enable, d4, d5, d6, d7) int datapin=11; //Data Pin volatile unsigned long duration=0; unsigned char i[5]; //array to hold 5 formats of data unsigned int j[40]; //declare a array of size 40 to store the 40bits of data from the sensor unsigned char value=0; //Declare and initialize value Variable that is received unsigned checksum=0; //Declare and initialize Checksum Variable int z=0; //Declare and initialize length Variable void setup() { lcd.begin(16, 2); lcd.setCursor(0, 0); lcd.print(" Welcome to "); //Welcome Note lcd.setCursor(0, 1); //Set the location where the text needs to be displayed in LCD lcd.print("NerdyElectronics "); delay(2000); } void loop() { delay(1000); while(1) { delay(1000); pinMode(datapin,OUTPUT); //intially declaring datapin as the output pin digitalWrite(datapin,LOW); //Arduino sends a start signal in low state ,this can be done by setting datapin in Low state delay(20); //output is set to low, and low retention time can’ t be less than 18ms digitalWrite(datapin,HIGH); pinMode(datapin,INPUT_PULLUP); //by default it will become high due to internal pull up duration=pulseIn(datapin, LOW); if(duration <= 84 && duration >= 72) { while(1) { duration=pulseIn(datapin, HIGH); //duration variable starts timing when sensor sends High voltage level response if(duration <= 26 && duration >= 20) //if duration is in between 26µs and 20µs then bit 0 is transmitted by sensor { value=0; //Bit 0 is received } else if(duration <= 74 && duration >= 65) //if duration is in between 74µs and 65µs then bit 1 is transmitted by sensor { value=1; //Bit 0 is received } else if(z==40) { break; //Come out of the loop when 40 bis of data is received } i[z/8]|=value<<(7- (z%8)); //leftshift each bit of data j[z]=value; //store the data in the array variable j z++; //increment length i.e z } } checksum=i[0]+i[1]+i[2]+i[3]; //checksum should be equal to addition of high and low byte of humidity and temperature if(checksum==i[4] && checksum!=0) //checksum should be equal to last byte of 40 bits data and it should not be 0 { lcd.clear(); lcd.setCursor(0, 0); lcd.print("Temp = "); lcd.setCursor(7,0); lcd.print(i[2]); //print high temperature byte in decimal form lcd.print((char)223); //print degree symbol lcd.print("C"); lcd.setCursor(0,1); lcd.print("Humidity = "); lcd.setCursor(11,1); lcd.print(i[0]); //print high Humidity byte in decimal form lcd.setCursor(13,1); lcd.print("%"); } z=0; //set length of bits =0 i[0]=i[1]=i[2]=i[3]=i[4]=0; //set the 5 data formats to 0 } }
Interfacing DHT Sensor using DHT library
DHT-11 interfacing with Arduino can also be done by using a library called DHT.h .The DHT.h library that has many built-in functions which makes us easy to write down the code.
Before uploading the code given below make sure that there is DHT library in your Arduino IDE, If its not there then you can install it by following steps:
>Go to Sketch >>Select Include Library >>Select Manage Libraries >>Then Type DHT sensor Library in the search box >>Click on Install.
By this the required library will be installed, and you can start uploading the code given below.
Code:
# include "DHT.h" //DHT library # include "LiquidCrystal.h" //lcd library # define DHTPIN 11 //DHT pin //Uncomment whichever Type you are using # define DHTTYPE DHT11 //# define DHTTYPE DHT22 DHT dht(DHTPIN, DHTTYPE); //Creating DHT object const int rs = 2, en = 3, d4 = 4, d5 = 5, d6 = 6, d7 = 7; LiquidCrystal lcd(rs, en, d4, d5, d6, d7); float t; //temperature in celcius float h; //humidity in percentage void setup() //setup { Serial.begin(9600); //Setting Boudrate dht.begin(); lcd.begin(16, 2); // Initializes the interface to the LCD screen lcd.setCursor(0, 0); lcd.print(" Welcome to "); lcd.setCursor(0, 1); //Set the location where the text needs to be displayed in LCD lcd.print("NerdyElectronics "); delay(2000); lcd.clear(); } void loop() //loop { h = dht.readHumidity() ; t = dht.readTemperature(); lcd.setCursor(0, 0); lcd.print("Temp: "); lcd.print(t); //Prints the temperature value from the sensor lcd.print(""); lcd.print((char)223); //shows degrees character lcd.print("C"); lcd.setCursor(0, 1); lcd.print("Humi: "); lcd.print(h); lcd.print("% "); delay(5000); lcd.setCursor(0, 0); lcd.print("Temp: "); lcd.print(dht.convertCtoF(t)); //Inbuilt function to convert Temp in Celcius to Fahrenhit lcd.print(" "); lcd.print((char)223); //shows degrees character lcd.print("F"); delay(5000); }
Applications of DHT Sensor
1. DHT sensors can be used as a compensators with ultrasonic sensors to determine the distance more precisely.you can find this application in this link https://nerdyelectronics.com/embedded-systems/sensors/how-to-improve-readings-of-ultrasonic-sensor-temperature-and-humidity-compensation/
2. DHT sensor is used in various applications such as measuring humidity and temperature values in heating, ventilation and air conditioning (HVAC) systems.
3. This sensor can be used in warehouse as level of humidity in air affects various physical, chemical and biological processes.
4. Weather stations also use these sensors to predict weather conditions.
Really good explanation