How DHT11 and DHT22 Sensors Work and interfacing it with a MCU on Proteus
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.
- 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
Excerpt
Frequently Asked Questions
1. What is the difference between DHT11 and DHT22?
The DHT11 is a basic, cheaper sensor with a temperature range of 0-50°C and humidity range of 20-80%. The DHT22 offers higher accuracy, measuring temperatures from -40 to 80°C and humidity from 0-100%. Additionally, DHT22 has better precision, making it suitable for more demanding applications.
2. How do these sensors measure humidity and temperature?
DHT sensors use a thermo-hygrometer technique. They incorporate a polymer humidity sensor and a thermistor for temperature. The sensor interacts with the surrounding air, generating a digital signal that represents the humidity and temperature, which can be read by microcontrollers like Arduino.
3. What is the pin configuration of DHT sensors?
DHT sensors typically feature three pins: VCC (power), GND (ground), and Data (signal). Connect VCC to a 3.3V or 5V power source, GND to the ground, and the Data pin to an input pin on your microcontroller for readings.
4. How to interface DHT11 with Arduino?
To interface a DHT11 with an Arduino, connect the VCC pin to 5V, GND to ground, and the Data pin to a digital pin. Use the DHT library in the Arduino IDE to read the sensor data. Just include the library, initialize the sensor, and start reading temperature and humidity values.
5. How to simulate DHT sensors in Proteus?
To simulate DHT sensors in Proteus, create a new schematic and add the DHT sensor component from the library. Wire it correctly to a microcontroller like Arduino. Configure the virtual environment to simulate temperature and humidity readings, allowing you to test your code within the software.
6. What is the accuracy of DHT11 vs DHT22?
The DHT11 has a temperature accuracy of ±2°C and a humidity accuracy of ±5%. In contrast, the DHT22 boasts a temperature accuracy of ±0.5°C and a humidity accuracy of ±2-5%, making it the better choice for precision applications requiring reliable readings.
7. What are the applications of DHT sensors?
DHT sensors are commonly used in various applications, including home automation, HVAC systems, weather stations, and greenhouse monitoring. They are popular for projects requiring accurate humidity and temperature data for efficient controls and monitoring.
8. How to troubleshoot DHT sensor errors?
To troubleshoot DHT sensor errors, first check the wiring and ensure the connections are secure. Verify that the sensor is powered properly and using the correct library. If errors persist, replace the sensor, as it may be faulty. Testing with another compatible microcontroller can also help isolate the issue.
9. How to calibrate a DHT sensor?
Calibrating a DHT sensor involves comparing its readings with a known accurate device. Take several measurements and adjust the readings in your code as needed to match the accurate device. This will enhance the sensor's accuracy for temperature and humidity measurements.
10. Can I use DHT sensors for outdoor weather monitoring?
Yes, DHT sensors can be used for outdoor weather monitoring, but they're sensitive to direct sunlight and moisture. To protect the sensor, use a protective enclosure, ensuring airflow while shielding it from rain. This setup will provide reliable outdoor temperature and humidity data.
1. What is the difference between DHT11 and DHT22?
The DHT11 is a basic, cheaper sensor with a temperature range of 0-50°C and humidity range of 20-80%. The DHT22 offers higher accuracy, measuring temperatures from -40 to 80°C and humidity from 0-100%. Additionally, DHT22 has better precision, making it suitable for more demanding applications.
2. How do these sensors measure humidity and temperature?
DHT sensors use a thermo-hygrometer technique. They incorporate a polymer humidity sensor and a thermistor for temperature. The sensor interacts with the surrounding air, generating a digital signal that represents the humidity and temperature, which can be read by microcontrollers like Arduino.
3. What is the pin configuration of DHT sensors?
DHT sensors typically feature three pins: VCC (power), GND (ground), and Data (signal). Connect VCC to a 3.3V or 5V power source, GND to the ground, and the Data pin to an input pin on your microcontroller for readings.
4. How to interface DHT11 with Arduino?
To interface a DHT11 with an Arduino, connect the VCC pin to 5V, GND to ground, and the Data pin to a digital pin. Use the DHT library in the Arduino IDE to read the sensor data. Just include the library, initialize the sensor, and start reading temperature and humidity values.
5. How to simulate DHT sensors in Proteus?
To simulate DHT sensors in Proteus, create a new schematic and add the DHT sensor component from the library. Wire it correctly to a microcontroller like Arduino. Configure the virtual environment to simulate temperature and humidity readings, allowing you to test your code within the software.
6. What is the accuracy of DHT11 vs DHT22?
The DHT11 has a temperature accuracy of ±2°C and a humidity accuracy of ±5%. In contrast, the DHT22 boasts a temperature accuracy of ±0.5°C and a humidity accuracy of ±2-5%, making it the better choice for precision applications requiring reliable readings.
7. What are the applications of DHT sensors?
DHT sensors are commonly used in various applications, including home automation, HVAC systems, weather stations, and greenhouse monitoring. They are popular for projects requiring accurate humidity and temperature data for efficient controls and monitoring.
8. How to troubleshoot DHT sensor errors?
To troubleshoot DHT sensor errors, first check the wiring and ensure the connections are secure. Verify that the sensor is powered properly and using the correct library. If errors persist, replace the sensor, as it may be faulty. Testing with another compatible microcontroller can also help isolate the issue.
9. How to calibrate a DHT sensor?
Calibrating a DHT sensor involves comparing its readings with a known accurate device. Take several measurements and adjust the readings in your code as needed to match the accurate device. This will enhance the sensor's accuracy for temperature and humidity measurements.
10. Can I use DHT sensors for outdoor weather monitoring?
Yes, DHT sensors can be used for outdoor weather monitoring, but they're sensitive to direct sunlight and moisture. To protect the sensor, use a protective enclosure, ensuring airflow while shielding it from rain. This setup will provide reliable outdoor temperature and humidity data.
