Skip to content
Home » Embedded Systems » Sensors » DHT11 and DHT22 Sensor: Working Principle and Interfacing Guide

DHT11 and DHT22 Sensor: Working Principle and Interfacing Guide

Arduino Uno wiring diagram for DHT22 and DHT11 temperature humidity sensors with 10k pull-up resistor and 16x2 LCD display showing readings
Sensors for Embedded Systems
Part 14 of 18View Full Path →

KEY TAKEAWAYS

  • DHT sensors measure both temperature and humidity using a single-wire digital protocol
  • DHT11 measures 0-50°C with ±2°C accuracy; DHT22 measures -40 to 80°C with ±0.5°C accuracy
  • The sensor sends 40 bits of data (humidity + temperature + checksum) in response to a start signal
  • A 10K pull-up resistor on the data line is required for reliable communication

The DHT sensor (Digital Humidity and Temperature) is one of the most popular sensors for Arduino projects — but picking the right one and wiring it correctly trips up a lot of beginners.

The DHT22 sensor offers better precision and accuracy than DHT11, but understanding their internal components and proper Arduino interfacing makes the difference between reliable readings and frustrating failures.

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:

  1. 8 – Bit data for integral part of RH value,
  2. 8 – Bit data for decimal part of RH value,
  3. 8 – Bit data for integral part of Temperature value,
  4. 8 – Bit data for decimal part of Temperature value and
  5. 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

DHT sensor 40-bit data frame structure showing five bytes: humidity high byte 00110101, humidity low byte 00000000, temperature high byte 00011000, temperature low byte 00000000, and checksum byte 01001101

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.

DHT sensor binary data checksum verification showing 8-bit addition of four data bytes 00110101 plus 00000000 plus 00011000 plus 00000000 equals checksum 01001101

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
DHT sensor overall single-bus communication process timing diagram showing MCU start signal, DHT response signal, data bit 0 and bit 1 output sequences, and bus pull-up after transmission completes

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.

Common Mistakes When Using DHT Sensors

  • Reading too frequently: The DHT11 needs at least 1 second between readings; the DHT22 needs 2 seconds. Reading faster returns stale data or causes the sensor to stop responding. If your loop() runs faster, use millis() to throttle reads rather than delay() so the rest of your code keeps running.
  • Missing pull-up resistor: The data line requires a 4.7kΩ–10kΩ pull-up to VCC. Some breakout boards include this resistor; bare sensors do not. Without it, the signal edges are too slow and the timing-based protocol fails — you get checksum errors or all-zero readings.
  • Interrupt conflicts: The DHT protocol is timing-critical (counting microseconds on pulse widths). If interrupts fire during a read — from a timer, serial RX, or other peripheral — the timing is corrupted. Some libraries disable interrupts during the read sequence. Be aware of this if you use a DHT sensor alongside time-sensitive interrupts.
  • Placing the sensor near heat sources: The DHT measures ambient air temperature. Mounting it near a voltage regulator, motor driver, or even the Arduino board itself introduces a 2–5°C bias. Mount the sensor away from the PCB, ideally in a ventilated enclosure, for accurate ambient readings.
  • Confusing DHT11 and DHT22 in code: The two sensors use the same protocol but different data formats. DHT11 sends integer-only values (no decimal). DHT22 sends a 16-bit signed value with one decimal place. Using the wrong DHTTYPE constant causes absurd readings (e.g., 2500°C). Always verify you have #define DHTTYPE DHT11 or DHT22 matching your actual hardware.
  • Checksum errors in noisy environments: If you consistently get failed reads, the data line may be picking up noise. Keep the wire short (under 5 metres for DHT22, under 1 metre for DHT11). For longer distances, use shielded cable and consider a logic-level buffer at the MCU end.

Related on this site

Frequently Asked Questions

What is the full form of DHT sensor?

DHT stands for Digital Humidity and Temperature. The DHT11 and DHT22 are integrated sensors that measure both temperature and relative humidity and report them on a single one-wire data line.

What is the difference between DHT11 and DHT22?

DHT22 has a wider range (-40 to 80 C vs DHT11’s 0 to 50 C) and tighter accuracy (+/-0.5 C vs +/-2 C), but it costs roughly twice as much and can only be read once every 2 seconds (DHT11 supports a 1-second sampling rate).

How does the DHT11 sensor work?

The DHT11 combines a capacitive humidity sensor (humidity changes the dielectric of a polymer film, changing its capacitance) with a thermistor for temperature. The on-board chip digitises both values and sends them over a single GPIO line using a one-wire protocol with strict timing.

Do I need a library to use DHT11 with Arduino?

No. The protocol is just timed pulses on one GPIO pin, so you can bit-bang it directly. The DHT library is convenient but adds about 1.5 KB of program memory — direct bit-banging is the better choice on tight 8-bit MCUs.

How often can you read the DHT11 / DHT22?

DHT11 can be sampled about once per second. DHT22 needs roughly 2 seconds between reads. Reading faster either returns the cached value or fails the checksum.

📖 Related: ADC in Microcontrollers: How It Works, Reading Patterns, and Common Pitfalls

Tags:

1 thought on “DHT11 and DHT22 Sensor: Working Principle and Interfacing Guide”

Leave a Reply

Your email address will not be published. Required fields are marked *