Cooling System using DHT Sensor – Arduino Tutorial

3,674 views

Introduction

Controlling a fan based on temperature readings is a crucial aspect in various fields of engineering such as HVAC systems, industrial process control, and home automation. The use of an Arduino microcontroller, a DHT11 humidity and temperature sensor, and a 5V SPDT relay allows for automated control of a 12V DC fan based on the ambient temperature. The DHT11 sensor, connected to the microcontroller, continuously measures the ambient temperature and sends the data to the microcontroller.

The microcontroller, programmed with a pre-determined threshold temperature value, compares the measured temperature to the threshold value. If the measured temperature exceeds the threshold, the microcontroller activates the relay, allowing power to be supplied to the fan, and turning it on. If the temperature falls below the threshold, the microcontroller deactivates the relay, cutting power to the fan, and turning it off.

Hardware Components

You will require the following hardware for Cooling System using DHT Sensor with Arduino.

S.noComponentValueQty
1.Arduino UNO1
2.USB Cable Type A to B1
3.Temperature and Humidity SensorDHT111
4.Relay1
5.Cooling Fan12V1
6.Power Adapter for Arduino9V1
7.Power Adapter12V1
8.DC Power Jack1
9.Jumper Wires1

Cooling System using DHT Sensor with Arduino

  1. Start by including the necessary libraries for the DHT11 sensor and the Serial monitor at the beginning of your code:
#include <DHT.h> // DHT library
#define DHTTYPE DHT11 // DHT 11
DHT dht(2, DHTTYPE); // Connect DHT sensor to digital pin 2
#include <SoftwareSerial.h> // SoftwareSerial library
  1. Define the pin number for the relay and the threshold temperature value in your code:
const int relayPin = 3; // Connect relay to digital pin 3
const float thresholdTemp = 25; // Threshold temperature value in Celsius
  1. In the setup() function, initialize the serial communication and set the relay pin as an output:
void setup() {
  Serial.begin(9600); // Initialize serial communication
  pinMode(relayPin, OUTPUT); // Set relay pin as output
  dht.begin();
}
  1. In the loop() function, read the temperature value from the DHT11 sensor and compare it to the threshold temperature value. If the temperature is above the threshold, turn on the relay and print the status on the serial monitor. If the temperature is below the threshold, turn off the relay and print the status on the serial monitor:
void loop() {
  float temperature = dht.readTemperature(); // Read temperature value
  if (temperature > thresholdTemp) {
    digitalWrite(relayPin, HIGH); // Turn on relay
    Serial.println("Fan On: Temperature above threshold");
  } else {
    digitalWrite(relayPin, LOW); // Turn off relay
    Serial.println("Fan Off: Temperature below threshold");
  }
  delay(1000); // Wait for 1 second before repeating the loop
}

Schematic

Make connections according to the circuit diagram given below.

Wiring / Connections

ArduinoDHT SensorRelay
3V3VCC
GNDGN
D12DATA
5VVCC
GNDGND
A5INP

Installing Arduino IDE

First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.

Installing Libraries

Before you start uploading a code, download and unzip the following libraries at /Progam Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.

Code

Now copy the following code and upload it to Arduino IDE Software.

#include "DHT.h"
#define RELAY_FAN_PIN A5 // Arduino pin connected to relay which connected to fan
#define DHTPIN 12           // Arduino pin connected to relay which connected to DHT sensor
#define DHTTYPE DHT11

const int TEMP_THRESHOLD_UPPER = 25; // upper threshold of temperature, change to your desire value
const int TEMP_THRESHOLD_LOWER = 20; // lower threshold of temperature, change to your desire value

DHT dht(DHTPIN, DHTTYPE);

float temperature;    // temperature in Celsius

void setup()
{
  Serial.begin(9600); // initialize serial
  dht.begin();        // initialize the sensor
  pinMode(RELAY_FAN_PIN, OUTPUT); // initialize digital pin as an output
}

void loop()
{
  // wait a few seconds between measurements.
  delay(2000);

  temperature = dht.readTemperature();;  // read temperature in Celsius
  
  if (isnan(temperature)) {
    Serial.println("Failed to read from DHT sensor!");
  } else {
    if(temperature > TEMP_THRESHOLD_UPPER){
      Serial.println("The fan is turned on");
      digitalWrite(RELAY_FAN_PIN, HIGH); // turn on
    } else if(temperature < TEMP_THRESHOLD_LOWER){
      Serial.println("The fan is turned off");
      digitalWrite(RELAY_FAN_PIN, LOW); // turn on
    }
  }
}

Working Explanation

In the setup() function, the microcontroller initializes the serial communication and sets the relay pin as an output. This allows the microcontroller to send data to the serial monitor and to control the relay.

In the loop() function, the microcontroller reads the temperature value from the DHT11 sensor and compares it to the threshold temperature value. If the temperature is above the threshold, the microcontroller activates the relay by setting the relay pin to a high state and it prints the status on the serial monitor. If the temperature is below the threshold, the microcontroller deactivates the relay by setting the relay pin to a low state and it prints the status on the serial monitor.

Applications

  • HVAC Systems
  • Industrial Process Control
  • Home Automation
  • Greenhouses
  • Data Centers
  • Laboratories
  • Automotive

Conclusion.

We hope you have found this Cooling System using DHT Sensor Circuit very useful. If you feel any difficulty in making it feel free to ask anything in the comment section.