How to Use the DHT11 Temperature and Humidity Sensor with Arduino Uno
A weather station on your roof, a room monitor on your study table, a greenhouse that waters itself when the air gets dry. Every one of these projects starts with the same small blue sensor: the DHT11. It measures temperature and humidity with a single part and a single wire, and it costs less than a samosa plate.
In this guide, you will learn how to use the DHT11 sensor with Arduino Uno to build a room climate monitor. You will wire the sensor, install the DHT library, and write code that reads temperature and humidity every two seconds, prints the values to the Serial Monitor, and lights an LED when the room gets hotter than 30 °C. You will also learn how the DHT11 differs from its more accurate brother, the DHT22, so you can pick the right sensor for your own project.
Components Required
| Part | Quantity | Purpose | Price |
|---|---|---|---|
| DHT11 Temperature & Humidity Sensor | 1 | Measures temperature and humidity | Rs 215 |
| Arduino Uno R3 DIP | 1 | Reads the sensor and runs the code | Rs 1,200 |
| MB102 solderless breadboard, 830 points | 1 | Holds the sensor and LED without soldering | Rs 250 |
| 40-pin male-to-male jumper wires | 5 | Connects the sensor and LED to the Uno | Rs 180 |
| 5 mm LED (any colour) | 1 | Lights up when the room is hotter than 30 °C | Rs 6 |
| 220 ohm resistor | 1 | Protects the LED from too much current | Available on order |
| USB cable (type A to B) | 1 | Powers the Uno and uploads the code | — |
How the DHT11 Temperature and Humidity Sensor Works
Inside the small blue plastic case of the DHT11 there are three things: a capacitive humidity sensing element, an NTC thermistor that reacts to temperature, and a tiny chip that turns both readings into digital numbers. You never deal with analog voltages here. The sensor talks to the Arduino over a single digital wire, which is why the wiring is so simple.
A reading works like this. The Arduino pulls the DATA line LOW for at least 18 milliseconds to wake the sensor up. The sensor answers, then sends 40 bits: humidity, temperature, and a checksum byte. The library checks the checksum, and if it does not match, the reading is thrown away and you get a NaN instead of a number.
Two limits matter. First, the DHT11 is slow: at most one reading per second, so our code waits two seconds. Second, its resolution is 1 °C and 1 % RH, so the decimal part is always zero. A reading of 27 °C and 54 % is all the detail you get — fine for a room monitor or a greenhouse alarm, but not for a laboratory.
The full numbers are in the DHT11/DHT22 datasheet.
DHT11 vs DHT22: which one should you use?
The DHT22 (also sold as the AM2302) looks almost the same and uses the same wiring and the same library. The difference is inside: better sensing elements. Use this table to choose.
| Feature | DHT11 | DHT22 (AM2302) |
|---|---|---|
| Temperature range | 0 to 50 °C | −40 to 80 °C |
| Temperature accuracy | ±2 °C | ±0.5 °C |
| Humidity range | 20 to 90 % RH | 0 to 100 % RH |
| Humidity accuracy | ±5 % RH | ±2 to 5 % RH |
| Resolution | 1 °C, 1 % RH | 0.1 °C, 0.1 % RH |
| Fastest reading | once per second | once every 2 seconds |
| Power | 3 to 5 V, 2.5 mA max | 3 to 5 V, 2.5 mA max |
| Price at QKZee Technologies | Rs 215 | Available on order |
Rule of thumb: the DHT11 is enough for room monitoring, weather stations and most student projects. Pick the DHT22 when you need decimal readings, freezing temperatures, or accuracy better than ±2 °C. The code works for both — you only change one word, DHT11 to DHT22.
Key specifications of the DHT11:
| Specification | Value |
|---|---|
| Operating voltage | 3 to 5 V DC |
| Working current | 2.5 mA max during conversion |
| Temperature range | 0 to 50 °C, ±2 °C accuracy |
| Humidity range | 20 to 90 % RH, ±5 % RH accuracy |
| Resolution | 1 °C, 1 % RH |
| Sampling rate | 1 Hz max (once per second) |
| Interface | Single-wire digital |
| Size | 15.5 x 12 x 5.5 mm |
DHT11 Pinout and Wiring with Arduino Uno
The ready-made DHT11 module sold at QKZee Technologies has three pins. That is the version used in this guide.
| DHT11 module pin | Arduino Uno pin | Wire colour used here |
|---|---|---|
| VCC | 5V | Red |
| DATA | D2 | Yellow |
| GND | GND | Black |
Connect the LED as well. Push the LED’s long leg (anode) into one row of the breadboard and the short leg (cathode) into another row. Connect a 220 ohm resistor from the short leg’s row to the Uno GND pin. Connect a jumper wire from the long leg’s row to Uno pin D13.
Two notes before you power on. First, the bare 4-pin DHT11 sensor (not the module) has a different pinout: pin 1 is VCC, pin 2 is DATA, pin 3 is not connected, and pin 4 is GND. The bare sensor also needs a 10k ohm pull-up resistor between DATA and VCC, or it will not answer at all. The ready-made module already has that resistor on its small circuit board. Second, the sensor must get 5 V from the Uno. It will also run on 3.3 V, but 5 V gives the most stable readings.

