Detecting Rainfall Using Arduino UNO R3


Rainfall Detection is most commonly used in motor vehicles’ and home automation. In motor vehicles, when rainfall starts during traveling time, glass wipers are automatically switched on. In the home automation sector, during rainfall time if any of the windows are opened, then they're automatically closed.
Prerequisites for developing this system:
Hardware requirements:
Hardwares Nos 
Arduino UNO 
 Rain Sensor
10k ohm potentiometer 
LCD crystal display 
220-ohm resistor 
Breadboard  1
Connecting wires
Software requirements
  • Arduino IDE (For uploading code to Arduino Microcontroller)
In this experiment, we have three stages of work to detect rainfall using Arduino:
  1. Connecting all hardware
  2. Write & upload code to Arduino board
  3. Test our developed system
Stage 1 [Connecting all hardware]
Step 1
First, we can connect the LCD crystal display with the Arduino UNO board using Jumper wires. I am using a breadboard to extend power terminals for making this system.
Refer to the below LCD crystal pin diagram for making an easy connection with Arduino
Place a 10K ohm potentiometer near the LCD crystal display. It can adjust the contrast of the LCD screen and create a power connection from Arduino +5V and GND right and left end of the potentiometer. Make a connection from the center terminal of the potentiometer to VEE (contrast control) LCD crystal display. (Refer to the connection diagram)
Pin Configuration
Arduino                        Breadboard                                        LCD Crystal Display
Pin 2 ---------------------------------------------------------------------------> Data pin 4
Pin 3 ---------------------------------------------------------------------------> Data pin 5
Pin 4 ---------------------------------------------------------------------------> Data pin 6
Pin 5 ----------------------------------------------------------------------------> Data pin 7
Pin 7 ----------------------------------------------------------------------------> Register select
Pin 6 ----------------------------------------------------------------------------> Enable
+ 5V -----------------> Potentiometer Right Terminal------------------->Potentiometer Centre terminal ----> VEE (Contrast Control) LCD crystal
+5V ---------------------------> 220-ohm Resistor -----------------------> LED +5V pin [LCD Crystal]
GND ---------------------------------------------------------------------------->Potentiometer Left terminal, Ground pin & LED - Ground pin [Connect to both pin]
Step 2
Next, connect a rain sensor to the Arduino board as per the below pin Configuration.
Pin Configuration
Rain Electrode                     Sensor circuit board                                                   Arduino
  (+) ------------------------------------->(+)       Vcc -----------------------------------------------------------> +5V
   (-) ------------------------------------->(-)         AO ------------------------------------------------------------->A0
                                                                GND ----------------------------------------------------------> Gnd
Stage 2 [Write & upload code to Arduino]
In this step, write code for Arduino using the Arduino IDE.
  1. Download Arduino software using this link here.
  2. Install and open an Arduino IDE in your PC/Laptop
  3. Connect your Arduino board to your computer using a USB data cable
  4. Copy and paste below the code into the Arduino IDE
  1. #include "LiquidCrystal.h"    
  2. #define LCD_PIN_RS  8    
  3. #define LCD_PIN_E 7    
  4. #define LCD_PIN_DB4 3    
  5. #define LCD_PIN_DB5 4    
  6. #define LCD_PIN_DB6 5    
  7. #define LCD_PIN_DB7 6    
  9. const int sensorMin = 0;     
  10. const int sensorMax = 1024;     
  11. void setup()     
  12. {    
  13.   Serial.begin(9600);    
  14.    lcd.begin(16, 2);    
  15. lcd.print("C#corner");    
  16.  lcd.setCursor(0,1);    
  17.  lcd.print("Rain Detection");    
  18.  delay(2000);    
  19. }    
  21. void loop()    
  22. {    
  23.   int sensorReading = analogRead(A0);    
  24.   int range = map(sensorReading, sensorMin, sensorMax, 0, 3);    
  26.   switch (range)    
  27.     {    
  28.       case 0:    
  29.          lcd.clear();    
  30.     lcd.setCursor(0,0);     
  31.   lcd.print("Rainy");    
  32.         break;    
  34.       case 1:    
  35.          lcd.clear();    
  36.     lcd.setCursor(0,0);     
  37.   lcd.print("Rain Started");    
  38.         break;    
  40.       case 2:    
  41.          lcd.clear();    
  42.     lcd.setCursor(0,0);     
  43.   lcd.print("No Rain");    
  44.         break;    
  45.     }    
  46.   delay(1000);     
  47. }    
Save and upload this code to the Arduino UNO board. After the code upload is completed, the device can start to detect rainfall.
Stage 3 [Test the developed system]
Our developed rainfall detection using Arduino is working successfully!


In the above article, we studied detecting rainfall using Arduino UNO R3.