In this NodeMCU(ESP8266) Tutorial, I will tell you how to use the DHT11 or the DHT22 sensor for measuring temperature and humidity with the NodeMCU(ESP 8266). You can watch the following video or read the written tutorial below for more details.
Overview
These sensors are very popular for electronics hobbyists because there are very cheap but still providing great performance. Here are the main specifications and differences between these two sensors:
The DHT22 is the more expensive version which obviously has better specifications. Its temperature measuring range is from -40 to +125 degrees Celsius with +-0.5 degrees accuracy. The DHT11 temperature range is from 0 to 50 degrees Celsius with +-2 degrees accuracy.
The DHT22 sensor has better humidity measuring range, from 0 to 100% with 2-5% accuracy. The DHT11 humidity range is from 20 to 80% with 5% accuracy.
There are two specifications where the DHT11 is better than the DHT22. That’s the sampling rate which for the DHT11 is 1Hz or one reading every second. The DHT22 sampling rate is 0,5Hz or one reading every two seconds and also the DHT11 has a smaller body size. The operating voltage of both sensors is from 3 to 5 volts. The max current used when measuring is 2.5mA.
Table of Contents:
DHT11 / DHT22 Working Principle
Ok now let’s see how these sensors actually work. They consist of a humidity sensing component, an NTC temperature sensor (or thermistor) and an IC on the backside of the sensor.
For measuring humidity they use the humidity sensing component which has two electrodes with moisture holding substrate between them. So as the humidity changes, the conductivity of the substrate changes or the resistance between these electrodes changes. This change in resistance is measured and processed by the IC which makes it ready to be read by a microcontroller.
A thermistor is actually a variable resistor that changes its resistance with the change of the temperature. These sensors are made by sintering of semiconductive materials such as ceramics or polymers in order to provide larger changes in the resistance with just small changes in temperature. The term “NTC” means “Negative Temperature Coefficient”, which means that the resistance decreases with the increase of the temperature.
Required Components:
Required Components:
1. NodeMCU Buy from Amazon
2. DHT22 Buy from Amazon
3. Breadboard Buy from Amazon
4. Connecting wires Buy from Amazon
5.Data Cable Buy from Amazon
Circuit Schematics
The DHTxx sensors have four pins, VCC, GND, data pin and a not connected pin which has no usage. A pull-up resistor from 5K to 10K Ohms is required to keep the data line high. In order to enable the communication between the sensor and the Arduino Board. There are some versions of these sensors that come with breakout boards with a built-in pull-up resistor. They will have just 3 pins.
The DHTXX sensors have their own single wire protocol used for transferring the data. This protocol requires precise timing and the timing diagrams for getting the data from the sensors. which can be found from the datasheets of the sensors. However, we don’t have to worry much about these timing diagrams because we will use the DHT library which takes care of everything.
Source Code
First, we need to include the DHT library which can be found from the Arduino official website. Then define the pin number to which our sensor is connected. Create a DHT object. In the setup section, we need to initiate the serial communication to print the results. Using the read22() function we will read the data from the sensor. If you use the DHT11 sensor you will need to you the read11() function. In the end, we will print the temperature and the humidity values on the serial monitor.
/* DHT11/ DHT22 Sensor Temperature and Humidity Tutorial * Program made by M V Subrahmanyam, * www.electronicsinnovation.com */ /* * You can find the DHT Library from Arduino official website * https://playground.arduino.cc/Main/DHTLib */ #include #define dataPin D6 // Defines pin number to which the sensor is connected dht DHT; // Creats a DHT object void setup() { Serial.begin(9600); pinMode(D5,OUTPUT); pinMode(D7,OUTPUT); digitalWrite(D5,HIGH); digitalWrite(D7,LOW); } void loop() { int readData = DHT.read22(dataPin); // Reads the data from the sensor float t = DHT.temperature; // Gets the values of the temperature float h = DHT.humidity; // Gets the values of the humidity // Printing the results on the serial monitor Serial.print("Temperature = "); Serial.print(t); Serial.print(" *C "); Serial.print(" Humidity = "); Serial.print(h); Serial.println(" % "); delay(2000); // Delays 2 secods, as the DHT22 sampling rate is 0.5Hz }
We will be able to see the temperature and humidity on the serial monitor. once we upload the code.
[…] The working principle and more detailed information about DHT22/DHT11 are available here. […]