Rain sensor is used to monitor the rain and send closure requests to electronic shutters, windows, awnings or skylights whenever the rain is detected.
How Rain Sensor works?
The working of rain sensor is very easy.
The sensing pad with exposure copper traces, work as a variable resistor (like a potentiometer) whose resistance changes according to the water level on its surface.
The resistance is inversely proportional to the amount of water.
• When more water is on the surface of sensing the pad it means conductivity will increase and resistance will decrease.
• When lesser water is on the surface of sensing the pad it means conductivity will decrease and resistance will increase.
Hardware Overview
A rain sensor has two components.
The Sensing Pad
The sensor consist of a sensing pad with series of copper traces that work as a variable resistor. Place this pad outside in the open possibly over the roof or where it can be affected by rainfall.
The Module
The sensor consist on a module that connect to the sensing pad. This module produce an output voltage according to the resistance of sensing pad and we read it form AO pin.
The signal is store to a LM393 Comparator to digitize it and gives output to Digital Output (DO) pin.
This module has built in potentiometer for adjusting the sensitivity of digital output (DO). You can set a threshold by using a potentiometer. So, that when the amount of water increase from the threshold value, the module give output LOW otherwise HIGH.
The module has two LEDs. One is power LED and second is Status LED. When we give power to the module the Power LED will ON. The Status LED will ON when the digital output goes LOW.
Pinout Rain Sensor
It is easy to connect and use the rain sensor. Rain sensor has 4 pins to connect.
AO (Analog Output) pin gives us an analog signal. Connect AO to analog pin on Arduino.
DO (Digital Output) pin gives us Digital output. You can connect it to any digital pin on Arduino or directly to a 5V relay or similar device.
GND is a ground connection. Connect GND pin of module to GND on Arduino.
VCC pin supplies power for the sensor. Power up the sensor between 3.3V – 5V. The analog output will change depending on voltage, provided to the sensor.
Wiring Rain Sensor with Arduino
Connection is very simple and easy.
First of all connect the VCC pin of module to the 5V of Arduino. We know that the module is very sensitive and cannot bear continuous current. So, we give power to the sensor using digital pin of Arduino and set it LOW and HIGH as per your requirement.
Let’s connect the VCC pin to the digital pin # 8 on Arduino. Connect GND pin of module to the GND on Arduino. Finally, connect DO pin of module to the digital pin # 8 on Arduino.
Calibrating Rain Sensor
Frist of all you have to calibrate the reading to get the accurate reading. The module has a built-in potentiometer for calibrating the digital output (DO).
You can set a threshold by turning knob of potentiometer. When the amount of water is increases than threshold value, Status LED will turn on and DO pin give LOW output.
Detecting Rain – Arduino Code
Once the circuit is build, upload the following code to your Arduino to calibrate threshold.
Place the rain sensor in a location where rain drops can fall directly into the sensor, possibly over the roof. Also place it slightly tilted (~20°) to facilitate the flow of water.
Electronic module is waterproof, be careful to install it so that only the sensing pad will come in contact with water.
// Sensor pins
#define sensorPower 7
#define sensorPin 8
void setup() {
pinMode(sensorPower, OUTPUT);
// Initially keep the sensor OFF
digitalWrite(sensorPower, LOW);
Serial.begin(9600);
}
void loop() {
//get the reading from the function below and print it
int val = readSensor();
Serial.print("Digital Output: ");
Serial.println(val);
// Determine status of rain
if (val) {
Serial.println("Status: Clear");
} else {
Serial.println("Status: It's raining");
}
delay(1000); // Take a reading every second
Serial.println();
}
// This function returns the sensor output
int readSensor() {
digitalWrite(sensorPower, HIGH); // Turn the sensor ON
delay(10); // Allow power to settle
int val = digitalRead(sensorPin); // Read the sensor output
digitalWrite(sensorPower, LOW); // Turn the sensor OFF
return val; // Return the value
}
The output on Serial monitor is
Code Explanation:
First of all, declare the Arduino pin to which VCC and DO pins are connected.
#define sensorPower 7
#define sensorPin 8
In void setup section, first declare the power connection to the sensor as output, then set it low. So, no power flows through the sensor initially. We also setup the serial monitor.
pinMode(sensorPower, OUTPUT);
digitalWrite(sensorPower, LOW);
Serial.begin(9600);
In the loop section, we call readSensor() function repeatedly at the interval of one second and print the returned value along with the status.
int val = readSensor();
Serial.print(“Digital Output: “);
Serial.println(val);
if (val) {
Serial.println(“Status: Clear”);
} else {
Serial.println(“Status: It’s raining”);
}
delay(1000);
The readSensor() function is used to get the current digital output of the sensor. It turns the sensor ON, waits for 10 milliseconds, reads the digital value from the sensor, turns the sensor OFF and then returns the result.
int readSensor() {
digitalWrite(sensorPower, HIGH);
delay(10);
int val = digitalRead(sensorPin);
digitalWrite(sensorPower, LOW);
return val;
}