ESP32 S3 - Rain Sensor - Relay

This tutorial shows you how to program the ESP32 S3 to activate a relay whenever rain is detected by a rain sensor, enabling you to automate devices in response to wet weather conditions. By connecting the relay to a motor, pump, or actuator, you can build a fully automated rain-triggered control system using your ESP32 S3.

What you'll build:

  1. A circuit connecting a rain sensor and relay module to the ESP32 S3
  2. A sketch that activates the relay when rain is detected and deactivates it when the surface dries
  3. A system capable of switching high-voltage loads such as pumps, window actuators, or warning lights via the relay
  4. A foundation for expanding into weather-responsive automation and smart outdoor projects
ESP32 S3 rain sensor and relay - turn on a solenoid lock, pump or light when rain is detected

Overview of Relay and Rain Sensor

The relay and rain sensor are a practical combination for building outdoor automation systems with the ESP32 S3. A rain sensor detects moisture on its sensing pad and outputs a digital signal that the ESP32 S3 reads in real time, while a relay safely switches high-voltage or high-current loads that would otherwise damage the board's GPIO pins. Together they let you build weather-responsive systems that automatically protect equipment, activate pumps, or trigger alerts when precipitation begins.

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 rain sensor and relay to your ESP32 S3 by following the diagram below. Double-check all connections before applying power, especially on the high-voltage side of the relay.

The wiring diagram between ESP32 S3 Rain Sensor 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
Rain Sensor GND GND
Rain Sensor VCC 3.3V
Rain Sensor DO GPIO2
Relay DC- GND
Relay DC+ 5V
Relay IN GPIO7

ESP32 S3 Code

The following code reads the digital output of a rain sensor connected to the ESP32 S3 and controls a relay accordingly — activating the relay when rain is detected and deactivating it when the surface is dry. All logic is handled with standard Arduino digitalRead() and digitalWrite() calls, so no external libraries are required.

/* * 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-rain-sensor-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-rain-sensor-relay */ #define RAIN_SENSOR_PIN 2 // The ESP32-S3 pin GPIO2 connected to the DO pin of the rain sensor #define RELAY_PIN 7 // The ESP32-S3 pin GPIO7 connected to the relay int rain_state = LOW; // current state of rain sensor's pin int prev_rain_state = LOW; // previous state of rain sensor's pin void setup() { Serial.begin(115200); // initialize serial pinMode(RAIN_SENSOR_PIN, INPUT); // set ESP32-S3 pin to input mode pinMode(RELAY_PIN, OUTPUT); // set ESP32-S3 pin to output mode } void loop() { prev_rain_state = rain_state; // store old state rain_state = digitalRead(RAIN_SENSOR_PIN); // read new state if (prev_rain_state == LOW && rain_state == HIGH) { // pin state change: LOW -> HIGH Serial.println("Rain detected!"); digitalWrite(RELAY_PIN, HIGH); // turn on } else if (prev_rain_state == HIGH && rain_state == LOW) { // pin state change: HIGH -> LOW Serial.println("Rain stopped!"); digitalWrite(RELAY_PIN, LOW); // turn off } }

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. Copy the above code and paste it into Arduino IDE.
  7. Click the Upload button to compile and upload the code to the ESP32 S3.
Arduino IDE Upload Code
  1. Place a few drops of water on the rain sensor's detection pad.
  2. Observe the relay activating (you will hear a click) when moisture is detected.
  3. Dry the sensor pad with a cloth and observe the relay deactivating.
  4. Pro Tip: Open the Serial Monitor at 115200 baud to print relay state messages in real time — this makes it much easier to debug unexpected behavior or calibrate the sensor's sensitivity threshold.

Serial Monitor Output

The following Serial Monitor output shows the ESP32 S3 responding to rain detection events and dry conditions in real time. Each line is timestamped so you can verify the timing between wet and dry transitions.

∞
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:23:41] Rain detected - Relay: ON [2026-06-16 10:23:55] No rain detected - Relay: OFF [2026-06-16 10:24:10] Rain detected - Relay: ON [2026-06-16 10:24:28] No rain detected - Relay: OFF
Ln 11, Col 1
ESP32S3 Dev Module on COM15
2

Application and Project Ideas

Once you master rain-sensor-controlled relay switching with the ESP32 S3, you can extend this pattern into a wide range of practical outdoor automation and weather-responsive projects.

  1. Automatic window closer: Trigger a linear actuator relay to close windows or a skylight as soon as rain begins, protecting interior spaces from water damage.
  2. Rain-activated irrigation shutoff: Use the relay to cut power to an irrigation pump whenever rain is detected, preventing overwatering and saving water.
  3. Car roof retraction: Activate a motor relay to retract a convertible car roof or awning when rainfall is detected outdoors.
  4. Weather alert system: Connect a buzzer or warning light to the relay to signal occupants that rain has started, useful in tutorials or garages.
  5. Laundry drying rack: Automatically retract an outdoor drying rack by driving a relay-controlled motor whenever the sensor detects rain.
  6. Greenhouse ventilation control: Close ventilation flaps automatically via a relay-driven actuator when rain is detected, keeping the interior dry.

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 rain sensor on the ESP32 S3, try these progressively challenging extensions to deepen your understanding and build more capable projects.

  1. Beginner: Modify the code so the relay stays ON for at least 30 seconds after rain is first detected, even if the sensor dries briefly, to avoid rapid relay toggling.
  2. Beginner: Add an LED indicator that lights up whenever the relay is active, giving a visual confirmation alongside the audible relay click.
  3. Intermediate: Implement a debounce or delay timer so the relay only activates after rain has been continuously detected for more than two seconds, reducing false triggers.
  4. Intermediate: Add a second rain sensor placed at a different outdoor location and activate the relay only when both sensors detect moisture simultaneously.
  5. Advanced: Build a data-logging system that records rain events with timestamps to an SD card so you can review precipitation history over days or weeks.
  6. Advanced: Extend the project with WiFi so the relay state can be monitored and overridden remotely via a web page served by the ESP32 S3, and send an email notification when rain is detected.

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