How DHT11 and DHT22 Sensors Work and interfacing it with a MCU on Proteus

dht11 temperature and humidity sensor

What is DHT11 and DHT22 Sensor

Sensors are used to collect data by measuring physical quantities and properties, this data is then used for various applications. Today we have sensors that can do all sorts of things: see, taste, hear and detect motions. It is hard to imagine life without these sensors around us, they help manage many things around us from opening the automatic door at your local store to controlling the water release into the nuclear plants. 

With this blog, we will be telling you about one such type of sensor: humidity and temperature sensor, namely DHT11 and DHT22, and how to use them on a circuit simulation software: Proteus. These can be used in HVAC systems in pharmaceutical plants to protect life-saving drugs, in indoor agriculture facilities to keep plants at optimum condition, food processing warehouses to avoid spoilage, etc.

Hardware components within the sensor

Best way to learn how a sensor work is to know its components DHT11 and DHT22 contains a humidity sensing component, a thermistor, and a small onboard IC.

  • The humidity sensing component has two electrodes with a moisture-holding substrate (salt or conductive plastic polymer). The conductivity between the electrodes changes as ions is released by the substrate as it absorbs water vapour. This conductivity change is directly proportional to the relative humidity.
dht11 arduino
  • A Thermistor is a particular type of resistor, whose resistance changes with change in temperature, there are two types: NTC (Negative Temperature Coefficient) and PTC (Positive Temperature Coefficient), NTC Thermistors resistance can decrease drastically with an increase of the temperature (about 100 ohms or more of change per degree) and vice-versa for PTC Thermistors. The DHTxx sensors uses an internal NTC Thermistor. 
dht11 pinout



  • The Onboard IC measures the analog signal coming from the humidity sensor and the thermistor and uses the stored calibration coefficients to convert them for the respective usage.

Explore Raspberry Pi Collection

Pins on DHT11 and DHT22 sensor

Vcc :-> provides power to the sensor (3.3V to 5.5V)

Data pin :-> used to communicate between the sensor and the microcontroller.

Gnd :-> connects to the ground terminal of the Arduino.

Differences between DHT11 & DHT22

Parameter

DHT22

DHT11

 

A close-up of a keychain

Description automatically generated with low confidence

Cost

Higher

Lower

Temp range

-40 to 125C

0 to 50C

Sampling Rate

0.5Hz

1Hz

Body Size

15.1mm x 25mm x 7.7mm

15.5mm x 12mm x 5.5mm

DHT22 is more precise and works with a larger range of temperature (-40 to 80°C) & humidity (0-100%), but when it comes to a higher sampling rate and the cost of the sensor, DHT11 takes the lead. So, it is essential to weigh the pros and cons and choose which will best suit the application you are going for.

Similarities between DHT11 and DHT22

  • Both can operate in the range of 3 to 5 volts. 
  • They have the same pinouts. 
  • They use the max current of 2.5mA to request data from microprocessor. 

These similarities allow them to be used interchangeably for different advantageous scenarios, all you need is to do is adjust the Code a bit and you are all set to go!

How to Connect DHT11 and DHT22 sensor to an Arduino compatible MCU within the Proteus software

Initial Setup of Proteus

Create a New Project with Arduino development board 328

dht11 sensor working

To get this screen  

dht11 temperature sensor

Then click on Library in the toolbar to go to Pick up library or the P symbol in the devices and select the DHT11 and DHT22 models. And click on the screen to place them in the workspace

dht11 arduino code

 

LM016L

dht11 sensor arduino
Virtual Terminal & Oscilloscope
dht11 sensor pin diagram

Go to meters   symbol in the left Task Bar and select Virtual terminal and Oscilloscope(2 times ).

dht11 sensor specifications
dht11 temperature and humidity sensor arduino code

Connections

First we will try to attach the RXD of the Arduino to the TXD of the Virtual Terminal and vice versa

Instead of connecting them directly with wires we use labels and different types of terminals in proteus to connect them .

To do that find the terminals   symbol from the left task bar and select the default terminal

dht11 vs dht22

Now we connect them to the Virtual terminal (clicking on the screen brings up the same object )

dht11 diagram

Now we right click on the default terminal and the rename it as TXD and RXD respectively

dht11 sensor information

DHT 11

Connect the DHT-11 sensor, where the Vdd pin goes to +5v (Using the terminal for power and renaming it as 5V) and the data pin to IO9(Rename the terminal as IO9) and connecting the resistor R5, with a value of 4.7K ohms between the Supply and the data pin. This ensures that it is always in a high state and a ground connection to the 4th pin. The third, not connected pin, is not shown in proteus. The ground pin is connected to the ground terminal.

