Arduino Nano - Rain Sensor

The rain sensor can detect and measure rain/snow levels, offering both digital (LOW/HIGH) and analog outputs. This tutorial demonstrates using an Arduino Nano to connect and utilize the rain sensor, covering how to read the digital signal for rain detection and the analog signal for measuring rain levels.

Following that, you can customize the code to trigger a motor or warning system upon detecting rain/snow.

Hardware Preparation

1×Arduino Nano
1×USB A to Mini-B USB cable
1×Rain Sensor
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for Arduino Nano
1×(Recommended) Breakout Expansion Board for Arduino Nano
1×(Recommended) Power Splitter For Arduino Nano

Or you can buy the following sensor 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 detect rain presence or measure water level from rainfall. It offers two options through a digital output pin and analog output pin.

The rain sensor consists of two parts: sensing pad and electronic module

Rain Sensor Pinout
image source: diyables.io

The sensing pad

The sensing pad, positioned outdoors to encounter rain/snow (e.g., on a roof), features exposed copper traces divided into two groups: power traces and sense traces. These traces remain unconnected unless bridged by water or snow. Both power and sense traces are interchangeable, allowing you to designate one as the power trace while the other functions as the sense trace.

The electronic module

The electronic module of the rain sensor transforms signals from the sensing pad into analog or digital values readable by the Arduino Nano. It includes four pins:

  • VCC pin: Connects to VCC (3.3V to 5V).
  • GND pin: Connects to GND (0V).
  • DO pin: Digital output pin. It is HIGH if rain is not detected and LOW if detected. The rain detection threshold is adjustable with a built-in potentiometer.
  • AO pin: Analog output pin. The output value decreases with increased water on the sensing pad and increases with decreased water.

Furthermore, it features two LED indicators:

  • One PWR-LED indicator for power.
  • One DO-LED indicator for the rain state on the DO pin, illuminating when rain is present.

How It Works

Regarding the DO pin:

  • The module features a built-in potentiometer for adjusting the threshold (sensitivity).
  • When the intensity surpasses the threshold, rain is detected, the sensor's output pin is LOW, and the DO-LED illuminates.
  • Conversely, when the intensity is below the threshold, no rain is detected, the output pin is HIGH, and the DO-LED is off.

For the AO pin:

  • The AO pin reads lower values as the water content in the sensing pad increases.
  • Conversely, it reads higher values as the water content decreases.

It's essential to note that the potentiometer does not influence the AO pin's value.

Wiring Diagram

As highlighted earlier, connecting the VCC pin of the sensor directly to the 3.3V or 5V pins on the Arduino Nano can shorten the sensor's lifespan due to electrochemical corrosion. To mitigate this, it is recommended to connect the VCC pin of the rain sensor to an output pin on the Arduino Nano. This way, the programming of that pin can be configured to power the rain sensor only during readings, minimizing the impact of 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 Nano and the rain sensor when using DO only.
The wiring diagram between Arduino Nano and rain sensor

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between Arduino Nano and the rain sensor when using AO only.
The wiring diagram between Arduino Nano and rain detection

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between Arduino Nano and the rain sensor when using both AO an DO.
The wiring diagram between Arduino Nano and rain detector

This image is created using Fritzing. Click to enlarge image

Arduino Nano Code - Read value from DO pin

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-rain-sensor */ #define POWER_PIN D5 // The Arduino Nano pin that provides the power to the rain sensor #define DO_PIN D9 // The Arduino Nano pin connected to DO pin of the rain sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the Arduino Nano'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

  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino Nano
  • Drop some water to the rain sensor
  • Check out the result on 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  

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 Nano Code - Read value from AO pin

/* * This Arduino Nano code was developed by newbiely.com * * This Arduino Nano code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-nano/arduino-nano-rain-sensor */ #define POWER_PIN D5 // The Arduino Nano pin that provides the power to the rain sensor #define AO_PIN A0 // The Arduino Nano pin connected to AO pin of the rain sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. 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 above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to Arduino Nano
  • Drop some water to the rain sensor
  • Check out the result on the Serial Monitor.
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!