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:
- A circuit connecting a rain sensor and relay module to the ESP32 S3
- A sketch that activates the relay when rain is detected and deactivates it when the surface dries
- A system capable of switching high-voltage loads such as pumps, window actuators, or warning lights via the relay
- A foundation for expanding into weather-responsive automation and smart outdoor projects

Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables ESP32 S3 Starter Kit (ESP32 S3 included) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
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.

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.
Detailed Instructions
- New to ESP32 S3? Complete our Getting Started with ESP32 S3 guide first.
- Wire the components following the wiring diagram shown above.
- Connect the ESP32 S3 board to your PC using a USB Type-C cable.
- Open Arduino IDE on your PC.
- Select the correct ESP32 S3 board and COM port from the Tools menu.
- Copy the above code and paste it into Arduino IDE.
- Click the Upload button to compile and upload the code to the ESP32 S3.

- Place a few drops of water on the rain sensor's detection pad.
- Observe the relay activating (you will hear a click) when moisture is detected.
- Dry the sensor pad with a cloth and observe the relay deactivating.
- 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.
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.
- 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.
- Rain-activated irrigation shutoff: Use the relay to cut power to an irrigation pump whenever rain is detected, preventing overwatering and saving water.
- Car roof retraction: Activate a motor relay to retract a convertible car roof or awning when rainfall is detected outdoors.
- Weather alert system: Connect a buzzer or warning light to the relay to signal occupants that rain has started, useful in tutorials or garages.
- Laundry drying rack: Automatically retract an outdoor drying rack by driving a relay-controlled motor whenever the sensor detects rain.
- 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.
- 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.
- Beginner: Add an LED indicator that lights up whenever the relay is active, giving a visual confirmation alongside the audible relay click.
- 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.
- Intermediate: Add a second rain sensor placed at a different outdoor location and activate the relay only when both sensors detect moisture simultaneously.
- 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.
- 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.