The Same connection is to be done for DHT-22; only the input should be IO11.

dht11 setup

 

LM016L

Connect the LCD screen According to the Diagram below

LM016L

Where Vss, Vee, RW, D0, D1, D2, D3 are connected to the ground, as shown.  

 Vdd is Connected to the +5V. 

RS, E, D4, D5, D6, D7 are connected to IO8, IO7, IO6, IO5, IO4, IO3 respectively using the default terminal.

Here we will be using the DHT library, making our work much easier.

Click the link to download the Library from the GitHub repository:

To install the Library in Proteus, open the installation folder of proteus, and Extract the folder in "Installation_Directory\Proteus 8 Professional\Tools\ARDUINO\libraries”

Note: - if any issues occur in proteus simulation, then it is recommended to use an older DHT library

The Code

 

// This is an Arduino Program to compare the readings of DHT-11 and DHT-22 temperature humidity sensor simulated in Proteus 

#include <LiquidCrystal.h>

#include<math.h>

// include the DHT library 

#include "DHT.h"

 

LiquidCrystal lcd(8,7,6,5,4,3);

 

DHT dht(9,DHT11); // This is to Initialize the which DHT 11 Library 

DHT dht2(11,DHT22); // Including DHT 22 library 

 

char temperature[] = "Temp 11 = 00.0 C  ";

char temperature2[] = "Temp 22 = 00.0 C  ";

char humidity[]    = "RH 11 = 00.0 %  ";

char humidity2[]    = "RH 22 = 00.0 %  ";

 

void setup()

 { 

 lcd.begin(162);

 Serial.begin(9600);

 dht.begin();

 dht2.begin();

 }

 

void loop()

 { 

  delay(500);           // wait 1s between readings

  // Read humidity

  float RH = dht.readHumidity();

  float RH2 = dht2.readHumidity();

  

  //Read temperature 

  float Temp = dht.readTemperature();

  float Temp2 = dht2.readTemperature();

  

  if (isnan(RH) || isnan(Temp)) {

    lcd.setCursor(60);

    lcd.print("Error");

    return;

  }

  

  temperature[10]     = Temp / 10 + 48; // At Char 10 we have our 10's Digit , we add 48 ( 0 in decimal) to convert it to ASCII nomenclature 

  temperature2[10]    = Temp2 / 10 + 48

 

  temperature[11]     = int(Temp) % 10 +48 ; // ones value

  temperature2[11]    = int(Temp2) % 10 + 48;

 

 

 //Char 12 is the decimal point in our initial set of Characters 

  temperature2[13]= (int(10*(Temp2-int(Temp2))))+48; // This is to calculate the remainder part for DHT 22 , because it has an accuracy of 0.2 -0.5 

 

  temperature[14]    = 223; // This assigns the degree symbol 

  temperature2[14]   =223;

 //Same for Humidity 

  humidity[8]        = RH / 10 + 48;

  humidity2[8]       = RH2 / 10 + 48;

  humidity[9]        = int(RH) % 10 + 48;

  humidity2[9]       = int(RH2) % 10 + 48;

  // char 10 is the dot , the char 12 will be the fraction part 

  humidity2[11]       = (int(10*(RH2-int(RH2))))+48;

  

  //Printing the Values 

  

  Serial.println(temperature);

  Serial.println(temperature2);

  

  Serial.println(humidity);

  Serial.println(humidity2);

  

  lcd.setCursor(00);

  lcd.print(temperature);

  lcd.print(humidity);

  lcd.scrollDisplayLeft();

  lcd.setCursor(01);

  lcd.print(temperature2);

  lcd.print(humidity2);

  lcd.scrollDisplayLeft();

}

 

 

Execution :-

This Code must be written in the Source Code of the Arduino -Proteus Simulation. You can find the Source code by clicking on the Source Code icon in the top taskbar.  Paste the Code in the file named main.ino

dht11 temperature and humidity sensor arduino program

 

The Next Step is to Build the Project

dht11 temperature and humidity sensor working

And then Click on Debug>>Start VSM debugging or Run simulation. 

Shortcut - Ctrl+F12

Graphical user interface, text, application

Description automatically generated

And Click on the Bottom Play button to Start the Simulation if the Simulation is Paused.

Chart

Description automatically generated

After That, we can compare the Reading of the Two sensors using the Virtual Terminal, LCD screen and an Oscilloscope.

Code explanation

The Code starts with defining the Arduino libraries like LiquidCrystal for LCD interface and math.h  for some math functions and DHT Library to Interface with the Sensors.

And we are Defining the LCD PINs followed by creating two separate DHT objects Connected to their respective data pins to access the various functions in the DHT library.

