Arduino - DHT22 - Relay

In this tutorial, we are going to learn how to use Arduino to control the relay based on the temperature read from DHT22 sensor.

Hardware Required

1×Arduino UNO or Genuino UNO
1×USB 2.0 cable type A/B
1×DHT22 Temperature and Humidity Sensor
1×Relay
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×(Optional) 12V Fan
1×(Optional) 9V Power Adapter for Arduino
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Please note: These are Amazon affiliate links. If you buy the components through these links, We will get a commission at no extra cost to you. We appreciate it.

About Relay and DHT22 Sensor

If you do not know about DHT22 temperature sensor and relay (pinout, how it works, how to program ...), learn about them in the following tutorials:

Wiring Diagram

Arduino dht22 sensor relay wiring diagram

This image is created using Fritzing. Click to enlarge image

How System Works

  • Arduino reads the temperature from the DHT22 sensor
  • If the temperature exceeds an upper threshold, Arduino turn on the relay
  • If the temperature falls below a lower threshold, Arduino turn off the relay

The above process is repeated infinitely in the loop.

If you want to turn on and turn off the relay when the temperature is above and below a specific value respectively, you just need to set the upper threshold and lower threshold to the same value.

Arduino Code

/* * Created by ArduinoGetStarted.com * * This example code is in the public domain * * Tutorial page: https://arduinogetstarted.com/tutorials/arduino-dht22-relay */ #include "DHT.h" #define RELAY_PIN A5 // Arduino pin connected to relay #define DHT22_PIN 2 // Arduino pin connected to DHT22 sensor 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 dht22(DHT22_PIN, DHT22); float temperature; // temperature in Celsius void setup() { Serial.begin(9600); // initialize serial dht22.begin(); // initialize the sensor pinMode(RELAY_PIN, OUTPUT); // initialize digital pin as an output } void loop() { // wait a few seconds between measurements. delay(2000); temperature = dht22.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 relay is turned on"); digitalWrite(RELAY_PIN, HIGH); // turn on } else if(temperature < TEMP_THRESHOLD_LOWER){ Serial.println("The relay is turned off"); digitalWrite(RELAY_PIN, LOW); // turn on } } }

In the above codes, the Arduino turn on the relay when the temperature exceeds 25°C, and keep the relay on until the temperature is below 20°C

Quick Steps

  • Connect Arduino to PC via USB cable
  • Open Arduino IDE, select the right board and port
  • Navigate to the Libraries icon on the left bar of the Arduino IDE.
  • Search “DHT”, then find the DHT sensor library by Adafruit
  • Click Install button to install the library.
Arduino DHT sensor library
  • You will be ased for intall some other library dependancies
  • Click Install All button all library dependancies.
Arduino Adafruit Unified sensor library
  • Copy the above code corresponding to the sensor you have and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino
  • Make enviroment around sensor hotter or colder
  • See the state of the relay

Video Tutorial

We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.

The Best Arduino Starter Kit

※ OUR MESSAGES