
How DHT11 and DHT22 Sensors Work and interfacing it with a MCU on Proteus
What is DHT11 & 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.
- 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.Β
- 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 |
Β |
|
|
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
To get this screenΒ Β
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
Β
LM016L
Go to meters Β Β symbol in the left Task Bar and select Virtual terminal and Oscilloscope(2 times ).
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
Now we connect them to the Virtual terminal (clicking on the screen brings up the same object )
Now we right click on the default terminal and the rename it as TXD and RXD respectively
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.
Β
LM016L
Connect the LCD screen According to the Diagram below
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(16,Β 2);
Β 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(6,Β 0);
Β Β Β Β 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(0,Β 0);
Β Β lcd.print(temperature);
Β Β lcd.print(humidity);
Β Β lcd.scrollDisplayLeft();
Β Β lcd.setCursor(0,Β 1);
Β Β 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
Β
The Next Step is to Build the Project
And then Click on Debug>>Start VSM debuggingΒ or Run simulation.Β
Shortcut - Ctrl+F12
And Click on the Bottom Play button to Start the Simulation if the Simulation is Paused.
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(16,Β 2);
Β 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(6,Β 0);
Β Β Β Β 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(0,Β 0);
Β Β lcd.print(temperature);
Β Β lcd.print(humidity);
Β Β lcd.scrollDisplayLeft();
Β Β lcd.setCursor(0,Β 1);
Β Β 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
ReadingsΒ
Virtual TerminalΒ
Oscilloscope
Β
The Delay is not that visible in the given screenshot.Β
LCD screen
Β
This blog has been submitted by Robotics and Circuits, MIT Manipal under the Robocraze Club Outreach Program.
Author: Anirudh Bharadwaj, Prakhar Swarnakar
Β