The DHT11 is one of the most popular sensors for measuring temperature and humidity using an Arduino. In this guide, you’ll learn how to wire it, read data from it, and use it in your own projects.
What You’ll Need
- Arduino UNO (or compatible)
- DHT11 temperature and humidity sensor
- 10k ohm resistor (optional but recommended)
- Breadboard
- Jumper wires
Wiring the DHT11 to Arduino
Connect the DHT11 sensor to the Arduino as follows:
- VCC → 5V on Arduino
- GND → GND
- DATA → Digital Pin 2
Optional: Place a 10k ohm resistor between VCC and DATA to improve signal stability.
Arduino Code Example
To use the DHT11, you’ll need the DHT sensor library.
1. Install the Library:
In the Arduino IDE, go to:
Search for “DHT sensor library” by Adafruit and install it. Also install “Adafruit Unified Sensor”.
2. Upload the Code:
```cpp
#include "DHT.h"
#define DHTPIN 2 // Digital pin connected to the DHT sensor
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float temp = dht.readTemperature();
float humidity = dht.readHumidity();
if (isnan(temp) || isnan(humidity)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print(" °C | Humidity: ");
Serial.print(humidity);
Serial.println(" %");
}
Troubleshooting Tips
If you’re having issues with your DHT11 sensor, try the following:
- ✅ Double-check the wiring: VCC to 5V, GND to GND, DATA to digital pin 2
- ✅ Use a 10k ohm pull-up resistor between VCC and DATA
- ✅ Make sure you’ve installed the correct libraries (DHT and Adafruit Unified Sensor)
- ✅ Try a different pin if pin 2 doesn’t work
- ✅ Upload the sketch again after verifying the board and COM port are correct
If the serial monitor shows:
“Failed to read from DHT sensor!”
…it usually means the wiring or timing is off.
