Categories

Arduino and DHT21 temperature and humidity sensor

Arduino and temperature sensor and humidity DHT21

 

Module DHT21 is used to measure temperature and humidity. The following example shows how to use its capabilities with Arduino.

In the example, we used the following elements:

Connection with Arduino DHT21:

In order to read the values of temperature and humidity sensor connect the circuit with the Arduino in the following way:

 

Pin DHT21Pin Arduino
VCC (red)5 V
DATE (yellow)2
GND (black)GND

 

 

The scheme of connection DHT21 sensor with the Arduino Uno.

 

Program for Arduino

At the beginning of the library you want to add to the environment of the Arduino (Sketch -> Include Library -> Add .ZIP Library...).

In the example, we used the following code:

 

#include  

DHT21 dht; 

#define DHT21PIN 2 //assigning Arduino pin 2 as the reading from the sensor 

void setup() 
{ 
 Serial.begin(115200); //initialize serial monitor 
 Serial.println("test Program DHT21"); 
 Serial.println(); 
} 

void loop() 
{ 
 int chk = DHT21.read(DHT21PIN); //check status of the sensor, and then a message appears on the serial monitor 

 Serial.print("sensor Status: "); 
 switch (chk) 
 { 
 case DHTLIB_OK: 
 Serial.print("Oct"); 
 break; 
 case DHTLIB_ERROR_CHECKSUM: 
 Serial.println("checksum Error"); 
 break; 
 case DHTLIB_ERROR_TIMEOUT: 
 Serial.println("End time out - no response"); 
 break; 
 default: 
 Serial.println("Unknown error"); 
 break; 
 } 
 Serial.print("Humidity (%): "); //output value humidity 
 Serial.print((float)DHT21.humidity, 2); 
 Serial.print("TT"); 
 Serial.print("Temperature (C): "); //display the temperature 
 Serial.println((float)DHT21.temperature, 2); 

 delay(1000); //delay between consecutive readings is 1 second. 
}
 

The result of this program, we can see on the screenshot:

 

A screenshot of the serial monitor.