ESP32 S3 - DHT22 - Relay

This tutorial shows you how to use the ESP32 S3 together with a DHT22 temperature and humidity sensor to automatically control a relay based on the measured temperature. By combining precise environmental sensing with relay switching, you will gain a practical understanding of how to trigger high-power loads in response to real-world temperature data on the ESP32 S3.

What you'll build:

  1. A circuit connecting a DHT22 temperature and humidity sensor and a relay module to the ESP32 S3
  2. A sketch that continuously reads the temperature from the DHT22 sensor
  3. A system that automatically turns the relay on when the temperature exceeds an upper threshold and turns it off when the temperature falls below a lower threshold
  4. A foundation for building temperature-triggered automation systems such as cooling fans, heating elements, and climate-controlled enclosures
ESP32 S3 - DHT22 - Relay

Overview of Relay and DHT22 Sensor

The DHT22 sensor and a relay are a practical pairing for building temperature-controlled automation on the ESP32 S3. The DHT22 measures both temperature and relative humidity with high accuracy using a single-wire digital protocol, while the ESP32 S3 reads that data and uses it to switch a relay that can safely control high-voltage or high-current loads far beyond the capability of the microcontroller's own GPIO pins. Together they form the core of any thermostat or climate control system built around the ESP32 S3.

Key Specifications

We have dedicated tutorials covering each component in depth. Each tutorial contains detailed information and step-by-step instructions about hardware pinout, working principle, wiring connection to the ESP32 S3, and Arduino code. Learn more about them at the following links:

Wiring Diagram

Connect the DHT22 sensor and relay to your ESP32 S3 by following the diagram below. Double-check all connections before applying power, especially the high-voltage wiring on the relay's output terminals.

The wiring diagram between ESP32 S3 DHT22 Relay

This image is created using Fritzing. Click to enlarge image

Safety Notes

Always verify that the relay's COM and NO (or NC) terminals are securely fastened and that no bare wires are exposed before powering the circuit. Keep the low-voltage control wiring to the ESP32 S3 physically separated from any high-voltage load wiring to reduce the risk of accidental contact.

Component Pin ESP32 S3 Pin
DHT22 Sensor VCC 3.3V
DHT22 Sensor GND GND
DHT22 Sensor DATA GPIO1
Relay DC- GND
Relay DC+ 5V
Relay IN GPIO14

ESP32 S3 Code

The following code reads the temperature from the DHT22 sensor on the ESP32 S3, compares the value against configurable upper and lower thresholds, and switches the relay on or off accordingly. The threshold values can be adjusted directly in the code to suit different temperature control requirements.

/* * This ESP32 S3 code was developed by newbiely.com * * This ESP32 S3 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp32-s3/esp32-s3-dht22-relay */ /* * This ESP32-S3 code is created by esp32io.com * * This ESP32-S3 code is released in the public domain * * For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-s3-dht22-relay */ #include <DHT.h> #define DHT22_PIN 1 // ESP32-S3 pin GPIO1 connected to DHT22 #define RELAY_PIN 14 // ESP32-S3 pin GPIO14 connected to relay #define DHT_SENSOR_TYPE DHT22 #define TEMP_UPPER_THRESHOLD 30 // upper temperature threshold #define TEMP_LOWER_THRESHOLD 15 // lower temperature threshold DHT dht22(DHT22_PIN, DHT_SENSOR_TYPE); void setup() { Serial.begin(9600); // initialize serial dht22.begin(); // initialize the DHT sensor pinMode(RELAY_PIN, OUTPUT); // set ESP32-S3 pin to output mode } 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); }

Detailed Instructions

  1. New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
  2. Wire the components following the wiring diagram shown above.
  3. Connect the ESP32 S3 board to your PC using a USB Type-C cable.
  4. Open Arduino IDE on your PC.
  5. Select the correct ESP32 S3 board and COM port from the Tools menu.
  6. Click the Libraries icon on the left bar of the Arduino IDE.
  7. Search "DHT", then find the DHT sensor library by Adafruit and click Install.
  8. When prompted to install library dependencies, click Install All.
  9. Copy the above code and paste it into Arduino IDE.
  10. Click the Upload button to compile and upload the code to the ESP32 S3.
