Arduino Temperature and humidity sensor using DHT11
The DHT11 sensor can measure both temp and humidity.
Introduction-
Technical Details
- 3.5 to 5v power and I/O
- 2.5mA max current use during conversion (while requesting data)
- Good for 20-80% humidity readings with 5% accuracy
- Good for 0-50°C temperature readings ±2°C accuracy
- No more than 1 Hz sampling rate (once every second)
- 4 pins with 0.1" spacing
Cost - About 1$.
Pinout
How to use-
In order to use the DHT11 Humidity and temperature sensor, we need an Arduino board.
Connection- DHT11/22 - Arduino
VCC - 5V
GND - GND
DATA - Pin 3(Digital Pin 3)
Note: You can connect data pin to any digital pin according to your program.
Library
You need to install dht11 library first.
Adafruit DHT-sensor-library and Adafrit-Unified-sensor-library.
This library is compatible with both Arduino and ESP8266.
OR you can just go to Arduino IDE > Click on sketch > Include Library > Manager Libraries > Search for dht > Install DHT Sensor Library and Unified Library by Adafruit.
isnan(event.relative_humidity)
So this can be used in if else statements.
Following will return temperature and humidity.
event.temperature
event.relative_humidity
This library is compatible with both Arduino and ESP8266.
To install a library -
If you got a zip file , unzip it and place in Arduino_installation_directory\librariesOR you can just go to Arduino IDE > Click on sketch > Include Library > Manager Libraries > Search for dht > Install DHT Sensor Library and Unified Library by Adafruit.
Coding-
Following lines will print information about Sensor (which is not useful).
Serial.println("Temperature");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" *C");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" *C");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" *C");
Serial.println("------------------------------------");
// Print humidity sensor details.
dht.humidity().getSensor(&sensor);
Serial.println("------------------------------------");
Serial.println("Humidity");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println("%");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println("%");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println("%");
Below are the important lines to include in the sketch.
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>
#define DHTPIN 3 // Pin to which DHT pin connected, Any pin can be used.
// Uncomment the type of sensor in use:
#define DHTTYPE DHT11 // DHT 11
//#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
DHT_Unified dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
sensor_t sensor;
dht.temperature().getSensor(&sensor);
dht.humidity().getSensor(&sensor);
}
void loop() {
delay(2000);
sensors_event_t event;
dht.temperature().getEvent(&event);
dht.humidity().getEvent(&event);
}
Note- The data from the DHT11 must be received with atleast a delay of 1 second.isnan(event.temperature)The above code must not to be changed, its bare minimum to get DHT to work .But to get temperature and humidity, we need to add these lines in loopFollowing function will check sensors for errors ( yes DHT11 has two sensors) and return 1 any error occured.
isnan(event.relative_humidity)
So this can be used in if else statements.
Following will return temperature and humidity.
event.temperature
event.relative_humidity
Sample Sketch
The following code will print temperature and humidity in the serial monitor.
#include <Adafruit_Sensor.h> #include <DHT.h> #include <DHT_U.h> #define DHTPIN 3 // Pin to which DHT pin connected, Any pin can be used. // Uncomment the type of sensor in use: #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT22 // DHT 22 (AM2302) //#define DHTTYPE DHT21 // DHT 21 (AM2301) DHT_Unified dht(DHTPIN, DHTTYPE); void setup() { Serial.begin(9600); dht.begin(); sensor_t sensor; dht.temperature().getSensor(&sensor); dht.humidity().getSensor(&sensor); } void loop() { delay(2000); sensors_event_t event; dht.temperature().getEvent(&event); dht.humidity().getEvent(&event); if (isnan(event.temperature)) Serial.println("Error reading temperature!"); else { Serial.print("Temperature : "); Serial.print(event.temperature); Serial.println(" celsius"); } if (isnan(event.relative_humidity)) Serial.println("Error reading humidity!"); else { Serial.print("Humidity : "); Serial.print(event.relative_humidity); Serial.println(" %"); } } Download
No comments