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

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

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.

  • DHT11: The DHT11 is an entry-level sensor, known for being inexpensive. It has a limited accuracy of ±2°C for temperature and ±5% for humidity. Its temperature range is 0°C to 50°C, making it suitable for simple indoor projects.
  • DHT22: The DHT22 is a more advanced sensor with higher accuracy (±0.5°C for temperature and ±2-5% for humidity). It covers a wider temperature range from -40°C to 80°C, making it more versatile and ideal for more precise projects, but it comes at a higher price.

Other Sensors

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

  • BME280: This sensor not only measures temperature and humidity but also pressure, making it great for advanced weather stations.
BME280-sensor-arduino
BME280 Sensor
  • AM2302: This sensor is essentially the same as the DHT22 but often comes in a more robust, waterproof package, perfect for outdoor use.
AM2302-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:

  • A humidity sensing element (a pair of electrodes with a moisture-absorbing substrate between them).
  • A thermistor to detect temperature.
  • A signal processing unit that converts the raw data into a readable digital signal for Arduino to interpret.

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:

  • VCC: Connect to the 5V pin on the Arduino.
  • GND: Connect to a GND pin on the Arduino.
  • Data Pin: Connect to digital pin 2 on the Arduino.
dht11-sensor-wiring-with-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:

  • DHT library: Manages communication with the sensor and reads data.
  • dht.readTemperature() and dht.readHumidity(): Functions to get temperature and humidity values from the sensor.
  • LED Control: The LED will light up if the temperature is above 30°C or if the humidity is below 40%, indicating the conditions have exceeded the set limits.

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.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *