DHT11 and DHT22 - Measure temperature and humidity

FunduinoPublished on

Zwei Sensoren im Vergleich – Messbereiche, Verkabelung und Sketch

DHT11 Temperatur- und Luftfeuchtigkeitssensor am Arduino Mikrocontroller

Mit den Sensoren DHT11 und DHT22 misst du am Arduino Luftfeuchtigkeit und Temperatur. Die Messwerte kannst du dir über den seriellen Monitor oder ein LCD anzeigen lassen. Der DHT11 misst Luftfeuchtigkeit von etwa 20 bis 80 % (5 % Genauigkeit) und Temperatur von etwa 0 bis 50 °C (etwa 2 °C Genauigkeit).

Aufgabe: Miss Luftfeuchtigkeit und Temperatur mit dem DHT11 und lass sie dir auf dem seriellen Monitor anzeigen.

You need this

DHT11 temperature sensor - humidity, 3.3-5.5 V, without breakout board DHT11 temperature sensor - humidity, 3.3-5.5 V, without breakout board 2,32 € *Item no. F23106840About the product — DHT11 temperature sensor - humidity, 3.3-5.5 V, without breakout board
DHT22 temperature and humidity sensor - without board, 3.3-5.5 V DHT22 temperature and humidity sensor - without board, 3.3-5.5 V 6,59 € *Item no. F23106799About the product — DHT22 temperature and humidity sensor - without board, 3.3-5.5 V
Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino 11,12 € *Item no. F23107480About the product — Funduino UNO R3 Microcontroller - ATmega328P, 16 MHz, for Arduino
830-point Breadboard - solderless breadboard for Arduino 830-point Breadboard - solderless breadboard for Arduino 2,90 € *Item no. FUN-1010553About the product — 830-point Breadboard - solderless breadboard for Arduino
Breadboard Cables Male-to-Male - 40 Pieces, 20 cm Breadboard Cables Male-to-Male - 40 Pieces, 20 cm 1,74 € *Item no. FUN-1010558About the product — Breadboard Cables Male-to-Male - 40 Pieces, 20 cm

All 5 parts together 24,67 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

The circuit diagram (DHT11 on the Arduino UNO)

In our example, the DHT11’s four contacts are combined into three contacts on a small board. For DHT11 sensors without this board, follow the wiring instructions in the DHT22 section.

With the front of the DHT11 facing you, connect the left contact to pin 2, the middle contact to 5V, and the right contact to GND on the microcontroller.

Schematic: DHT11 sensor connected to pin 2 of the Arduino UNO
DHT11 sensor on the Funduino UNO R3 (old version)

The program code (DHT11 on the Arduino UNO)

You need the DHT sensor library by Adafruit for the code. You can find it using the “Include Library” function in the Arduino software.

If you then open the Serial Monitor, it displays the readings one after another.

For the DHT11, you need two libraries: DHT-sensor-library and Adafruit_Sensor, both from Adafruit on GitHub.

DHT11 on Arduino UNO
#include <DHT.h>
DHT dht(2, DHT11); // DHT11 on pin 2
void setup() {
  Serial.begin(9600);
  dht.begin();
}
void loop() {
  float t = dht.readTemperature(); // Read temperature
  Serial.print("Temperature: ");
  Serial.print(t);
  Serial.println(" °C");
  delay(2000); // 2-second pause
}

The DHT22 on the Arduino microcontroller

You can also measure humidity and temperature with the DHT22. This sensor is a little more accurate and has wider measurement ranges: humidity from 0 to 100% (2 to 5% accuracy), temperature from -40 to 125 °C (0.5 °C accuracy).

Task: Measure humidity and temperature with the DHT22 and display them in the serial monitor.

The wiring (DHT22 on the Arduino UNO)

A 10 kΩ resistor should be connected between 5V and the data pin.

Circuit diagram: DHT22 sensor with pull-up resistor connected to an Arduino UNO
Wiring the DHT22 to the Arduino UNO

The program code (DHT22 on Arduino UNO)

Here too, you need Adafruit’s DHT sensor library. You can find it using the “Include Library” function in the Arduino software.

For the DHT22, you need the same two libraries as for the DHT11.

DHT22 on Arduino UNO
#include <DHT.h> // Include the DHT library
#define DHTPIN 2     // DHT22 data pin on pin D2
#define DHTTYPE DHT22 // Sensor type: DHT22
DHT dht(DHTPIN, DHTTYPE); // Create the sensor object
void setup() {
  Serial.begin(9600);    // Start the serial interface
  dht.begin();           // Start the DHT sensor
  Serial.println("Starting measurement with DHT22…");
}
void loop() {
  delay(2000); // Wait 2 seconds (recommended for DHT22)
  float temperatur = dht.readTemperature(); // Read the temperature (in °C)
  Serial.print("Temperature: ");
  Serial.print(temperatur); // Output the value
  Serial.println(" °C");    // Add the unit
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.