Site icon Mechatronics Learning

How to Use Temperature and Humidity Sensor with Arduino: Beginner’s Guide

get-started-with-arduino-for-beginners-lesson-10

Introduction

Measuring environmental conditions like temperature and humidity is crucial in many applications. In this guide, we will explore how to use temperature and humidity sensors with Arduino, focusing on the popular DHT11 and DHT22 sensors, and providing a step-by-step approach to wiring, coding, and reading values.

What are Temperature and Humidity Sensors?

Temperature and humidity sensors are electronic devices that measure two key environmental variables: temperature (in Celsius or Fahrenheit) and humidity (percentage of moisture in the air). These sensors play a vital role in projects like weather stations, home climate control systems, and greenhouse monitoring, providing critical data for decision-making and automation.

DHT11-Temperature-and-Relative-Humidity-Sensor-Module-NTC
DHT11 Temperature and Humidity Sensor Module

Types of Temperature and Humidity Sensors

Focus on DHT11 and DHT22

Two of the most popular temperature and humidity sensors used with Arduino are the DHT11 and DHT22. These sensors are easy to use, affordable, and supported by a wide range of Arduino libraries.

Other Sensors

While the DHT11 and DHT22 are popular choices, there are other sensors worth mentioning:

BME280-sensor-arduino
BME280 Sensor
AM2302 Sensor

How Sensor Works

The DHT11 and DHT22 use a capacitive humidity sensor and a thermistor to measure the surrounding air. The capacitive humidity sensor measures the moisture level by detecting changes in electrical resistance as the humidity varies. The thermistor measures temperature by changing its resistance in response to temperature changes. These measurements are then processed and output as digital data.

Internal Structure of DHT Sensors

The DHT11/DHT22 sensors consist of:

To demonstrate how to read temperature and humidity values, we will wire up the DHT sensor to an Arduino and control an LED based on certain conditions. For instance, if the temperature exceeds 30°C or the humidity drops below 40%, we can turn on an LED to indicate the values have reached a threshold.

Wiring DHT11 Sensor with Arduino

Wiring Diagram

Here’s how to wire the DHT11 or DHT22 sensor to an Arduino:

DHT11 sensor wiring with Arduino

Arduino Code

We will use the DHT library to read the values from the sensor and control the LED based on temperature or humidity conditions.

#include <DHT.h>

#define DHTPIN 2     // DHT sensor connected to digital pin 2
#define DHTTYPE DHT11   // Change to DHT22 if using DHT22
#define ledPin 13    // Built-in LED pin

DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(ledPin, OUTPUT);
  Serial.begin(9600);
  dht.begin();
}

void loop() {
  float temp = dht.readTemperature(); // Read temperature
  float hum = dht.readHumidity();     // Read humidity
  
  // Check if the reading failed
  if (isnan(temp) || isnan(hum)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }

  Serial.print("Temperature: ");
  Serial.print(temp);
  Serial.print(" °C, Humidity: ");
  Serial.print(hum);
  Serial.println(" %");

  // Turn on the LED if temp > 30°C or humidity < 40%
  if (temp > 30 || hum < 40) {
    digitalWrite(ledPin, HIGH); // Turn on LED
  } else {
    digitalWrite(ledPin, LOW);  // Turn off LED
  }

  delay(2000);  // Wait 2 seconds before the next reading
}

Explanation of the Code:

Conclusion

Temperature and humidity sensors like the DHT11 and DHT22 are great starting points for beginners working with Arduino. These sensors are simple to use and offer valuable data that can be applied to weather stations, home automation systems, or environmental monitoring. As you progress, you can explore more advanced sensors like the BME280 for additional capabilities like pressure measurement or integrate IoT technologies to monitor environmental conditions remotely. This project opens the door to countless future projects, including data logging, smart home integration, and climate control systems.

Exit mobile version