The DHT11 is a widely used digital temperature and humidity sensor that offers a simple interface and easy integration into various electronic projects, including DIY projects and home automation systems. It measures the surrounding air and provides a digital signal with the temperature and humidity readings. Its cost-effectiveness and simplicity make it an ideal choice for hobbyists and educators who are introducing concepts of environmental sensing.
Pin Number | Name | Description |
---|---|---|
1 | VCC | Power supply (3.5-5.5 V DC) |
2 | DATA | Digital data output |
3 | NC | Not connected |
4 | GND | Ground |
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
// Wait a few seconds between measurements.
delay(2000);
// Reading temperature or humidity takes about 250 milliseconds!
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Compute heat index in Celsius (isFahrenheit = false)
float heat_index = dht.computeHeatIndex(temperature, humidity, false);
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.print("°C Heat index: ");
Serial.print(heat_index);
Serial.println("°C");
}
Q: How long does the DHT11 need to acclimatize to the environment? A: It is recommended to let the DHT11 sensor sit in the new environment for at least an hour before taking readings.
Q: Can the DHT11 sensor be used outdoors? A: The DHT11 is not waterproof and is designed for indoor use. If used outdoors, it should be placed in a protective enclosure.
Q: How often can I read data from the DHT11? A: The DHT11 should not be read more than once every second. Reading it more frequently may lead to self-heating and inaccurate readings.