Arduino Mega - Rain Sensor
In this guide, we will learn how to use an Arduino Mega with a rain sensor to see if it rains or snows. We will talk about the following topics:
- Connect a rain sensor to an Arduino Mega.
- Program the Arduino Mega to detect rain using the digital signal from the rain sensor.
- Program the Arduino Mega to measure how hard it is raining using the analog signal from the rain sensor.

Then you can change the code to turn on a motor or an alarm when it detects rain or snow.
Hardware Preparation
| 1 | × | Arduino Mega | |
| 1 | × | USB 2.0 cable type A/B (for USB-A PC) | |
| 1 | × | USB 2.0 cable type C/B (for USB-C PC) | |
| 1 | × | Rain Sensor | |
| 1 | × | Jumper Wires |
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (30 sensors/displays) | |
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Rain Sensor
The rain sensor can sense rain or snow and tell how much rain or snow there is. It provides two kinds of output signals: a digital signal (LOW or HIGH) and an analog signal.
The rain sensor has two parts:
- The sensor pad
- The control unit

The sensing pad
The sensor pad is placed outside where it can get rain or snow, for example on a roof. The pad has several copper wires, split into two kinds: power wires and sensor wires. These two kinds stay separate and do not touch each other unless water or snow bridges them. Both kinds work the same, so you can choose any wire to be the power wire and another to be the sensor wire.
The electronic module
The rain sensor module turns the signal from the sensing pad into a value the Arduino Mega can read, either analog or digital. It has four pins.
- VCC pin: Connect to power (3.3V to 5V).
- GND pin: Connect to ground (0V).
- DO pin: Digital output. It goes HIGH when there is no rain and LOW when rain is detected. Use the small knob to set the rain level.
- AO pin: Analog output. The reading goes down when there is more water on the sensing pad and goes up when there is less water.
It also has two small LED lights.
- One power light (PWR-LED).
- One rain indicator light (DO-LED) on the DO pin; it turns on when it rains.
How It Works
About the DO pin:
- The module has a knob to set the sensitivity.
- If the rain level goes over the limit you set, the sensor's output goes LOW and the DO-LED lights up.
- If the rain level stays under the limit, the sensor's output stays HIGH and the DO-LED stays off.
About the AO pin:
- When the sensor pad has more water, the AO pin value goes down.
- When the sensor pad has less water, the AO pin value goes up.
The pot does not change the value on the analog output pin.
Wiring Diagram
Connect the sensor’s VCC pin to a 3.3V or 5V pin on the Arduino Mega. But wiring it straight to the power pins can shorten the sensor’s life because of chemical corrosion. A better way is to connect the sensor’s VCC pin to an Arduino output pin. Then you can write a program to turn this pin on only when you take readings. This helps reduce chemical corrosion and wear on the sensor.
The rain sensor has two outputs. You can use one or both, depending on what you need.

This image is created using Fritzing. Click to enlarge image
Arduino Mega Code - Read value from DO pin
Detailed Instructions
Follow these steps one at a time:
- Connect the rain sensor to the Arduino Mega as shown in the diagram.
- Connect the Arduino Mega to your computer with a USB cable.
- Open the Arduino IDE on your computer.
- Choose the right board (Arduino Mega) and the correct port.
- Copy the code above and open it in the Arduino IDE.
- Press the Upload button to send the code to the Arduino Mega.
- Put a few drops of water on the rain sensor.
- Check the results in the Serial Monitor.
Remember: if the LED stays on all the time or goes off even when it rains on the sensor, you can turn the knob to change how sensitive the sensor is.
Arduino Mega Code - Read value from AO pin
Detailed Instructions
- Copy the code and open it in the Arduino IDE.
- Click the Upload button to upload the code to the Arduino Mega.
- Pour some water on the rain sensor.
- See what the Serial Monitor shows.