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:

Arduino Mega 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)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
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
Rain Sensor Pinout
image source: diyables.io

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.

The wiring diagram between Arduino Mega rain sensor

This image is created using Fritzing. Click to enlarge image

Arduino Mega Code - Read value from DO pin

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-rain-sensor */ #define POWER_PIN 3 // The Arduino UNO R4 pin that provides the power to the rain sensor #define DO_PIN 4 // The Arduino's pin connected to DO pin of the rain sensor void setup() { // initialize serial communication Serial.begin(9600); // initialize the Arduino's pin as an input pinMode(POWER_PIN, OUTPUT); // configure the power pin pin as an OUTPUT pinMode(DO_PIN, INPUT); } void loop() { digitalWrite(POWER_PIN, HIGH); // turn the rain sensor's power ON delay(10); // wait 10 milliseconds int rain_state = digitalRead(DO_PIN); digitalWrite(POWER_PIN, LOW); // turn the rain sensor's power OFF if (rain_state == HIGH) Serial.println("The rain is NOT detected"); else Serial.println("The rain is detected"); delay(1000); // pause for 1 sec to avoid reading sensors frequently to prolong the sensor lifetime }

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.
COM6
Send
The rain is NOT detected The rain is NOT detected The rain is NOT detected The rain is detected The rain is detected The rain is detected The rain is detected The rain is detected The rain is NOT detected The rain is NOT detected The rain is NOT detected
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

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

/* * This Arduino Mega code was developed by newbiely.com * * This Arduino Mega code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-mega/arduino-mega-rain-sensor */ #define POWER_PIN 3 // The Arduino UNO R4 pin that provides the power to the rain sensor #define AO_PIN A0 // The Arduino UNO R4 pin connected to AO pin of the rain sensor void setup() { // initialize serial communication Serial.begin(9600); pinMode(POWER_PIN, OUTPUT); // configure the power pin pin as an OUTPUT } void loop() { digitalWrite(POWER_PIN, HIGH); // turn the rain sensor's power ON delay(10); // wait 10 milliseconds int rainValue = analogRead(AO_PIN); digitalWrite(POWER_PIN, LOW); // turn the rain sensor's power OFF Serial.println(rainValue); // print out the analog value delay(1000); // pause for 1 sec to avoid reading sensors frequently to prolong the sensor lifetime }

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.
COM6
Send
225 2426 236 563 687 959 975 1009 1017 1053 1078 841 743 440 279
Autoscroll Show timestamp
Clear output
9600 baud  
Newline  

Video Tutorial

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