Arduino library 路 Sensors

DHT sensor library: read a DHT11 or DHT22 on one pin

The DHT sensor library from Adafruit reads the blue DHT11 and the white DHT22 (also sold as the AM2302), two cheap temperature and humidity sensors that talk over a single data pin with a timing protocol of their own. It is not I2C and not 1-Wire: the board pulls the line low, the sensor answers with 40 bits whose meaning is in the length of each high pulse, and the library times them.

Built in on Mokxi: #include <DHT.h> and it builds, with nothing to install. Free, and no account needed.

What DHT sensor library does

You tell the library which sensor you have when you create the object, because the two chips pack their bytes differently. Pick the wrong type and you get numbers that look plausible but are wrong, such as a humidity of 1 percent or a temperature stuck at a round value.

Both sensors are slow. The DHT11 gives a new reading about once a second and the DHT22 every two seconds; ask more often and the library hands back the last reading it took. A failed read returns NaN rather than zero, so every sketch should test for it before printing or acting on the value.

Library Manager nameDHT sensor library
AuthorAdafruit
Include#include <DHT.h>
#include <DHT_U.h>
Boardsevery board Mokxi compiles for
In the simulatorSimulated: it runs against the part the way it does on the bench.
Parts it drivesTemperature and humidity, DHT22

Install it

It builds on every board Mokxi compiles for.

In Mokxi it is built in: #include <DHT.h> and the sketch compiles, with no download, no zip and no Library Manager.

On a real board, install "DHT sensor library" by Adafruit from the Arduino IDE鈥檚 Library Manager (Sketch, Include Library, Manage Libraries), or add DHT sensor library to lib_deps in PlatformIO. Mokxi follows the calls of release 1.4.6, so a sketch written here builds there too.

ESP32-C3 路 Arduino Uno 路 Arduino Nano 路 Arduino Mega 2560 路 Arduino Leonardo 路 SparkFun Pro Micro 路 Arduino Micro 路 ESP32-C6 路 ATtiny85 路 Raspberry Pi Pico 路 Raspberry Pi Pico W 路 Seeed XIAO SAMD21 路 STM32 路 STM32 Nucleo-F411RE 路 STM32 Blue Pill 路 micro:bit V2 路 Arduino Uno R4 路 ESP32 DevKit 路 ESP32-S3-DevKitC-1 路 ESP8266 NodeMCU 路 Arduino Pro Mini 3.3 V 路 Seeed XIAO ESP32-C3 路 ESP32-C3 SuperMini 路 Seeed XIAO ESP32-C6 路 ESP32-DevKitC V4 路 LOLIN32 Lite 路 Wemos D1 R32 路 Cheap Yellow Display 路 TTGO T-Display 路 Seeed XIAO ESP32-S3 路 Arduino Nano ESP32 路 Arduino Nano RP2040 Connect 路 Seeed XIAO RP2040 路 Waveshare RP2040-Zero 路 Arduino Uno R4 WiFi

The calls you will use

In the order a sketch meets them.

DHT dht(2, DHT22)

Creates the sensor on data pin 2. The second argument is DHT11, DHT22, DHT21 or AM2302, and must match the part in your hand.

dht.begin()

Sets the pin up and starts the timer that spaces readings out. Call it once in setup().

float h = dht.readHumidity()

Relative humidity in percent, or NaN if the sensor did not answer.

float t = dht.readTemperature()

Temperature in degrees Celsius. readTemperature(true) gives Fahrenheit.

float t = dht.readTemperature();
if (isnan(t)) {
  Serial.println(F("Failed to read from DHT sensor!"));
  return;
}

dht.computeHeatIndex(t, h, false)

The heat index (how hot it feels) from a temperature and a humidity. The last argument is false when the temperature is in Celsius.

dht.convertCtoF(t)

Converts a Celsius reading to Fahrenheit, with convertFtoC() for the other way.

Example: A DHT read with Mark Ruys鈥檚 DHT library, the model found by itself

On the Arduino Uno R3. It runs in the browser exactly as printed.

sketch.ino 路 Arduino Uno R3
#include "DHT.h"

DHT dht;

void setup() {
  Serial.begin(9600);
  dht.setup(2);  // the model is found from the first reading
  Serial.println(dht.getModel() == DHT::DHT11 ? "DHT11" : "DHT22");
  Serial.println("Status	Humidity (%)	Temperature (C)");
}

void loop() {
  delay(dht.getMinimumSamplingPeriod());
  float humidity = dht.getHumidity();
  float temperature = dht.getTemperature();
  Serial.print(dht.getStatusString());
  Serial.print("	");
  Serial.print(humidity, 1);
  Serial.print("		");
  Serial.println(temperature, 1);
}

Wiring

  • Arduino Uno R3 2 to Temperature and humidity, DHT22 DATA
  • Arduino Uno R3 5V to Temperature and humidity, DHT22 VCC
  • Arduino Uno R3 GND to Temperature and humidity, DHT22 GND

Open it in the editor, wired and ready to run

Common errors and how to fix them

Failed to read from DHT sensor!

This is the sketch鈥檚 own message when a read came back NaN. Check the data pin number matches the wire, that the sensor has power and ground, and that a bare four-pin sensor has a 10 k pull-up from data to VCC (the three-pin modules have one already).

Humidity reads 1 or 0 percent, or the temperature is far off

The type in the constructor is wrong. A blue sensor is almost always a DHT11 and a white one a DHT22 or AM2302; change DHT22 to DHT11 (or the other way) and upload again.

fatal error: Adafruit_Sensor.h: No such file or directory

On a real board the DHT library needs the Adafruit Unified Sensor library beside it. Install it from the Library Manager; recent IDE versions offer to install it when you add the DHT library.

Readings repeat and never change

The loop asks faster than the sensor measures, so the library keeps returning its last reading. Put at least delay(2000) between reads for a DHT22, or time them with millis().

Try DHT sensor library without installing anything

Open the example, change the sketch, and keep your version in a free account.