Imagine this: You’re relaxing at home when suddenly, dark clouds roll in. Before you know it, rain is pouring through open windows and skylights, soaking your furniture and floors. Wouldn’t it be great if your home could sense the rain and respond automatically—like closing skylights, retracting an awning, or sending alerts?
That’s exactly where a rain sensor comes in! This sensor can detect not only rain but also slushy snow or hail, allowing you to take action before water damage becomes a problem.
In this tutorial, we’ll explore how the rain sensor works and how to connect and configure it with an Arduino to detect rainfall.
Let’s get started and bring some weather-responsive automation into your projects!
How Does a Rain Sensor Work?
The working principle of a rain sensor is quite simple.
It consists of a sensing pad with a series of exposed copper traces. These traces are typically not connected to each other but are bridged by water when rain falls on the pad. This forms a variable resistor, much like a potentiometer, where the resistance changes based on the amount of water present on the pad.
- When more water covers the surface, the conductivity increases, causing the resistance to decrease.
- When less water is present, the conductivity decreases, which leads to higher resistance.

The sensor generates an output voltage based on this changing resistance. By measuring the voltage, we can determine if it is raining and, in some cases, even estimate how much rain has fallen.
Hardware Overview
A typical rain sensor module consists of two main parts: the sensing pad and the LM393 comparator module.
The Sensing Pad
The sensing pad is the component that detects rain. It has a set of exposed copper traces and is typically placed in an open area, such as a rooftop, where it can come into direct contact with rain.

The LM393 Comparator Module
In addition to the sensing pad, the module includes an LM393 comparator, which provides both analog and digital outputs.
The analog output (AO pin) comes directly from the changing resistance of the sensing pad. As more water accumulates, the resistance decreases, which results in a lower voltage at the analog output pin.

This analog signal is also sent to the LM393 comparator, which compares it to a threshold (reference voltage) set by a potentiometer on the module.
- If the amount of water on the sensing pad is below the set threshold, the comparator gives a HIGH digital output signal at the digital pin (DO).
- If the water level on the pad exceeds the threshold, the comparator outputs a LOW digital signal.
This digital output can be used to trigger automated actions—such as activating a relay to close a window or sending an alert when rain is detected.
You can adjust the threshold by turning the potentiometer:

The module also has two LED indicators:
- Power LED – Lights up when the module is powered on.
- Status LED – Lights up when the digital output (DO) goes LOW, indicating that rain has been detected.
Rain Sensor Pinout
The rain sensor is extremely simple to use and only requires four pins to connect.

AO is the analog output pin. This pin gives a variable voltage that corresponds to the amount of water on the sensing pad—the more the water, the lower the output voltage. You connect this to an analog input pin on the Arduino (like A0).
DO is the digital output pin. This pin outputs LOW when the amount of water on the sensing pad exceeds the threshold value set by the potentiometer, and HIGH otherwise. You connect this to a digital input pin on the Arduino.
VCC pin provides power to the sensor. It’s best to power the sensor with 3.3V to 5V. Keep in mind that the analog output value will change based on the voltage you supply.
GND is the ground pin.
Wiring the Rain Sensor with Arduino
When using a rain sensor with an Arduino, setting it up the right way can help it last longer. Let’s go step by step to connect the sensor properly.
At first, powering the rain sensor might seem simple—you might think of directly connecting its VCC (power) pin to the Arduino’s 5V pin. However, keeping it powered all the time can cause the sensor’s surface to corrode faster due to constant exposure to moisture. This will reduce its effectiveness over time.
The solution? Don’t keep the sensor powered all the time. Instead, only turn it on when you want to take a reading. You can do this by connecting the sensor’s VCC pin to a digital pin on the Arduino instead of the 5V pin. This way, you can control when the sensor gets power just by changing the state of the digital pin in your code.
Don’t worry about power consumption—the rain sensor only uses about 8 milliamps (mA) when both its LEDs are on. This is so little that it’s completely safe to power the sensor directly from an Arduino digital pin. In this setup, we will connect the VCC pin of the sensor module to digital pin 7 on the Arduino and the GND pin to the GND on the Arduino.
To read the sensor’s output, we will connect the DO (Digital Output) pin on the sensor module to digital pin 8 on the Arduino. This setup allows the Arduino to detect rain by reading the digital signal from the sensor.
Here’s a quick reference table for the pin connections:
Rain Sensor | Arduino | |
VCC | 7 | |
GND | GND | |
DO | 8 |
Please refer to the image below to see the proper wiring setup.