// This is an Arduino Program to compare the readings of DHT-11 and DHT-22 temperature humidity sensor simulated in Proteus 

#include <LiquidCrystal.h>

#include <math.h>

// include the DHT library 

#include "DHT.h"

 

LiquidCrystal lcd(8,7,6,5,4,3);

 

DHT dht(9,DHT11); // This is to Initialize the which DHT 11 Library 

DHT dht2(11,DHT22); // Including DHT 22 library

 

Then we define our Character Strings which we will manipulate to Display the Temperature and the Relative Humidity. 

char temperature[] = "Temp 11 = 00.0 C  ";

char temperature2[] = "Temp 22 = 00.0 C  ";

char humidity[]    = "RH 11 = 00.0 %  ";

char humidity2[]    = "RH 22 = 00.0 %  ";

 

Next comes the void setup function, where we begin the Lcd and initiate the serial communication to print results on the serial monitor and  initialize both the sensors.

void setup()

 { 

 lcd.begin(162);

 Serial.begin(9600);

 dht.begin();

 dht2.begin();

 }

 

In the loop function

void loop()

 { 

  delay(500);           // wait 1s between readings

  // Read humidity

  float RH = dht.readHumidity();

  float RH2 = dht2.readHumidity();

  

  //Read temperature 

  float Temp = dht.readTemperature();

  float Temp2 = dht2.readTemperature();

 

We set a delay so the values are readable. 

And we define the RH and RH2 as float variables to read humidity in the dht and dht2 object, respectively. 

Likewise, we define Temp and Temp2 as float variables to read the temperature in the dht and dht2 objects, respectively. 

Error detection code 

  if (isnan(RH) || isnan(Temp)) {

    lcd.setCursor(60);

    lcd.print("Error");

    return;

  }

 

If, the sensors read no value then the LCD screen will display Error

Conversion

temperature[10]     = Temp / 10 + 48; // At Char 10 we have our 10's Digit , we add 48 ( 0 in decimal) to convert it to ASCII nomenclature 

temperature2[10]    = Temp2 / 10 + 48

 

temperature[11]     = int(Temp) % 10 +48 ; // ones value

temperature2[11]    = int(Temp2) % 10 + 48;

 

 

//Char 12 is the decimal point in our initial set of Characters 

temperature2[13]= (int(10*(Temp2-    int(Temp2))))+48; // This is to calculate the remainder part for DHT 22 , becauseit has an accuracy of 0.2 -0.5 

 

temperature[14]    = 223; // This assigns the degree symbol 

temperature2[14]   =223;

 

In this, we convert the raw float values and individually replace them with the Character set we defined earlier 

Note that we add 48 after the calculation because LCD screens work with ASCII nomenclature; 48 is 0 in ASCII Nomenclature 

char 13 is the fractional part of the DHT 22 sensor as it has uncertainty or accuracy of 0.2 - 0.5 Celsius, while DHT 11 has uncertainty or accuracy of 2 deg Celsius.  

Same for Relative Humidity 

Now we print and display the results 

  //Printing the Values 

  

  Serial.println(temperature);

  Serial.println(temperature2);

  

  Serial.println(humidity);

  Serial.println(humidity2);

 

This is for the Serial monitor. Then, we just must print the Character sets, which have all the texts. 

Next part is for the Lcd LM160L

 

  lcd.setCursor(00);

  lcd.print(temperature);

  lcd.print(humidity);

  lcd.scrollDisplayLeft();

  lcd.setCursor(01);

  lcd.print(temperature2);

  lcd.print(humidity2);

  lcd.scrollDisplayLeft();

 

Here we display the temperature and Humidity of DHT 11 in the Same Line and temperature and Humidity of DHT 22 in the below line as the LCD cannot display all of them at the same time; we use the scrollDisplayleft function to display them all, scrolling through them.

Expected Output 

Final Circuit

circuit diagram of dht11

Readings 

Graphical user interface

Description automatically generated

Virtual Terminal 

Text

Description automatically generated

Oscilloscope

A picture containing text, electronics, display

Description automatically generated

 

The Delay is not that visible in the given screenshot. 

LCD screen

A picture containing text, clock

Description automatically generatedA picture containing text, clock

Description automatically generated 

This blog has been submitted by Robotics and Circuits, MIT Manipal under the Robocraze Club Outreach Program.

Author: Anirudh Bharadwaj, Prakhar Swarnakar

 

Components and Supplies

    You may also like to read

    Frequently Asked Questions

    Back to blog

    Leave a comment

    Please note, comments need to be approved before they are published.

    Components and Supplies

      You may also like to read