Arduino MicroPython Rain Sensor

This guide teaches you how to use a rain sensor with an Arduino and MicroPython to detect rain or snow. You will learn:

Arduino MicroPython rain sensor

Hardware Preparation

1×Arduino Giga R1 WiFi
1×USB Cable Type-C
1×Rain Sensor
1×Jumper Wires
1×(Recommended) Screw Terminal Block Shield for Arduino Uno/Mega/Giga
1×(Recommended) Breadboard Shield For Arduino Uno/Mega/Giga
1×(Recommended) Enclosure For Arduino Giga

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 detects and measures the amount of rain or snow. It offers two types of output signals: a digital signal (LOW or HIGH) and an analog signal.

The rain sensor consists of two components:

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

The Sensing Pad

The sensing pad is placed outdoors, where it can be exposed to rain or snow, such as on a roof. It is made up of multiple copper lines, which are categorized into two types: power lines and sensor lines. These lines stay separated and only connect when water or snow makes contact with them. Both types of lines work the same way, so you can assign any line as a power line and another as a sensor line.

The Electronic Module

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

  • VCC pin: Connects to VCC (3.3V to 5V).
  • GND pin: Connects to GND (0V).
  • DO pin: Digital output pin that goes HIGH when no rain is detected and LOW when rain is detected. The potentiometer can be adjusted to change the rain detection sensitivity.
  • AO pin: Analog output pin where the value decreases as more water collects on the sensing pad and increases as the water dries up.

The module also features two LED indicators:

  • Power indicator (PWR-LED): Lights up when the module is powered.
  • Rain status indicator (DO-LED): Connected to the DO pin, it lights up when rain is detected.

How It Works

For 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.

For the DO pin:

  • The module has 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.

Wiring Diagram

Connect the sensor's VCC pin to a 3.3V or 5V pin on the Arduino. However, a direct connection may shorten the sensor's lifespan due to electrochemical corrosion. To prevent this, connect the VCC pin to a programmable output pin on the Arduino, supplying power to the sensor only when taking readings. This method helps minimize electrochemical corrosion.

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

The wiring diagram between Arduino MicroPython rain sensor

This image is created using Fritzing. Click to enlarge image

Arduino MicroPython Code - Read value from DO pin

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-rain-sensor """ from machine import Pin import utime # Import utime for delay functions # Define the pins POWER_PIN = Pin('D2', Pin.OUT) # The Arduino Giga WiFi pin provides the power to the rain sensor DO_PIN = Pin('D3', Pin.IN) # The Arduino Giga WiFi pin 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 run the above MicroPython code on Arduino with Thonny IDE:

  • Make sure Thonny IDE is installed on your computer.
  • Make sure MicroPython firmware is installed on your Arduino board.
  • If you are new to Arduino with MicroPython, see the Getting Started with Arduino and MicroPython.
  • Connect the Arduino board to the rain sensor according to the provided diagram.
  • Connect the Arduino board to your computer with a USB cable.
  • Open Thonny IDE and go to Tools Options.
  • Under the Interpreter tab, select MicroPython (generic) from the dropdown menu.
  • Select the COM port corresponding to your Arduino board (e.g., COM33 on Windows or /dev/ttyACM0 on Linux).
  • Copy the provided Arduino MicroPython code and paste it into Thonny's editor.
  • Save the MicroPython code to your Arduino by:
    • Clicking the Save button or pressing Ctrl+S.
    • In the save dialog, choose MicroPython device and name the file main.py.
  • Click the green Run button (or press F5) to execute the code.
  • 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 (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

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

Arduino MicroPython Code - Read value from AO pin

""" This Arduino MicroPython script was developed by newbiely.com This Arduino MicroPython script is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/arduino-micropython/arduino-micropython-rain-sensor """ from machine import Pin, ADC import utime # Import utime for delay functions # Define the pins POWER_PIN = Pin('D2', Pin.OUT) # The Arduino Giga WiFi pin provides the power to the rain sensor AO_PIN = ADC(Pin('A0')) # The Arduino Giga WiFi pin for reading analog input (corresponds to ADC2) 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 904 931 981 2553 2950 3602 3916 3938 4007 4205 4305 3326 2934 1751 1146
MicroPython (generic) • Giga Virtual Comm Port in FS Mode @ COM33 ≡

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!