ESP8266 - Rain Sensor

The rain sensor detects and measures rain/snow levels, offering both digital (LOW/HIGH) and analog outputs. This tutorial demonstrates using an ESP8266 to connect and utilize the rain sensor, covering:

Afterward, you can customize the code to trigger a motor or warning when rain/snow is detected.

Hardware Preparation

1×ESP8266 NodeMCU
1×Micro USB Cable
1×Rain Sensor
1×Jumper Wires
1×(Optional) ESP8266 Screw Terminal Adapter

Or you can buy the following sensor kit:

1×DIYables Sensor Kit 30 types, 69 units
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 rain presence or measure water level. It offers two options through a digital output pin and an analog output pin.

The sensor comprises two components: the sensing pad and the electronic module

Rain Sensor Pinout
image source: diyables.io

The sensing pad

The sensing pad, positioned outdoors to encounter rain/snow (e.g., on the roof), features exposed copper traces categorized into two groups: power traces and sense traces. These traces remain unconnected unless water or snow bridges them. Both power and sense traces are identical, allowing flexibility in designating one as the power trace and the other as the sense trace.

The electronic module

The electronic module of the rain sensor converts signals from the sensing pad into analog or digital values readable by the ESP8266 NodeMCU. It includes four pins:

  • VCC pin: Connects to VCC (3.3V to 5V).
  • GND pin: Connects to GND (0V).
  • DO pin: Digital output pin, indicating HIGH when rain is not detected and LOW when detected. The rain detection threshold is adjustable with a built-in potentiometer.
  • AO pin: Analog output pin, with the value decreasing with increased water on the sensing pad and increasing with decreased water.

Furthermore, it features two LED indicators:

  • PWR-LED indicator for power.
  • DO-LED indicator for the rain state on the DO pin, illuminating when rain is present.

How It Works

FRegarding the DO pin:

  • The module is equipped with a built-in potentiometer for setting the sensitivity threshold.
  • When the intensity exceeds the threshold, rain is detected, and the sensor's output pin goes LOW, triggering the illumination of the DO-LED.
  • Conversely, when the intensity falls below the threshold, rain is not detected, the output pin goes HIGH, and the DO-LED turns off.

Concerning the AO pin:

  • The AO pin's value decreases with more water in the sensing pad.
  • Conversely, the AO pin's value increases with less water in the sensing pad.

It's important to note that the potentiometer does not influence the value on the AO pin.

Wiring Diagram

As previously mentioned, connecting the VCC pin of the sensor directly to the 3.3V or 5V pins on the ESP8266 can shorten the sensor's lifespan due to electrochemical corrosion. The recommended approach is to connect the VCC pin of the rain sensor to an output pin on the ESP8266. By programming that pin, the rain sensor can be powered only during readings, minimizing the impact of electrochemical corrosion.

Since the rain sensor module has two outputs, you can choose to use one or both of them, depending on what you need.

  • The wiring diagram between ESP8266 and the rain sensor when using DO only.
The wiring diagram between ESP8266 NodeMCU and rain sensor

This image is created using Fritzing. Click to enlarge image

See more in ESP8266's pinout and how to supply power to the ESP8266 and other components.

  • The wiring diagram between ESP8266 and the rain sensor when using AO only.
The wiring diagram between ESP8266 NodeMCU and rain detection

This image is created using Fritzing. Click to enlarge image

  • The wiring diagram between ESP8266 and the rain sensor when using both AO an DO.
The wiring diagram between ESP8266 NodeMCU and rain detector

This image is created using Fritzing. Click to enlarge image

ESP8266 Code - Read value from DO pin

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-rain-sensor */ #define POWER_PIN D7 // The ESP8266 pin that provides the power to the rain sensor #define DO_PIN D2 // The ESP8266 pin connected to DO pin of the rain sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. Serial.begin(9600); // initialize the ESP8266'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

To get started with ESP8266 on Arduino IDE, follow these steps:

  • Check out the how to setup environment for ESP8266 on Arduino IDE tutorial if this is your first time using ESP8266.
  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP8266
  • Drop some water to the rain sensor
  • Check out the result 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 keep in mind that if you notice the LED status remaining on constantly or off even when the sensor faces to a rain, you can adjust the potentiometer to fine-tune the sensitivity of the sensor.

ESP8266 Code - Read value from AO pin

/* * This ESP8266 NodeMCU code was developed by newbiely.com * * This ESP8266 NodeMCU code is made available for public use without any restriction * * For comprehensive instructions and wiring diagrams, please visit: * https://newbiely.com/tutorials/esp8266/esp8266-rain-sensor */ #define POWER_PIN D7 // The ESP8266 pin that provides the power to the rain sensor #define AO_PIN A0 // The ESP8266 pin connected to AO pin of the rain sensor void setup() { // Initialize the Serial to communicate with the Serial Monitor. 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

  • Wire the components as shown in the diagram.
  • Connect the ESP8266 board to your computer using a USB cable.
  • Open Arduino IDE on your computer.
  • Choose the correct ESP8266 board, such as (e.g. NodeMCU 1.0 (ESP-12E Module)), and its respective COM port.
  • Copy the above code and open with Arduino IDE
  • Click Upload button on Arduino IDE to upload code to ESP8266
  • Drop some water to the rain sensor
  • Check out 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!