Arduino - Rain Sensor
The rain sensor is capable of detecting and measuring rain/snow level. The rain sensor provides two outputs: a digital output (LOW/HIGH) and an analog output.
In this tutorial, we will learn how to use an Arduino and an rain sensor to detect and measure the rain. Specifically, we will cover the following:
- How to connect the rain sensor to an Arduino.
- How to program the Arduino to detect the rain by reading the digital signal from the rain sensor.
- How to program the Arduino to measure the rain level by reading the analog signal from the rain sensor.
Afterward, you can modify the code to activate a motor or warning when it detects rain./snow
Hardware Required
Or you can buy the following sensor kits:
1 | × | DIYables Sensor Kit (30 sensors/displays) | |
1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some links direct to products from our own brand, DIYables.
About Rain Sensor
The rain sensor can be utilized to detect the presence of rain or measure the level of water dropped by the rain. The rain sensor provides two options via a digital output pin and analog output pin.
The rain sensor includes two parts:
- The sensing pad
- The electronic module
The sensing pad
The sensing pad is placed outside that can face with rain/snow (e.g. the roof). The sensing pad has a series of exposed copper traces, devided into two groups: power traces and sense traces. These power traces and sense traces are not connected unless they are bridged by water or snow. There is no differences between the power trace and sense trace. You can pick one as power trace and the other wil be come the sense trace
The electronic module
The The electronic module of rain sensor converts the signal from the sensing pad to analog or digital value that can be read by Arduino. It includes four pins:
- VCC pin: It needs to be connected to VCC (3.3V to 5V).
- GND pin: It needs to be connected to GND (0V).
- DO pin: It is a digital output pin. It is HIGH if the rain is not detected and LOW if detected. The threshold value for rain detection can be adjusted using a built-in potentiometer.
- AO pin: It is an analog output pin. The output value decreases as the water in the sensing pad is increased, and it increases as water in the sensing pad is decreased.
Furthermore, it has two LED indicators:
- One PWR-LED indicator for power.
- One DO-LED indicator for the rain state on the DO pin: it is on when rain is present.
How It Works
For the DO pin:
- The module has a built-in potentiometer for setting the threshold (sensitivity).
- When the intensity is above the threshold value, the rain is detected, the output pin of the sensor is LOW, and the DO-LED is on.
- When the intensity is below the threshold value, the rain is NOT detected, the output pin of the sensor is HIGH, and the DO-LED is off.
For the AO pin:
- The more water in the sensing pad, the lower the value read from the AO pin.
- The less water in the sensing pad, the higher the value read from the AO pin.
Note that the potentiometer does not affect the value on the AO pin.
Wiring Diagram
As mentioned above, the VCC pin of the sensor should be connected to the 3.3V or 5V. If we connect this pin directly to 3.3V or 5V pin of Arduino, the lifespan of sensor will be shorten because of electrochemical corrosion. The best way is to connect the the VCC pin of the rain sensor to an output pin of Arduino, We can program that pin to power the rain sensor when reading only. This can mimimize the impart of the electrochemical corrosion.
Since the rain sensor module has two outputs, you can choose to use one or both of them, depending on what you need.
- The wiring diagram between Arduino and the rain sensor when using DO only.
This image is created using Fritzing. Click to enlarge image
- The wiring diagram between Arduino and the rain sensor when using AO only.
This image is created using Fritzing. Click to enlarge image
- The wiring diagram between Arduino and the rain sensor when using both AO an DO.
This image is created using Fritzing. Click to enlarge image
Arduino Code - Read value from DO pin
Quick Steps
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Drop some water to the rain sensor
- See the result on Serial Monitor.
Please keep in mind that if you notice the LED status remaining on constantly or off even when the sensor faces to a rain, you can adjust the potentiometer to fine-tune the sensitivity of the sensor.
Arduino Code - Read value from AO pin
Quick Steps
- Copy the above code and open with Arduino IDE
- Click Upload button on Arduino IDE to upload code to Arduino
- Drop some water to the rain sensor
- See the result on Serial Monitor.
Video Tutorial
We are considering to make the video tutorials. If you think the video tutorials are essential, please subscribe to our YouTube channel to give us motivation for making the videos.