Raspberry Pi Pico - Water Sensor

This guide will show you how to use a water sensor with Raspberry Pi Pico. A water sensor can detect water leaks, measure rainfall, see if a tank is too full, or monitor water levels. We will learn about these in detail.

Raspberry Pi Pico and water sensor module

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico (Alternatively)
1×Micro USB Cable
1×Water level 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 Water Level Sensor

Water Level Sensor Pinout

The water level sensor has three pins:

  • S (Signal) pin: Analog output. Connect to Raspberry Pi Pico's analog input.
  • + (VCC) pin: Provides power to the sensor. Use between 3.3V and 5V.
  • - (GND) pin: Ground connection.
water sensor pinout

※ NOTE THAT:

The signal from the sensor changes when the voltage at its VCC pin is adjusted.

How Water Level Sensor Works

As the sensor encounters more water, the voltage on the signal pin goes up.

Let's examine this more carefully.

This section includes advanced information that may be overwhelming. If you are unsure about the content, feel free to skip it and move on to the next sections.

The sensor has ten copper lines visible. It contains five power lines and five sensor lines. These lines are placed side by side, with one sensor line between every two power lines. Normally, these lines are separate, but if the sensor is placed in water, the water links the lines.

The traces act like an adjustable resistor (like a potentiometer) that changes its resistance depending on the water level.

  • The change in resistance indicates the distance from the top of the sensor to the surface of the water.
  • The resistance gets lower as the water level rises.
  • The more the sensor is submerged in water, the better it conducts electricity, which lowers the resistance.
  • The less the sensor is submerged in water, the poorer it conducts electricity, which raises the resistance.
  • The sensor produces an output voltage that depends on the resistance.

We can determine the water level by checking the voltage.

Wiring Diagram

Connect the sensor's VCC pin to the 5V pin on the Raspberry Pi Pico and the sensor's GND pin to the GND pin on the Raspberry Pi Pico to power the sensor.

It's not recommended to keep the sensor powered on in a wet environment because it can damage the sensor quickly and shorten its life. To prevent this, only turn on the sensor when you need to check its data. Connect the VCC pin of the sensor to a digital pin on the Raspberry Pi Pico. To read the sensor, set the Pico's pin to HIGH. After reading, set it to LOW.

The wiring diagram between Raspberry Pi and Pico Water Sensor

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Pico Code - Reading Value from Water Sensor

""" 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-water-sensor """ from machine import Pin, ADC import time # Constants for power and signal pins POWER_PIN = 0 # The Raspberry Pi Pico pin GP0 connected to the VCC pin of water sensor SIGNAL_PIN = 26 # The Raspberry Pi Pico pin GP26 (ADC0) connected to the S pin of water sensor # Setup power pin power = Pin(POWER_PIN, Pin.OUT) power.value(0) # Initially turn off the sensor # Setup ADC for reading the water sensor signal = ADC(Pin(SIGNAL_PIN)) while True: power.value(1) # Power on the sensor time.sleep(0.01) # Wait 10ms for sensor to stabilize value = signal.read_u16() # Read the analog value (0-65535 range on Pico) power.value(0) # Power off the sensor print("Sensor value:", value) # Print the value time.sleep(1) # Wait for a second before next read

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 water 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.
  • Slowly put the sensor into a glass of water.
  • Check out the message in the Shell at the bottom of Thonny.

. It will display a value of 0 if the sensor isn't touching anything.

Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot Sensor value: 0 Sensor value: 0 Sensor value: 0 Sensor value: 1595 Sensor value: 6201 Sensor value: 18163 Sensor value: 27386 Sensor value: 27839 Sensor value: 28229 Sensor value: 29116 Sensor value: 29868 Sensor value: 33341 Sensor value: 33822 Sensor value: 35398
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

※ NOTE THAT:

Do not put the sensor completely underwater; only the visible parts on the electronic board should touch the water. Install it with care.

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.

How To Detect Water Leakage

To check for water leaks, rain, or overflowing tanks, we compare the reading to a preset limit. We set this limit in the calibration part of this tutorial.

Raspberry Pi Pico Code - Finding Water Leaks

""" 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-water-sensor """ from machine import Pin, ADC import time # Constants for power and signal pins POWER_PIN = 0 # The Raspberry Pi Pico pin GP0 connected to the VCC pin of water sensor SIGNAL_PIN = 26 # The Raspberry Pi Pico pin GP26 (ADC0) connected to the S pin of water sensor THRESHOLD = 15000 # Setup the power pin power = Pin(POWER_PIN, Pin.OUT) power.value(0) # Initially turn off the sensor # Setup ADC for reading the water sensor signal = ADC(Pin(SIGNAL_PIN)) # Initialize the power pin to off power.value(0) while True: power.value(1) # Turn on power to the sensor time.sleep_ms(10) # Short delay to allow sensor to stabilize value = signal.read_u16() # Read the analog value from sensor power.value(0) # Turn off power to the sensor if value > THRESHOLD: # Check if the water level exceeds the threshold print("Water detected") time.sleep(1) # Delay for a second before the next reading

How To Measure The Water Level

To divide the highest water level into separate stages and find out which stage it is currently at, follow the method in the code below. Note, the highest water level is the same as the sensor's height. This code divides the top height into 4 stages.

""" 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-water-sensor """ from machine import Pin, ADC import time # Constants for power and signal pins POWER_PIN = 0 # The Raspberry Pi Pico pin GP0 connected to the VCC pin of water sensor SIGNAL_PIN = 26 # The Raspberry Pi Pico pin GP26 (ADC0) connected to the S pin of water sensor SENSOR_MIN = 0 SENSOR_MAX = 33408 # Setup the power pin power = Pin(POWER_PIN, Pin.OUT) # Setup ADC for reading the water sensor signal = ADC(Pin(SIGNAL_PIN)) # Function to map the sensor value from one range to another def map_value(x, in_min, in_max, out_min, out_max): return (x - in_min) * (out_max - out_min) // (in_max - in_min) + out_min power.value(0) # Initially turn off the sensor while True: power.value(1) # Activate the sensor time.sleep_ms(10) # Wait for the sensor to stabilize value = signal.read_u16() # Read the sensor output power.value(0) # Deactivate the sensor # Map the value to a 0-4 scale, adjusting for 16-bit ADC value level = map_value(value, SENSOR_MIN, SENSOR_MAX * 65535, 0, 4) # Print the water level print("Water level:", level) time.sleep(1) # Delay for one second before repeating

※ NOTE THAT:

  • SENSOR_MIN and SENSOR_MAX are set when calibrating.
  • The mapping method described might not be very accurate, but it works well for many purposes. To increase accuracy, measure the threshold values for each level as described in the calibration section.

Water Level Sensor Calibration

The sensor's performance changes with the water level and its conductivity. Pure water cannot conduct electricity, but water with minerals and impurities can. If the water is more conductive, the sensor is more responsive. Also, the output value varies when different voltages are used at the sensor's VCC pin.

For precise measurements with the water sensor, we recommend setting it up for the specific water type you are checking.

Before setting a limit for an action, first test the real value from the sensor.

How to do the test:

  • Look at the given drawing to check the sensor’s reading.
  • Put the sensor in the water until the level you want.
  • Write down the number you see in the Shell at the bottom of Thonny.
  • Use this number as the limit to start an action.

You might need to try this test several times to succeed.

The test can also help to find out:

  • Lowest sensor reading when not in water.
  • Highest sensor reading when fully submerged in water.
  • A set value to identify water leaks.
  • Set values for different levels on your scale.

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!