Rain Sensor Interfacing with 8051

In this tutorial we are going to see Rain Sensor Interfacing with 8051.


Before start this tutorial we should know below topics. If you know already, please go further.

Components Required

  • 8051 Development Board
  • Rain Drop Sensor
  • LCD Module (To print the Sensor output)


Water is a basic need in every one’s life. Saving water and proper usage of water is very important. Here is an easy project which will display the rain status, so that we can make some actions for rain water harvesting or take out our cloths from outside, before its getting wet due to rain. You can also add the alarm circuit to this project.

Rain water detector will detect the rain and make an alert; rain water detector is used in the irrigation field, home automation, communication, automobiles etc. It may, however, be tinkered for a number of other applications.

Rain Drop Sensor

Raindrop sensor is basically a board on which nickel is coated in the form of lines. It works on the principal of resistance.

Rain Sensor module allows to measure moisture via analog output pins and it provides a digital output when a threshold of moisture exceeds. The module is based on the LM393 op amp.


  • Adopts high quality of RF-04 double sided material.
  • Area: 5cm x 4cm nickel plate on side,
  • Anti-oxidation, anti-conductivity, with long use time;
  • Comparator output signal clean waveform is good, driving ability, over 15mA;
  • Potentiometer adjust the sensitivity;
  • Working voltage 5V;
  • Output format: Digital switching output (0 and 1) and analog voltage output AO;
  • With bolt holes for easy installation;
  • Small board PCB size: 3.2cm x 1.4cm;
  • Uses a wide voltage LM393 comparator

Working Principle of Rain Drop Sensor

The rain sensor used is composed by two parts. The first one is the effective sensor, which is a plaque that is exposed to the rain. This plaque has two strips of conductive material, very close to each other, but without touching.

So, if we apply a voltage between the the two strips, it will be an open circuit. Nevertheless, when we expose this surface to the rain, the water that falls closes the circuit between the strips and a different voltage can be measured.

Keep in mind that when the rain drops the two strips will not be short circuited because water is not a perfect conductor. So, this sensor will act as a variable resistor, which will be lower when more water falls on the surface, connecting the stripes in more points. I’ve tested the resistance of the sensor with a multimeter after applying some water drops and it was about 65 kΩ.

The second part is the electronic circuit board responsible to process the signal from the plaque and expose it as two signals, one digital and another analog.

So, we have a digital output pin, which operates as active-low, indicating that rain is being detected or not. Since this pin is active-low, it will have a value of GND when rain is detected, and VCC when rain is not detected.

Since, as stated, the rain sensor will act as a variable resistor, it’s output will be an analog voltage that needs to be converted to this digital one. So, the electronic circuit uses a LM393 comparator to compare this analog voltage to a certain threshold and output GND or VCC accordingly.

The PCB of this electronic circuit has a potenciometer that we can change to adjust this threshold, making the sensor more or less sensible to the rain drops.

Additionally, the  sensor has an analog output with a variable voltage that depends on the resistance of the sensor and thus, on the amount of water on it.

This module can work with voltage supplies of both 3.3 V and 5 V. Just keep in mind that there can be different versions from different manufacturers.

Rain Sensor Interfacing with 8051


Rain Sensor Sensor

  • Vcc – 5v
  • Gnd – Ground
  • DO – P1.0


  • RS – P2.0
  • RW – P2.1
  • EN – P2.2
  • Data Lines – P3.0 – P3.7

Rain Sensor Interfacing with 8051

Source Code

If it is detecting rain, LCD will display “Rain Detected”. You can also add buzzer for indication.

/* Rain Sensor Interfacing with 8051
 * Done by EmbeTronicX

#define lcd P3
sbit RAIN=P1^0;
sbit rs=P2^0; //register select
sbit rw=P2^1; //RW
sbit en=P2^2; //enable
void lcd_init();
void cmd(unsigned char);
void dat(unsigned char);
void delay();
void lcd_string(char *s);
void main()
    lcd_string("  EmbeTronicX   ");
    while(1) {
        if(RAIN == 0) {
            lcd_string("Rain Detected");
        } else {
            lcd_string("                ");
void lcd_init()
void cmd(unsigned char a)
void dat(unsigned char b)
void lcd_string(char *s)
    while(*s) {
void delay()
    unsigned int i;

Troubleshooting Rain Sensor

Although this is a relatively simple sensor to interface with, some problems may arise when using it.

The first one is related to the sensitivity of the module. The configuration of the potentiometer that comes when we buy it may not be the best, so the output of the digital pin of the module is always at GND or VCC, independently if there is water or not on the sensor. So, we may have to play around with the resistor a little bit until we find a nice equilibrium.

Also, some people experience problems with the salinity of the water, which may not be conducting well enough. You can change the type of water used or add a little bit of salt to check if it solves the problem. You can also use a resistor between the two strips of the sensor to try to simulate the connectivity caused by the water drops, and check if the module works properly.

If the module doesn’t work at all, it may be a unit with defect. From my personal experience, I once bought one of these modules with a defect that was putting the digital LED with a value of GND no matter if there were raindrops or not. The problem persisted even if I changed the sensitivity of the potentiometer, and so it was really a defective unit.

NOTE: If you want to find the amount of rain, then you have to use A0 pin and ADC.

0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Newest Most Voted
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x
Ads Blocker Image Powered by Code Help Pro
Ad Blocker Detected!!!

It seems you are using adblocking software that prevents the page from fully loading.

The revenue we earn by advertisements is used to manage this website. Still, If you want to disable the ads, please become a premium member. Ads are disabled for Premium members.

Or please add www.embetronicx.com to your ad blocker whitelist or disable your adblocking software and reload the page.

Note: Please disable the ad blocker to access the membership pages also.

False Detection? Please write a mail to [email protected]