Raspberry Pi Pico - Rain Sensor

This guide will show you how to use a Raspberry Pi Pico with a rain sensor to identify rain or snow. We will learn the followings:

Raspberry Pi Pico 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×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Rain Sensor
1×Jumper Wires
1×Breadboard
1×(Optional) Screw Terminal Expansion Board for Raspberry Pi Pico

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 sense and assess how much rain or snow falls. It produces two kinds of output signals: a digital signal (LOW or HIGH) and an analog signal.

The rain sensor consists of two parts:

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

The sensing pad

The sensing pad is put outside, where it could be exposed to rain or snow, like on a roof. It has many copper lines which are of two kinds: power lines and sensor lines. These lines are separate and only connect if water or snow touches them. Both kinds of lines work the same way, so you can pick any line as the power line and another as the sensor line.

The electronic module

The electronic module of the rain sensor transforms the signal from the sensing pad into a form (analog or digital) that the Raspberry Pi Pico can understand. It comes with four pins.

  • VCC pin: Connect this to VCC (between 3.3V and 5V).
  • GND pin: Connect this to GND (0V).
  • DO pin: This is a digital output pin. It shows HIGH if there is no rain and LOW if there is rain. Change the rain detection sensitivity using the potentiometer.
  • AO pin: This is an analog output pin. The output value decreases with more water on the sensing pad and increases with less water.

It also has two LED lights.

  • One light that shows power is on (PWR-LED).
  • One light that shows rain status (DO-LED) on the DO pin: it lights up when it rains.

How It Works

For the DO pin:

  • The module has a potentiometer to change the sensitivity.
  • When the rain level is too 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 remains HIGH, and the DO-LED light stays off.

About the AO pin:

  • When the sensing pad has more water, the AO pin value decreases.
  • When the sensing way has less water, the AO pin value increases.

The potentiometer does not alter the value on the AO pin.

Wiring Diagram

Connect the VCC pin of the sensor to a pin on the Raspberry Pi Pico that provides either 3.3V or 5V. But, directly connecting it might shorten the sensor's life because of electrochemical corrosion. A better way is to connect the sensor's VCC pin to a programmable output pin on the Raspberry Pi Pico. Set up the pin to give power to the sensor only when you need to take readings. This approach reduces electrochemical corrosion.

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

The wiring diagram between Raspberry Pi and Pico rain sensor

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code - Read value from DO pin

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-rain-sensor """ from machine import Pin import utime # Import utime for delay functions # Define the pins POWER_PIN = Pin(27, Pin.OUT) # The Raspberry Pi Pico pin GP27 provides the power to the rain sensor DO_PIN = Pin(2, Pin.IN) # The Raspberry Pi Pico pin GP2 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

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the rain sensor according to the provided diagram.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • 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 (Raspberry Pi Pico) • Board CDC @ COM29 ≡

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

If you name your script main.py and save it to the root directory of the Raspberry Pi Pico, it will automatically run each time the Pico is powered on or reset. This is useful for standalone applications that need to start running immediately upon power-up. If you name your script another name other than main.py, you will need to manually run it from Thonnys's Shell.

Raspberry Pi Pico Code - Read value from AO pin

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-rain-sensor """ from machine import Pin, ADC import utime # Import utime for delay functions # Define the pins POWER_PIN = Pin(27, Pin.OUT) # The Raspberry Pi Pico pin GP27 provides the power to the rain sensor AO_PIN = ADC(Pin(28)) # The Raspberry Pi Pico pin GP28 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_u16() >> 4 # Read the analog value from the sensor and convert back to 12-bit ADC 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 (Raspberry Pi Pico) • Board CDC @ COM29 ≡

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!