The DHT11 is a widely used digital sensor that measures ambient temperature and humidity. It is known for its ease of use, affordability, and reasonable accuracy. The sensor is encapsulated in a small package with a single digital output, which makes it an excellent choice for hobbyists and makers for integrating into weather stations, HVAC systems, and home automation projects.
Pin Number | Name | Description |
---|---|---|
1 | VCC | Power supply (3-5.5V DC) |
2 | DATA | Serial 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();
// Read temperature as Celsius (the default)
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 heatIndex = dht.computeHeatIndex(temperature, humidity, false);
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(temperature);
Serial.print("°C Heat index: ");
Serial.print(heatIndex);
Serial.println("°C");
}
Note: The above code uses the DHT
library, which can be installed via the Arduino Library Manager.
Q: How often can I read data from the DHT11? A: The DHT11 should not be read more than once every second.
Q: Can the DHT11 sensor be used outdoors? A: Yes, but it should be protected from direct sunlight, rain, and condensation.
Q: Is calibration required for the DHT11 sensor? A: The DHT11 comes pre-calibrated from the factory and does not typically require additional calibration.
For further assistance, consult the datasheet of the DHT11 sensor or reach out to the community forums for support.