Arduino Nano ESP32 - DHT22 - Relay

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

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×DHT22 Temperature and Humidity Sensor
1×12V Power Adapter
1×(Optional) DC Power Jack
1×Jumper Wires
1×(Optional) 12V Fan
1×(Recommended) Screw Terminal Adapter for Arduino Nano

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you. We appreciate your support.

Overview of 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

The wiring diagram between Arduino Nano ESP32 and dht22 sensor relay

This image is created using Fritzing. Click to enlarge image

How System Works

  • Arduino Nano ESP32 reads the temperature from the DHT22 sensor
  • If the temperature exceeds an upper threshold, Arduino Nano ESP32 turn on the relay
  • If the temperature falls below a lower threshold, Arduino Nano ESP32 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 Nano ESP32 Code

/* * This Arduino Nano ESP32 code was developed by * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * */ #include <DHT.h> #define DHT22_PIN D2 // The Arduino Nano ESP32 pin connected to DHT22 #define RELAY_PIN D13 // The Arduino Nano ESP32 pin connected to relay #define TEMP_UPPER_THRESHOLD 30 // upper temperature threshold #define TEMP_LOWER_THRESHOLD 15 // lower temperature threshold DHT dht22(DHT22_PIN, DHT22); void setup() { Serial.begin(9600); // initialize serial dht22.begin(); // initialize the DHT sensor } void loop() { float temperature = dht22.readTemperature();; // read temperature in Celsius if (isnan(temperature)) { Serial.println("Failed to read from DHT22 sensor!"); } else { if (temperature > TEMP_UPPER_THRESHOLD) { Serial.println("Turn the relay on"); digitalWrite(RELAY_PIN, HIGH); // turn on } else if (temperature < TEMP_LOWER_THRESHOLD) { Serial.println("Turn the relay off"); digitalWrite(RELAY_PIN, LOW); // turn off } } // wait a 1 seconds between readings delay(1000); }

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

Detailed Instructions

To get started with Arduino Nano ESP32, follow these steps:

  • If you are new to Arduino Nano ESP32, refer to the tutorial on how to set up the environment for Arduino Nano ESP32 in the Arduino IDE.
  • Wire the components according to the provided diagram.
  • Connect the Arduino Nano ESP32 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the Arduino Nano ESP32) board and its corresponding COM port.
  • Click 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 Nano ESP32 DHT sensor library
  • You will be ased for intall some other library dependancies
  • Click Install All button all library dependancies.
Arduino Nano ESP32 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 ESP32
  • Make enviroment around sensor hotter or colder
  • See the state of the relay

Video Tutorial


  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!