The DHT11 is one of the most common beginner sensors for real environmental data — cheap, simple to wire, and good enough for room-temperature projects like a desk weather display or a greenhouse monitor.
Intermediate
⌛ 30 min
🔧 What You'll Need
- Arduino Uno
- 1x DHT11 sensor module
- 1x 10kΩ resistor (if using a bare sensor, not needed on most 3-pin modules)
- Breadboard
- 3x jumper wires
Wiring the Circuit
Most DHT11 modules come on a small board with only 3 pins: VCC to 5V, GND to GND, and OUT (or SIG) to digital pin 2. If you're using a bare 4-pin DHT11 without a breakout board, you'll also need a 10kΩ pull-up resistor between the data pin and VCC.
The Code
#include
#define DHTPIN 2
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000); // DHT11 needs ~2s between readings
float humidity = dht.readHumidity();
float tempC = dht.readTemperature();
if (isnan(humidity) || isnan(tempC)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("% Temperature: ");
Serial.print(tempC);
Serial.println("C");
}
The DHT library handles the sensor's timing-sensitive single-wire protocol for you. dht.readTemperature() and dht.readHumidity() return the two readings as floats. The isnan() check matters more than it looks — the DHT11 fairly often returns a bad reading, and printing garbage data without checking for it is a common beginner mistake.
💡 Tip: You'll need to install the "DHT sensor library" by Adafruit (and its dependency, "Adafruit Unified Sensor") through the Arduino IDE's Library Manager before this sketch will compile.
Troubleshooting
- Constant "Failed to read" errors: DHT11s are genuinely a bit unreliable — confirm wiring first, but also expect the occasional failed read even on a correct circuit.
- Readings never change: Some cheap clones return the same static value when miswired — double check VCC and GND aren't reversed.
- Compile error mentioning DHT.h: The library isn't installed — search "DHT sensor library" in Library Manager, not just "DHT".
Real sensor data is where Arduino projects start feeling genuinely useful rather than just demonstrations. Next: a sensor that measures distance instead of climate.
Menu