ESP32 MicroPython Rain Sensor

This tutorial instructs you how to use a rain sensor with a ESP32 and MicroPython to detect rain or snow. We will learn the followings:

ESP32 MicroPython rain sensor

You can modify the code so that it activates a motor or an alarm when it detects rain or snow.

Hardware Preparation

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Rain Sensor
1×Jumper Wires
1×Breadboard
1×(Recommended) Screw Terminal Expansion Board for ESP32

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
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.
Additionally, some of these links are for products from our own brand, DIYables.

Overview of Rain Sensor

The rain sensor can detect and measure the amount of rain or snow that falls. It provides two types of output signals: a digital signal (LOW or HIGH) and an analog signal.

The rain sensor is composed of two parts:

  • The sensor pad
  • The electronic module
Rain Sensor Pinout
image source: diyables.io

The Sensing Pad

The sensing pad is placed outside, where it can be exposed to rain or snow, such as on a roof. It consists of multiple copper lines, which are divided into two types: power lines and sensor lines. These lines remain separate and only connect when water or snow touches them. Both types of lines function identically, so you can designate any line as the power line and another as the sensor line.

The Electronic Module

The electronic module of the rain sensor converts the signal from the sensing pad into a form (analog or digital) that the ESP32 can interpret. It has four pins:

  • VCC pin: Connects to VCC (between 3.3V and 5V).
  • GND pin: Connects to GND (0V).
  • DO pin: A digital output pin that goes HIGH when there is no rain and LOW when rain is detected. The potentiometer can be adjusted to change the rain detection sensitivity.
  • AO pin: An analog output pin where the output value decreases as more water accumulates on the sensing pad and increases as the water lessens.

The module also includes two LED indicators:

  • A power indicator (PWR-LED) that lights up when the module is powered on.
  • A rain status indicator (DO-LED) connected to the DO pin, which lights up when rain is detected.

How It Works

For the DO pin:

  • The module includes a potentiometer to adjust sensitivity.
  • When the rain level is high, the sensor's output pin goes LOW, and the DO-LED light turns on.
  • When the rain level is low, the sensor's output pin stays HIGH, and the DO-LED light remains off.

Regarding the AO pin:

  • As the sensing pad detects more water, the AO pin value decreases.
  • As the sensing pad detects less water, the AO pin value increases.
  • The potentiometer does not affect the AO pin value.

Wiring Diagram

Connect the sensor's VCC pin to a 3.3V or 5V pin on the ESP32. However, a direct connection could reduce the sensor's lifespan due to electrochemical corrosion. Instead, connect the VCC pin to a programmable output pin on the ESP32, providing power to the sensor only when taking readings. This approach helps reduce electrochemical corrosion.

  • How to connect ESP32 and rain sensor using breadboard

The rain sensor module offers two outputs. You can use either one or both as required.

The wiring diagram between ESP32 MicroPython rain sensor

This image is created using Fritzing. Click to enlarge image

How to connect ESP32 and rain sensor

ESP32 MicroPython Code - Read value from DO pin

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-rain-sensor """ from machine import Pin import utime # Import utime for delay functions # Define the pins POWER_PIN = Pin(19, Pin.OUT) # The ESP32 pin GPIO19 provides the power to the rain sensor DO_PIN = Pin(21, Pin.IN) # The ESP32 pin GPIO21 connected to the DO pin of the rain sensor while True: POWER_PIN.value(1) # Turn the rain sensor's power ON utime.sleep_ms(10) # Wait 10 milliseconds rain_state = DO_PIN.value() POWER_PIN.value(0) # Turn the rain sensor's power OFF if rain_state == 1: print("The rain is NOT detected") else: print("The rain is detected") utime.sleep(1) # Pause for 1 second to avoid reading sensors frequently

Detailed Instructions

Here’s instructions on how to set up and run your MicroPython code on the ESP32 using Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Confirm that MicroPython firmware is loaded on your ESP32 board.
  • If this is your first time using an ESP32 with MicroPython, check out the ESP32 MicroPython Getting Started guide for step-by-step instructions.
  • Connect the ESP32 board to the rain sensor according to the provided diagram.
  • Connect the ESP32 board to your computer with a USB cable.
  • Open Thonny IDE on your computer.
  • In Thonny IDE, go to Tools Options.
  • Under the Interpreter tab, choose MicroPython (ESP32) from the dropdown menu.
  • Make sure the correct port is selected. Thonny IDE usually detects it automatically, but you might need to select it manually (like COM12 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided MicroPython code and paste it into Thonny's editor.
  • Save the code to your ESP32 by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device.
    • Name the file main.py.
  • Click the green Run button (or press F5) to execute the script.
  • Place some water drops on the rain sensor. Do not use pure water.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 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
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

Please note, if the LED light remains constantly on or stays off even during rain, adjust the sensor's sensitivity by changing the potentiometer.

ESP32 MicroPython Code - Read value from AO pin

""" This ESP32 MicroPython code was developed by newbiely.com This ESP32 MicroPython code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/esp32-micropython/esp32-micropython-rain-sensor """ from machine import Pin, ADC import utime # Import utime for delay functions # Define the pins POWER_PIN = Pin(19, Pin.OUT) # The ESP32 pin GPIO19 provides the power to the rain sensor AO_PIN = ADC(Pin(36)) # The ESP32 pin GPIO36 (ADC0) for reading analog input (corresponds to ADC2) # Set the ADC width (resolution) to 12 bits AO_PIN.width(ADC.WIDTH_12BIT) # Set the attenuation to 11 dB, allowing input range up to ~3.3V AO_PIN.atten(ADC.ATTN_11DB) while True: POWER_PIN.value(1) # Turn the rain sensor's power ON utime.sleep_ms(10) # Wait 10 milliseconds rain_value = AO_PIN.read() # Read the analog value from the sensor POWER_PIN.value(0) # Turn the rain sensor's power OFF print(rain_value) # Print out the analog value utime.sleep(1) # Pause for 1 second to avoid reading sensors frequently

Detailed Instructions

  • Copy the code and open it in Thonny IDE.
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Put some water on the rain sensor.
  • Check out the message in the Shell at the bottom of Thonny.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot 900 9711 944 2253 2750 3838 3902 4038 4070 4215 4315 3366 2974 1761 1116
MicroPython (ESP32) • CP2102 USB To UART Bridge Controller @ COM12 ≡

※ NOTE THAT:

This tutorial demonstrates how to use the adc.read() function to read values from an ADC (Analog-to-Digital Converter) connected to a rain sensor. The ESP32's ADC is suitable for projects that do not require high precision. However, if your project needs accurate measurements, keep the following in mind:

  • The ESP32 ADC is not perfectly accurate and may require calibration for precise results. Each ESP32 board may vary slightly, so calibration is necessary for each individual board.
  • Calibration can be challenging, especially for beginners, and might not always yield the exact results you desire.

For projects requiring high precision, consider using an external ADC (e.g., ADS1115) with the ESP32 or opt for an Arduino, which has a more reliable ADC. If you still wish to calibrate the ESP32 ADC, refer to the ESP32 ADC Calibration Driver.

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!