DHT11 is a low-cost digital output
basic temperature and humidity sensor. The DHT11 detects humidity by measuring
the resistance between two electrodes. The humidity sensing component
resistance changes with the water vapor present in the surrounding air. The
DHT11 measures temperature with an NTC temperature sensor.
The DHT11 sends data on a single
line in digital format, so there is no need for ADC. The signal line needs to be
pulled up by 10Kohm resistor to keep the signal line high by default.
Here are the ranges of DHT11
- Humidity Range: 20-90% RH
- Humidity Accuracy: ±5% RH
- Temperature Range: 0-50 °C
- Temperature Accuracy: ±2 °C
- Operating Voltage: 3V to 5.5V
Required Hardware:
- Arduino UNO
- DHT11 or DHT22
How to connect DHT11 with Arduino Uno:
Here this schematic shows how to
connect DHT11 with Arduino, if you are using DHT11 module then you can directly
connect the signal pin to the Arduino, a pull-up resistor is already present
on the module.
Reading Temperature & Humidity:
To use the DHT11 sensor with Arduino we
first, need to add DHT library. Download the library from here and add it
to the Arduino by going to Sketch à Include
Library à Add.ZIP
Library
After installing the library into
Arduino IDE, use this program to display temperature and humidity on serial
monitor.
/* How to use the DHT-11 sensor with Arduino uno
Temperature and humidity sensor
*/
//Libraries
#include <DHT.h>;
//Constants
#define DHTPIN 4 // what pin we're connected to
#define DHTTYPE DHT11 // comment this line to use DHT22
// #define DHTTYPE DHT22 // Uncomment this line to use DHT22
DHT dht(DHTPIN, DHTTYPE); //// Initialize DHT sensor for normal 16mhz Arduino
//Variables
float hum; //Stores humidity value
float temp; //Stores temperature in Celsius
float farh; //Stores temperature in Fahrenheit
float heatIndex1; //Stores heat Index in Fahrenheit
float heatIndex2; //Stores heat Index in Celsius
void setup()
{
Serial.begin(9600);
dht.begin();
}
void loop()
{
//Read data and store it to variables hum and temp
hum = dht.readHumidity();
temp = dht.readTemperature();
farh = dht.readTemperature(true);
heatIndex1 = dht.computeHeatIndex(farh, hum);
heatIndex2 = dht.computeHeatIndex(temp, hum, false);
//Print temp and humidity values to serial monitor
Serial.print("Humidity: ");
Serial.print(hum);
Serial.println(" %");
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" °C\t");
Serial.print(farh);
Serial.println(" °F");
Serial.print("Heat Index: ");
Serial.print(heatIndex2);
Serial.print(" °C\t");
Serial.print(heatIndex1);
Serial.println(" °F");
delay(2000); //Delay 2 sec.
}
Upload this code to Arduino and open serial
monitor and set the baud rate to 9600. You can see the humidity and temperature
every 2 seconds. You can use the same code to use the DHT22 sensor. Just Comment #define
DHTTYPE DHT11 and Uncomment #define
DHTTYPE DHT22.
How the code works?
First, we need to include the DHT library. You can install the library
from Tools à Manage Libraries
and searching for DHT. Install the library from Adafruit. You can also get the
library from here.
#include <DHT.h>;
Now we define the pin to which the sensor is attached to
#define DHTPIN 4 // what pin we're connected to
Next, we define
which type of DHT sensor we are using. In this case, we are using DHT11 sensor.
In order to use DHT22, replace DHT11 with DHT22
#define DHTTYPE DHT11 // comment this line to use DHT22
// #define DHTTYPE DHT22 // Uncomment this line to use DHT22
// #define DHTTYPE DHT22 // Uncomment this line to use DHT22
Next we
initialize the sensor
DHT dht(DHTPIN, DHTTYPE);
Here we define some variables to
store the data getting from the sensor
float hum; //Stores humidity value
float temp; //Stores temperature in Celsius
float farh; //Stores temperature in Fahrenheit
float heatIndex1; //Stores heat Index in Fahrenheit
float heatIndex2; //Stores heat Index in Celsius
float temp; //Stores temperature in Celsius
float farh; //Stores temperature in Fahrenheit
float heatIndex1; //Stores heat Index in Fahrenheit
float heatIndex2; //Stores heat Index in Celsius
In void setup, we initialize the
serial monitor and DHT11 sensor
void setup()
{
Serial.begin(9600);
dht.begin();
}
{
Serial.begin(9600);
dht.begin();
}
Next, in the void loop we define
different functions to get the data continuously
hum = dht.readHumidity(); This function will take the
reading of the Humidity and save it in the variable hum
temp = dht.readTemperature(); This function will take the
reading of the temperature in Celsius and store the value in the variable temp.
To get the temperature reading in Fahrenheit we write the function as dht.readTemperature(true);
This DHT library also gives us to
calculate the heat index, the heat index is the actual heat we feel when relative
humidity is factored with air temperature. To calculate this we use dht.computeHeatIndex(temp,
hum, false); to get it in Celsius or dht.computeHeatIndex(farh, hum); (here the temperature should be in Fahrenheit, in our example code above we used farh
as a variable to store Fahrenheit value) to get it in Fahrenheit.
Next
we print this information on the serial monitor. A delay of 2 seconds is
provided since the DHT11 sensor takes samples for every 2 seconds.
Serial.print("Humidity: ");
Serial.print(hum);
Serial.println(" %");
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" °C\t");
Serial.print(farh);
Serial.println(" °F");
Serial.print("Heat Index: ");
Serial.print(heatIndex2);
Serial.print(" °C\t");
Serial.print(heatIndex1);
Serial.println(" °F");
delay(2000); //Delay 2 sec.
Serial.print(hum);
Serial.println(" %");
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" °C\t");
Serial.print(farh);
Serial.println(" °F");
Serial.print("Heat Index: ");
Serial.print(heatIndex2);
Serial.print(" °C\t");
Serial.print(heatIndex1);
Serial.println(" °F");
delay(2000); //Delay 2 sec.
Nice
ReplyDelete