ESP32 - DHT11 - Relay

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

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×DHT11 Temperature and Humidity Sensor
1×Relay
1×12V Power Adapter
1×(Optional) DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Optional) 12V Fan
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to 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

ESP32 dht11 sensor relay wiring diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

How System Works

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

ESP32 Code

/* * This ESP32 code is created by esp32io.com * * This ESP32 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-dht11-relay */ #include <DHT.h> #define DHT11_PIN 23 // ESP32 pin GPIO23 connected to DHT11 #define RELAY_PIN 18 // ESP32 pin GPIO18 connected to relay #define DHT_SENSOR_TYPE DHT11 #define TEMP_UPPER_THRESHOLD 30 // upper temperature threshold #define TEMP_LOWER_THRESHOLD 15 // lower temperature threshold DHT dht11(DHT11_PIN, DHT_SENSOR_TYPE); 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 ESP32 turn on the relay when the temperature exceeds 25°C, and keep the relay on until the temperature is below 20°C

Quick Instructions

  • If this is the first time you use ESP32, see how to setup environment for ESP32 on Arduino IDE.
  • Do the wiring as above image.
  • Connect the ESP32 board to your PC via a micro USB cable
  • Open Arduino IDE on your PC.
  • Select the right ESP32 board (e.g. ESP32 Dev Module) and 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.
ESP32 DHT sensor library
  • You will be ased for intall some other library dependancies
  • Click Install All button all library dependancies.
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

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES