Unit D - Sensors & Buses

18. DHT11 Temperature & Humidity

Use a library-based digital environmental sensor.

Estimated time 3 hours

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.

IncludeLibrary Manager nameAuthorInstall note
DHT.hDHT sensor libraryAdafruitInstall via Library Manager.
(dependency)Adafruit Unified SensorAdafruitRequired dependency of the DHT sensor library. Install via Library Manager if prompted or if compile fails.
DHT11 Temperature & Humidity instructional connection diagram
Open the diagram for a larger view. Trace every connection before building.

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

Breadboard wiring for lesson 18: DHT11 Temperature & Humidity
Breadboard layout for this lesson. Match colours and pins before powering the circuit. Click the image for a larger view.
  1. VCC -> 5 V
  2. GND -> GND
  3. DATA -> D2
  4. Bare sensor: 4.7-10 k ohm from DATA to VCC
  5. Pin 3 of a common bare four-pin package is not connected
Power rule: switch off before moving wires. Arduino I/O pins are control signals; high-current loads require a driver and suitable external supply.
#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

  1. isnan detects the librarys failed floating-point reading.
  2. The two-second delay respects sensor timing.

Test and record evidence

Expected result: Serial reports plausible temperature and relative humidity every two seconds.

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.
Extension challenge: Display both values on the I2C LCD and retain the previous display when a read fails.

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.