Arduino UNO R4 - Rain Sensor

In this guide, we will learn to use an Arduino UNO R4 with a rain sensor to detect rain or snow. We will cover the following topics:

Arduino UNO R4 rain sensor

You can then change the code to turn on a motor or an alarm when it senses rain or snow.

Hardware Preparation

1×Arduino UNO R4 WiFi
1×Arduino UNO R4 Minima (Alternatively)
1×USB Cable Type-C
1×Rain Sensor
1×Jumper Wires
1×(Optional) 9V Power Adapter for Arduino UNO R4
1×(Recommended) Screw Terminal Block Shield for Arduino Uno
1×(Optional) Transparent Acrylic Enclosure For Arduino Uno

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. We appreciate your support.

Overview of Rain Sensor

The rain sensor can detect and measure the amount of rain or snow. It gives two types of output signals: a digital signal (LOW or HIGH) and an analog signal.

The rain sensor has two components:

  • The detection pad
  • The electronic unit
Rain Sensor Pinout
image source: diyables.io

The sensing pad

The sensing pad is placed outdoors where it may be exposed to rain or snow, such as on a roof. The pad has several copper lines, divided into two types: power lines and sensor lines. These two types of lines are separate and do not connect unless water or snow bridges them. Both types of lines function the same way, so you can choose any line to be the power line and another to be the sensoria line.

The electronic module

The rain sensor's electronic module changes the signal from the sensing pad into an analog or digital value that the Arduino UNO R4 can read. It has four pins.

  • VCC pin: Connect this to VCC (3.3V to 5V).
  • GND pin: Connect this to GND (0V).
  • DO pin: This is a digital output pin. It shows HIGH when there is no rain and LOW when rain is detected. Adjust the rain detection level with the potentiometer.
  • AO pin: This is an analog output pin. The output value goes down when more water is on the sensing pad and goes up when there is less water.

It also has two LED lights:

  • One power indicator light (PWR-LED).
  • One rain status indicator light (DO-LED) on the DO pin: it lights up when there is rain.

How It Works

For the DO pin:

  • The module includes a potentiometer to adjust the sensitivity.
  • If the rain level exceeds the set limit, the sensor's output pin becomes LOW, and the DO-LED light turns on.
  • If the rain level is under the set limit, the sensor's output pin stays HIGH, and the DO-LED light stays off.

Regarding the AO pin:

  • When there is more water in the sensing pad, the value from the AO pin is lower.
  • When there is less that is present on the sensing pad, the value from the AO pin is higher.

The potentiometer does not change the value on the AO pin.

Wiring Diagram

Connect the VCC pin of the sensor to either a 3.3V or 5V pin on the Arduino UNO R4. However, directly connecting it can reduce the sensor's lifespan due to electrochemical corrosion. Instead, it's better to connect the sensor's VCC pin to an output pin on the Arduino. You can then program this pin to supply power to the sensor only when taking readings. This method helps minimize the effects of electrochemical corrosion.

The rain sensor module has two outputs. You can use one or both, depending on your needs.

The wiring diagram between Arduino UNO R4 rain sensor

This image is created using Fritzing. Click to enlarge image

Arduino UNO R4 Code - Read value from DO pin

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-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 instructions step by step:

  • If this is your first time using the Arduino Uno R4 WiFi/Minima, refer to the tutorial on setting up the environment for Arduino Uno R4 WiFi/Minima in the Arduino IDE.
  • Connect the rain sensor to the Arduino Uno R4 according to the provided diagram.
  • Connect the Arduino Uno R4 board to your computer using a USB cable.
  • Launch the Arduino IDE on your computer.
  • Select the appropriate Arduino Uno R4 board (e.g., Arduino Uno R4 WiFi) and COM port.
  • Copy the code above and open it in Arduino IDE.
  • Click the Upload button in Arduino IDE to upload the code to Arduino UNO R4.
  • Put a few drops of water on the rain sensor.
  • Check the results 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 remember, if the LED light stays on all the time or is off even when the sensor is exposed to rain, you can change the potentiometer to adjust the sensor's sensitivity.

Arduino UNO R4 Code - Read value from AO pin

/* * This Arduino UNO R4 code was developed by newbiely.com * * This Arduino UNO R4 code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/arduino-uno-r4/arduino-uno-r4-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 Arduino IDE
  • Click the Upload button in Arduino IDE to upload the code to Arduino UNO R4
  • Put some water on the rain sensor
  • Check 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!