Rain Sensor Interfacing with 8051

In our last tutorial, we have interfaced the fire or flame detecting sensor with 8051. In this tutorial, we are going to see Rain Sensor Interfacing with 8051.

Prerequisites

Before starting this tutorial we should know the below topics. If you know already, please skip this section.

Components Required

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

Introduction

Water is a basic need in everyone’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 rainwater harvesting or take out our clothes from outside, before its getting wet due to rain. You can also add the alarm circuit to this project.

Rainwater 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

A raindrop sensor is basically a board on which nickel is coated in the form of lines. It works on the principle 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.

Specifications

  • 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 of 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 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 raindrops 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 waterfalls 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, its output will be an analog voltage that needs to be converted to this digital one. So, the electronic circuit uses an 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 potentiometer that we can change to adjust this threshold, making the sensor more or less sensible to the raindrops.

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

Connection

Rain Sensor Sensor

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

LCD

  • 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 a buzzer for indication.

/* Rain Sensor Interfacing with 8051
 * 
 * Done by EmbeTronicX
*/

#include<reg51.h>
#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_init();
    lcd_string("  EmbeTronicX   ");
    while(1) {
        if(RAIN == 0) {
            cmd(0xc0);
            lcd_string("Rain Detected");
            delay();
        } else {
            cmd(0xc0);
            lcd_string("                ");
        }
    }
}
 
void lcd_init()
{
    cmd(0x38);
    cmd(0x0e);
    cmd(0x06);
    cmd(0x01);
    cmd(0x80);
}
 
void cmd(unsigned char a)
{
    lcd=a;
    rs=0;
    rw=0;
    en=1;
    delay();
    en=0;
}
 
void dat(unsigned char b)
{
    lcd=b;
    rs=1;
    rw=0;
    en=1;
    delay();
    en=0;
}
 
void lcd_string(char *s)
{
    while(*s) {
       dat(*s++);
     }
}
 
void delay()
{
    unsigned int i;
    for(i=0;i<20000;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 the 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.

In our next tutorial, we will see how to detect the LPG gas leakage using 8051.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
STM32F7 Bootloader TutorialsRaspberry PI Pico Tutorials
STM32F103 Bootloader TutorialsRT-Thread RTOS Tutorials
Zephyr RTOS Tutorials - STM32Zephyr RTOS Tutorials - ESP32
AUTOSAR TutorialsUDS Protocol Tutorials
Product ReviewsSTM32 MikroC Bootloader Tutorial
VHDL Tutorials
Subscribe
Notify of
guest

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

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Table of Contents