Arduino IDE Upload Code
  1. Open the Serial Monitor at 115200 baud.
  2. Make the environment around the sensor hotter or colder and observe the relay state change.
  3. Check the Serial Monitor to see the live temperature readings and relay state printed in real time.
  4. Pro Tip: Try setting the upper and lower thresholds to the same value if you want the relay to switch on and off at a single fixed temperature — this simplifies the logic for straightforward on/off thermostat applications.

Serial Monitor Output

The following Serial Monitor output shows the ESP32 S3 reading temperature data from the DHT22 sensor and switching the relay in real time. Each line displays the current measured temperature and the corresponding relay state triggered by the temperature thresholds.

∞
Newbiely | Arduino IDE 2.3.8
──
☐
✕
File
Edit
Sketch
Tools
Help
ESP32S3 Dev Module
Newbiely.ino
···
8 Serial.println("Hello World!");
Output
Serial Monitor
Message (Enter to send message to 'ESP32S3 Dev Module' on 'COM15')
New Line
9600 baud
[2026-06-16 10:22:01] Temperature: 22.4°C | Relay: OFF [2026-06-16 10:22:03] Temperature: 24.9°C | Relay: OFF [2026-06-16 10:22:05] Temperature: 25.3°C | Relay: ON [2026-06-16 10:22:07] Temperature: 26.1°C | Relay: ON
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Application and Project Ideas

Once you can trigger a relay from a DHT22 temperature reading on the ESP32 S3, you can extend this pattern to a wide range of temperature-sensitive automation and control projects.

  1. Automatic cooling fan: Activate a 12V fan via the relay when the temperature inside an enclosure rises above a safe operating level.
  2. Greenhouse heater controller: Switch a heating element or heat lamp relay on when temperature drops below a minimum threshold to protect plants from cold damage.
  3. Server room temperature alarm: Trigger a 12V siren or warning light via the relay whenever the ambient temperature exceeds a critical value.
  4. Incubator thermostat: Maintain a precise temperature band for egg incubation by cycling a heating pad relay on and off based on DHT22 readings.
  5. 3D printer enclosure ventilation: Automatically vent hot air from a printer enclosure by activating an exhaust fan relay when the internal temperature climbs too high.
  6. Cold storage monitor: Protect refrigerated goods by activating an alarm relay when the storage temperature rises above the safe range.

Video Tutorial

Watch the step-by-step video walkthrough for this ESP32 S3 project below.

Challenge Yourself

Now that you can control a relay with a DHT22 sensor on the ESP32 S3, try these progressively challenging extensions to deepen your understanding and build more capable temperature-controlled systems.

  1. Beginner: Modify the upper and lower threshold values in the code and observe how they change the activation and deactivation points for the relay, then print a confirmation message to the Serial Monitor showing which threshold is currently active.
  2. Beginner: Add an LED indicator wired to a separate GPIO pin that lights up whenever the relay is energised, giving you a low-voltage visual confirmation alongside the audible relay click.
  3. Intermediate: Extend the sketch to also read humidity from the DHT22 and add a second relay that activates when humidity exceeds a set percentage, creating a combined temperature and humidity control system.
  4. Intermediate: Add an I2C OLED screen to display the live temperature, humidity, and relay state in a formatted readout, removing the dependency on the Serial Monitor for monitoring.
  5. Advanced: Store configurable threshold values in ESP32 S3 non-volatile storage (Preferences library) so the settings persist across power cycles without re-uploading code.
  6. Advanced: Extend the project with WiFi so the relay can be monitored and the temperature thresholds adjusted remotely from a web page served by the ESP32 S3.

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