Library Installation
- In the Arduino IDE, open Sketch > Include Library > Manage Libraries.
- Search for “DHT sensor library” and install the one by Adafruit.
- Search for “Adafruit Unified Sensor” and install it as well. The DHT library needs it, and the IDE will usually offer to install it as a dependency. Accept it.
- Restart the IDE if the examples do not appear.
Complete Arduino Code
/*
DHT11 Room Climate Monitor with Arduino Uno
Reads temperature and humidity every 2 seconds, prints them
to the Serial Monitor, and lights the LED on pin 13 when the
room gets hotter than 30 degrees C.
Needs the Adafruit "DHT sensor library" and
"Adafruit Unified Sensor" libraries.
To use a DHT22 instead, change DHT11 to DHT22 below.
*/
#include <Adafruit_Sensor.h>
#include <DHT.h>
#define DHT_PIN 2 // DHT11 DATA pin
#define DHT_TYPE DHT11 // Change to DHT22 for a DHT22 sensor
#define LED_PIN 13 // Built-in LED on the Uno
const float HOT_ALERT_C = 30.0; // LED on above this
const unsigned long READ_INTERVAL_MS = 2000; // DHT11: max 1 reading/sec
DHT dht(DHT_PIN, DHT_TYPE);
unsigned long lastReadTime = 0;
void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);
dht.begin();
Serial.println("DHT11 Room Climate Monitor ready");
Serial.println("Temp (C) | Humidity (%) | Heat Index (C)");
}
void loop() {
unsigned long now = millis();
// millis() timing: read on a schedule, never with delay().
if (now - lastReadTime >= READ_INTERVAL_MS) {
lastReadTime = now;
float humidity = dht.readHumidity();
float tempC = dht.readTemperature(); // Celsius by default
// A failed reading returns NaN ("Not a Number").
// Check for it, or the monitor will print nonsense.
if (isnan(humidity) || isnan(tempC)) {
Serial.println("Failed to read from DHT sensor!");
digitalWrite(LED_PIN, LOW);
return;
}
float heatIndex = dht.computeHeatIndex(tempC, humidity, false);
Serial.print(tempC, 1);
Serial.print(" C | ");
Serial.print(humidity, 0);
Serial.print(" % | ");
Serial.print(heatIndex, 1);
Serial.println(" C");
// LED on when the room is hotter than the alert limit.
if (tempC > HOT_ALERT_C) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
}
}Code Explanation
Includes at the top. Adafruit_Sensor.h is the unified sensor base the DHT library depends on, and DHT.h is the sensor library itself. Without both, the code will not compile.
Constants. DHT_PIN is 2, matching the wiring table. DHT_TYPE is DHT11. To use a DHT22, change this one word to DHT22 and nothing else. HOT_ALERT_C is 30.0, the temperature that turns the LED on. READ_INTERVAL_MS is 2000. Two seconds is safely above the DHT11’s one-reading-per-second limit, and it is also slow enough for a DHT22, which needs two seconds.
DHT dht(DHT_PIN, DHT_TYPE) creates the sensor object. dht.begin() in setup() starts it. The Serial Monitor header line prints once, so the columns of numbers below it are easy to read.
loop() runs on a millis() schedule, not on delay(). Every two seconds it calls readHumidity() and readTemperature(). Both return a float, or NaN when the reading fails. The isnan() check catches failures. Without it, a bad reading prints as “nan” and the LED logic would misbehave. A failed reading is normal once in a while, especially just after power-on. The code prints a warning and tries again at the next interval.
computeHeatIndex() combines temperature and humidity into how hot the air feels. The third argument, false, means Celsius. The LED logic is a plain comparison: hotter than 30 °C, LED on.
Demonstration: Expected Serial Monitor Output
If you have never uploaded code before, see our guide on how to upload code in Arduino Uno.
Upload the code, then open the Serial Monitor with Tools > Serial Monitor and set the baud rate to 9600. After the first two seconds you should see output like this:
DHT11 Room Climate Monitor ready
Temp (C) | Humidity (%) | Heat Index (C)
27.0 C | 54 % | 27.8 C
27.0 C | 54 % | 27.8 C
28.0 C | 52 % | 28.3 C
28.0 C | 53 % | 28.5 CBreathe gently on the sensor and watch the humidity climb. Hold a warm cup near it (not touching) and the temperature will rise. If the temperature passes 30 °C, the LED lights up. The readings change slowly, one step per two seconds, because the sensor itself is slow. That is normal.
Troubleshooting
- The monitor always shows “Failed to read from DHT sensor!”. This is the most common DHT11 problem, and it is almost always wiring. Check that DATA is on D2 and matches DHT_PIN in the code, VCC is on 5V, and GND is on GND. If you are using the bare 4-pin sensor instead of the module, add the 10k pull-up resistor between DATA and VCC. Also check that DHT_TYPE matches the sensor you bought.
- Readings work, then stop after a few minutes. You are probably reading too fast. The DHT11 needs at least one second between readings, and the DHT22 needs two. Our code waits two seconds, which is safe for both. If you changed READ_INTERVAL_MS to something smaller, put it back to 2000 or more.
- The temperature reads 2 °C higher or lower than another thermometer. That is within the DHT11’s ±2 °C accuracy, so it is not a fault. Keep the sensor away from the Uno’s voltage regulator and out of direct sunlight. Both warm the little blue case and push the reading up.
- Humidity is stuck at 20 % or 90 %. The sensor is at the edge of its range. Below 20 % or above 90 % RH the DHT11 cannot measure reliably, so it sits at the limit. Move it to a normal room and it should start moving again. If it stays stuck, the sensor may be damaged by water or condensation.
- The code does not compile: “DHT.h: No such file or directory”. The DHT sensor library is not installed. Install “DHT sensor library” by Adafruit from the Library Manager, and also install “Adafruit Unified Sensor”. Both are needed.
- The code compiles and uploads, but the Serial Monitor is empty. Check the baud rate in the Serial Monitor. It must be 9600, the same as Serial.begin(9600). Also check that you selected Arduino Uno as the board and the right COM port under Tools. The first reading takes two seconds, so wait before deciding nothing is happening.
Project Ideas and FYP Ideas Using the DHT11
- Room climate monitor with LCD. Add an LCD1602 display with I2C module and show temperature and humidity on it instead of the Serial Monitor. Add a buzzer that sounds when the room passes 35 °C. It is a complete, demonstrable project with three parts and one sensor.
- Smart fan controller. Connect a relay module to the Uno. When the temperature passes 30 °C, the relay switches a small DC fan on. When it falls below 28 °C, the fan switches off. The two-degree gap stops the fan flicking on and off at the boundary.
- Greenhouse monitor. Put the DHT11 inside a small greenhouse or a plant box. Log temperature and humidity to an SD card every ten minutes. At the end of a week you have a real dataset to show in your FYP report, with graphs of day and night changes.
- Cold storage alarm. A DHT22 (not the DHT11, because it must read below zero) inside a freezer or a medicine fridge. If the temperature rises above 8 °C, a buzzer sounds. Pharmacies and labs use exactly this idea, so it is easy to explain to an examiner.
- Weather station with data logging. Combine the DHT11 with a real-time clock module (DS3231) and an SD card module. Record temperature and humidity with the time, once every ten minutes, for a full month. Add a second sensor outside the house and compare indoor and outdoor readings.
Still stuck?
If the build does not work, message us on WhatsApp and we will help. You can also watch our videos on YouTube.
Frequently Asked Questions
Which is better, DHT11 or DHT22?
For most student projects, the DHT11 is enough. It is cheaper and faster (one reading per second). The DHT22 is better when you need decimal readings, temperatures below 0 °C, or accuracy better than ±2 °C. Both use the same wiring and the same library, so you can swap them later by changing one word in the code.
Why does my DHT11 show NaN?
NaN means the sensor did not answer in time. The usual causes are a loose DATA wire, the wrong pin in the code, a missing 10k pull-up resistor on a bare sensor, or reading the sensor too often. Check the wiring first, then check that DHT_PIN and DHT_TYPE match your build.
Can the DHT11 work with an ESP32?
Yes. The DHT library supports the ESP32, and the wiring is the same: DATA to any GPIO pin, VCC to 3.3 V or 5 V, GND to GND. Note that the ESP32 is a 3.3 V board, so power the sensor from 3.3 V. The reading interval rule is the same: at least one second for the DHT11.
How accurate is the DHT11?
Temperature accuracy is ±2 °C over 0 to 50 °C, and humidity accuracy is ±5 % RH over 20 to 90 % RH. Resolution is 1 °C and 1 % RH. That is good enough for room monitoring and weather stations, but not for a laboratory or a medical device.
What is the DHT11 sensor price in Pakistan?
The DHT11 temperature and humidity sensor costs Rs 215 at QKZee Technologies. A complete starter setup with an Arduino Uno, breadboard, jumper wires and LED costs roughly Rs 1,850.
Conclusion
The DHT11 sensor with Arduino is one of the easiest useful builds in electronics. Three wires, one library, and a few lines of code give you a working room climate monitor that reports real numbers every two seconds. You have also learned the sensor’s honest limits: one reading per second, ±2 °C accuracy, and a 20 to 90 % humidity range. Knowing those limits is what separates a working project from a lucky one. Try the LCD version next. It uses the same code you just wrote, with the Serial prints swapped for LCD prints.
Components for this project are available at QKZee Technologies, and smart circuit hub with nationwide Cash on Delivery across Pakistan.
If you missed it, our HC-SR04 ultrasonic sensor guide builds a distance meter the same way.





