Arduino Nano ESP32 - DHT11 - 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 DHT11 sensor.

Hardware Preparation

1×Arduino Nano ESP32
1×USB Cable Type-C
1×DHT11 Temperature and Humidity Sensor
1×DHT11 Temperature Humidity Sensor Module (short)
1×Relay
1×12V Power Adapter
1×(Optional) DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Optional) 12V Fan
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Relay and DHT11 Sensor

If you do not know about DHT11 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 dht11 sensor relay

This image is created using Fritzing. Click to enlarge image

How System Works

  • Arduino Nano ESP32 reads the temperature from the DHT11 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 newbiely.com * * This Arduino Nano ESP32 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano-esp32/arduino-nano-esp32-dht11-relay */ #include <DHT.h> #define DHT11_PIN D2 // The Arduino Nano ESP32 pin connected to DHT11 #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 dht11(DHT11_PIN, DHT1); void setup() { Serial.begin(9600); // initialize serial dht11.begin(); // initialize the DHT sensor } void loop() { float temperature = dht11.readTemperature();; // read temperature in Celsius if (isnan(temperature)) { Serial.println("Failed to read from DHT11 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

※ OUR MESSAGES

  • 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!