The DHT11 is a basic, low-cost digital temperature and humidity sensor. It uses a capacitive humidity sensor and a thermistor to measure the surrounding air and outputs a digital signal on the data pin. It's simple to use and ideal for environmental sensing and data logging and perfect for a weather station or humidor control system.
Pin Number | Name | Description |
---|---|---|
1 | VCC | Power supply (3.3 to 5V DC) |
2 | DATA | Serial data output |
3 | NC | Not connected |
4 | GND | Ground |
Powering the Sensor: Connect the VCC pin to a 3.3V or 5V power supply. The GND pin should be connected to the ground on your microcontroller board.
Data Connection: Connect the DATA pin to a digital I/O pin on your microcontroller.
Pull-up Resistor: A pull-up resistor (typically 4.7kΩ to 10kΩ) is required on the DATA pin to pull it up to the level of VCC.
#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 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: Can the DHT11 sensor measure temperature below 0°C? A: No, the DHT11 is limited to a temperature range of 0 to 50°C.
Q: How long should I wait between readings? A: Wait at least 2 seconds between readings to ensure accurate measurements.
Q: Can I use the DHT11 sensor with a Raspberry Pi? A: Yes, the DHT11 can be used with a Raspberry Pi using a suitable library for interfacing.
Q: Is the DHT11 sensor waterproof? A: No, the DHT11 is not waterproof and should be protected from moisture and condensation.