18. DHT11 Temperature & Humidity
Use a library-based digital environmental sensor.
Learning outcomes
- Describe relative humidity and temperature sensing
- Distinguish bare sensor and module pinouts
- Install and use the Adafruit DHT library
- Detect failed readings
Parts and preparation
Uno, DHT11 sensor/module, 4.7-10 k ohm pull-up for a bare sensor and jumpers. Install both Adafruit libraries listed in the Libraries box.
Before power: inspect wiring, confirm supply voltage and ensure all connected circuits share GND.
Libraries for this lesson
In Arduino IDE 2 open Tools → Manage Libraries…. Search by the Library Manager name and install the package by the exact author below. Similar names from other authors can use different APIs and will break the example.
| Include | Library Manager name | Author | Install note |
|---|---|---|---|
DHT.h | DHT sensor library | Adafruit | Install via Library Manager. |
(dependency) | Adafruit Unified Sensor | Adafruit | Required dependency of the DHT sensor library. Install via Library Manager if prompted or if compile fails. |
Sensor
The DHT11 combines a humidity element, thermistor and internal controller. It is inexpensive but has modest accuracy and slow updates.
Timing
Wait at least about two seconds between readings. Reading too often can return old or failed data.
Pinouts
A bare four-pin DHT11 commonly uses VCC, DATA, NC, GND viewed from the front; three-pin modules vary. Verify printed labels/datasheet.
Libraries
Install DHT sensor library by Adafruit and its Adafruit Unified Sensor dependency through Library Manager.
Wiring and safe build sequence

- VCC -> 5 V
- GND -> GND
- DATA -> D2
- Bare sensor: 4.7-10 k ohm from DATA to VCC
- Pin 3 of a common bare four-pin package is not connected
Worked sketch
Download .ino sketch#include <DHT.h>
const byte dhtPin = 2;
DHT dht(dhtPin, DHT11);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000);
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("DHT read failed");
return;
}
Serial.print(temperature, 1);
Serial.print(" C ");
Serial.print(humidity, 1);
Serial.println(" %RH");
}How the code works
- isnan detects the librarys failed floating-point reading.
- The two-second delay respects sensor timing.
Test and record evidence
Practical evidence checklist
Common faults and checks
- Verify sensor orientation and module pin labels.
- Install both required Adafruit libraries.
- Add/check the data pull-up for a bare sensor.
Check your understanding
Q1. Why check isnan?
Show answer
A failed DHT read returns a not-a-number value.
Q2. How frequently should a DHT11 be read?
Show answer
About once every two seconds or slower.