Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 19: Temperature and Humidity Sensor Module (DHT11)

In this lesson, you’ll learn how to measure temperature and humidity, as well as calculate the heat index using a DHT11 sensor with an Arduino Uno. We’ll cover reading and interpreting data from the DHT11 sensor, and displaying these values along with the heat index in both Celsius and Fahrenheit on the serial monitor. This project is perfect for Arduino beginners, providing hands-on experience with sensors and data handling in a simple yet engaging way.

Required Components

In this project, we need the following components.

It’s definitely convenient to buy a whole kit, here’s the link:

Name

ITEMS IN THIS KIT

LINK

Universal Maker Sensor Kit

94

Universal Maker Sensor Kit

You can also buy them separately from the links below.

Component Introduction

Purchase Link

Arduino UNO R3 or R4

BUY

Temperature and Humidity Sensor Module (DHT11)

BUY

Wiring

Note

The kit may contain different versions of the DHT11 module. Please confirm the wiring method according to the module you have.

module

diagram

dht11_module

dht11_module_circuit

dht11_module_withLED

dht11_module_withLED_circuit

Code

Note

To install the library, use the Arduino Library Manager and search for “DHT sensor library” and install it.

Code Analysis

  1. Inclusion of necessary libraries and definition of constants. This part of the code includes the DHT sensor library and defines the pin number and sensor type used in this project.

    Note

    To install the library, use the Arduino Library Manager and search for “DHT sensor library” and install it.

    #include <DHT.h>
    #define DHTPIN 2       // Define the pin used to connect the sensor
    #define DHTTYPE DHT11  // Define the sensor type
    
  2. Creation of DHT object. Here we create a DHT object using the defined pin number and sensor type.

    DHT dht(DHTPIN, DHTTYPE);  // Create a DHT object
    
  3. This function is executed once when the Arduino starts. We initialize the serial communication and the DHT sensor in this function.

    void setup() {
      Serial.begin(9600);
      Serial.println(F("DHT11 test!"));
      dht.begin();  // Initialize the DHT sensor
    }
    
  4. Main loop. The loop() function runs continuously after the setup function. Here, we read the humidity and temperature values, calculate the heat index, and print these values to the serial monitor. If the sensor read fails (returns NaN), it prints an error message.

    Note

    The heat index is a way to measure how hot it feels outside by combining the air temperature and the humidity. It is also called the “felt air temperature” or “apparent temperature”.

    void loop() {
      delay(2000);
      float h = dht.readHumidity();
      float t = dht.readTemperature();
      float f = dht.readTemperature(true);
      if (isnan(h) || isnan(t) || isnan(f)) {
        Serial.println(F("Failed to read from DHT sensor!"));
        return;
      }
      float hif = dht.computeHeatIndex(f, h);
      float hic = dht.computeHeatIndex(t, h, false);
      Serial.print(F("Humidity: "));
      Serial.print(h);
      Serial.print(F("%  Temperature: "));
      Serial.print(t);
      Serial.print(F("°C "));
      Serial.print(f);
      Serial.print(F("°F  Heat index: "));
      Serial.print(hic);
      Serial.print(F("°C "));
      Serial.print(hif);
      Serial.println(F("°F"));
    }