[EN] Arduino : DHT Sensor

This article describes how Adafruit’s DHT Sensor library is compatible with all Arduino-compatible architectures, making it easier to deploy DHT sensors for humidity and temperature readings across multiple platforms. This article has tested with ESP32, ESP8266, Arduino UNO and stm32f103c and found that it can be used without modifying the code in the working part or having to modify the source code to make it compatible with the platform.

(Figure. 1 dht22 and BluePill)

DHT Sensor

The DHT Sensor is a temperature and humidity sensor that is commonly used as a basic device in embedded and IoT learning. There are two types of sensors of this type, called DHT11 and DHT22, which differ in functionality. But the connection pins are the same, namely pins for power supply, ground pins and data pins.

Installation

To install libraries from Sketch menu, select Include Library, then select Manage Libraries … a window for searching and upgrading libraries will be displayed. Type DHT to find Adafruit libraries as shown in Figure 2. After that, install and close the page when finished.

(Figure. 2 Library Manager)

You can use the DHT library by importing a header file:

#include <DHT.h>

In order to create object variables to access the sensor, two things must be defined: the pin connected to the sensor’s data pin and the type of sensor it is in contact with which must be set to DHT11 or DHT22 to match the selected device. So the command line for creating a sensor object variable is as follows:

DHT variable = DHT( pin, type )

Instructions

Once the DHT sensor object variable has been created, The next thing to know is a set of instructions for using the DHT sensor library, which includes the following commands.

Start the program

The initialization command has the following form:

variable.begin();

Read temperature

Temperature readings in degrees Celsius have the following usage patterns.


float c = variable.readTemperature();

For Fahrenheit, you can use the command read temperature and add the true argument to the command:

  float f = variable.readTemperature(true);

Read humidity

The command for reading the percentage of moisture has the following form.

  float humidity = variable.readHumidity();

Calculate temperature indicator

The command for bringing the temperature value to calculate with the humidity value to be a measured temperature or Heat Index in degrees Celsius has the following usage patterns:

  float heatIndex = variable.computeHeatIndex(c, humidity, false);

If you want the unit of measure in Fahrenheit, use the following command.

  Float fHeadIndex = variable.computeHeatIndex(f, humidity);

Testing

This example is to read temperature and humidity from DHT22 sensor to display on I2C LCD (usage can be read in this article) with Blue Pill which is STM32F103 that is the same size as Arduino Nano and connect signal pin PA1 of DHT  (Installation of Arduino IDE for STM32, read from this article), which the program consists of

  • Displays first screen.
  • Displays the temperature in degrees Celsius, degrees Fahrenheit and humidity.
  • Displays head index.

An example of the operation is shown in Figures 2, 3 and 4, and the program code is as follows.

////////////////////////////////////////////////////////
// demoDHT
// chip: stm32f103
// board: stm32-station lab
// (C) 2020-2021, JarutEx
/////////////////////////////////////////////////////////
#include <DHT.h>
#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 16, 2);
DHT dht = DHT(PA1, DHT22);

void setup() {
  Serial.begin(115200);
  dht.begin();
  lcd.init();
  lcd.backlight();
}

void loop() {
  float h = dht.readHumidity();
  float tc = dht.readTemperature();
  float tf = dht.readTemperature(true);

  if (isnan(h) || isnan(tc) || isnan(tf)) {
    lcd.setCursor(0, 1);
    lcd.print("DHTsensor Failed");
    return;
  }

  float hic = dht.computeHeatIndex(tc, h, false);
  float hif = dht.computeHeatIndex(tf, h);

  lcd.setCursor(0, 0);
  lcd.print("STM32-StationLab");
  lcd.setCursor(0, 1);
  lcd.print(" www.jarutex.com");
  delay(2000);

  lcd.setCursor(0, 0);
  lcd.print("T:      C      F");
  lcd.setCursor(3, 0);
  lcd.print(tc);
  lcd.setCursor(10, 0);
  lcd.print(tf);
  lcd.setCursor(0, 1);
  lcd.print("Humidity:     % ");
  lcd.setCursor(9, 1);
  lcd.print(h);
  delay(5000);

  lcd.setCursor(0, 0);
  lcd.print("   Heat index   ");
  lcd.setCursor(0, 1);
  lcd.print("T:      C      F");
  lcd.setCursor(3, 1);
  lcd.print(hic);
  lcd.setCursor(10, 1);
  lcd.print(hif);
  delay(5000);
}
(Figure. 3 Displays first screen.)
(Figure. 4 Displays temperature and humidity.)
(Figure. 5 Displays Heat Index.)

An example for Arduino Mega is as follows: connect dht11 to pin D13 as shown in Figure 5 and the result shown in Figure 6.

// Arduino Mega2560

#include <DHT.h>

DHT dht = DHT(13, DHT11);

void setup() {
  Serial.begin(115200);
  dht.begin();
}

void loop() {

  float h = dht.readHumidity();
  float tc = dht.readTemperature();
  float tf = dht.readTemperature(true);

  if (isnan(h) || isnan(tc) || isnan(tf)) {
    Serial.print("DHTsensor Failed");
    return;
  }

  float hic = dht.computeHeatIndex(tc, h, false);
  float hif = dht.computeHeatIndex(tf, h);

  Serial.print("T: ");
  Serial.print(tc);
  Serial.print("C/");
  Serial.print(tf);
  Serial.print("Humidity: ");
  Serial.print(h);
  Serial.print("%, ");
  Serial.print("   Heat index   ");
  Serial.print(hic);
  Serial.print("C/");
  Serial.print(hif);
  Serial.println("F");
  delay(5000);
}
(Figure. 6 DHT11 connected with d13 of Arduino Mega 2560.)
(Figure. 7 Result of Arduino Mega2560)

Conclusion

From this article, you will find that the DHT library makes programming easier to use and does not depend on the processor. It only works with the Arduino framework. However, readings from the sensors require good readability of the DHT11 and DHT22, as each sensor type takes different amount of time to read and wait for stable values. Finally, have fun with programming.

If you want to talk with us, feel free to leave comments below!!

(C) 2020-2021, By Jarut Busarathid and Danai Jedsadathitikul
Updated 2021-10-22