Setting the threshold
As you may know, the rain sensor module comes with a small potentiometer that lets you customize how sensitive the sensor is to water. This means you can decide exactly how much water needs to be present before the sensor triggers. When the amount of water on the sensing pad exceeds the set threshold, the Status LED on the module will turn ON, and the DO (Digital Output) pin will output a LOW signal.
To set the threshold:
- Start by gently sprinkling some water onto the sensor’s sensing pad.
- Slowly turn the potentiometer clockwise until the Status LED lights up.
- Now, carefully turn the potentiometer just a bit counterclockwise until the LED turns off.
That’s all there is to it! Your rain sensor is now ready to use in your projects.
Positioning and Installation
When setting up your rain sensor, keep these important tips in mind:
- Place the sensor in an open area where it can directly detect rainfall, such as on a rooftop or outdoor surface.
- Tilt the sensing pad at about a 20-degree angle to help water drain off effectively.
- Be mindful that while the sensing pad is designed to handle exposure to rain, the electronic module itself is not waterproof. Make sure to install the module in a dry location so that only the sensing pad comes into contact with water.
Arduino Example Code
After wiring the sensor to the Arduino, upload the provided sketch to your board.
// Sensor pins
#define sensorPower 7
#define sensorPin 8
void setup() {
pinMode(sensorPower, OUTPUT);
// Initially keep the sensor OFF
digitalWrite(sensorPower, LOW);
Serial.begin(9600);
}
void loop() {
//get the reading from the function below and print it
int val = readSensor();
Serial.print("Digital Output: ");
Serial.println(val);
// Determine status of rain
if (val) {
Serial.println("Status: Clear");
} else {
Serial.println("Status: It's raining");
}
delay(1000); // Take a reading every second
Serial.println();
}
// This function returns the sensor output
int readSensor() {
digitalWrite(sensorPower, HIGH); // Turn the sensor ON
delay(300); // Allow power to settle
int val = digitalRead(sensorPin); // Read the sensor output
digitalWrite(sensorPower, LOW); // Turn the sensor OFF
return val; // Return the value
}
Once the sketch is running, open the Serial Monitor in the Arduino IDE. At first, you should see “Status: Clear” if the sensing pad is dry. Now to test the sensor, sprinkle some water onto the pad and watch as the status changes to “It’s raining”. Once the pad dries, the output should switch back to “Clear”.

Code Explanation:
The code starts by defining the Arduino pins that are connected to the VCC and DO pins of the sensor module.
#define sensorPower 7
#define sensorPin 8
In the setup() function, the first thing we do is set the sensorPower
pin as an OUTPUT so that we can control when the sensor receives power. To minimize corrosion and extend the sensor’s lifespan, we initially set this pin to LOW, meaning the sensor is off by default. We also initialize the Serial Monitor so we can see the sensor’s output in real-time.
void setup() {
pinMode(sensorPower, OUTPUT);
// Initially keep the sensor OFF
digitalWrite(sensorPower, LOW);
Serial.begin(9600);
}
Now, in the loop() function, we continuously check for rain. To do this, we call the readSensor()
function every second. The value returned by readSensor()
tells us whether rain has been detected. If the sensor returns a HIGH signal, it means the pad is dry, so we print “Status: Clear” in the Serial Monitor. If the sensor returns a LOW signal, it means enough water is present on the pad to be considered rain, so we display “Status: It’s raining” instead.
void loop() {
//get the reading from the function below and print it
int val = readSensor();
Serial.print("Digital Output: ");
Serial.println(val);
// Determine status of rain
if (val) {
Serial.println("Status: Clear");
} else {
Serial.println("Status: It's raining");
}
delay(1000); // Take a reading every second
Serial.println();
}
The readSensor()
function is responsible for retrieving the sensor’s digital output. To do this, it first turns the sensor ON by setting the power pin HIGH, then waits for 300 milliseconds to allow the sensor to stabilize. After that, it reads the digital value from the sensor’s DO pin, then immediately turns the sensor OFF again. Finally, the function returns the read value, which is used to determine whether it is raining or not. This method ensures that the sensor is only powered when necessary, reducing wear and tear.
int readSensor() {
digitalWrite(sensorPower, HIGH); // Turn the sensor ON
delay(300); // Allow power to settle
int val = digitalRead(sensorPin); // Read the sensor output
digitalWrite(sensorPower, LOW); // Turn the sensor OFF
return val; // Return the value
}