Raspberry Pi - Water Sensor

This tutorial instructs you how to use Raspberry Pi with the water sensor. In detail, we will learn:

Hardware Preparation

1×Raspberry Pi 4 Model B
1×ADS1115 ADC Module
1×Water level sensor
1×Jumper Wires
1×(Optional) Screw Terminal Adapter for Raspberry Pi

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. We appreciate your support.

Overview of Water Level Sensor

Water Level Sensor Pinout

The water level sensor has 3 pins:

  • The S (Signal) pin: This is an analog output that should be connected to one of the analog inputs on your Raspberry Pi.
  • The + (VCC) pin: This supplies power for the sensor and it is recommended to use between 3.3V – 5V.
  • The - (GND) pin: This is a ground connection.
water sensor pinout

※ NOTE THAT:

The signal pin of the sensor produces an analog output that is dependent on the voltage provided to the VCC pin.

How Water Level Sensor Works

In short, the output voltage on the signal pin increases as the amount of water the sensor is submerged in increases.

Let's take a closer look.

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 exposed copper traces, with five being power traces and the other five being sense traces. These traces are arranged in parallel, with one sense trace between every two power traces. Unless they are bridged by water when submerged, these traces remain unconnected.

The traces act as a variable resistor, similar to a potentiometer, whose resistance is dependent on the water level:

  • The resistance is determined by the distance from the top of the sensor to the surface of the water.
  • The resistance is inversely proportional to the amount of water present:
  • As more water is immersed in the sensor, the conductivity increases and the resistance decreases.
  • As less water is immersed in the sensor, the conductivity decreases and the resistance increases.
  • The output voltage of the sensor is based on the resistance.

Determining the water level can be done by measuring the voltage.

Wiring Diagram

The wiring diagram between Raspberry Pi and Water Sensor

This image is created using Fritzing. Click to enlarge image

Raspberry Pi Code - Reading Value from Water Sensor

Detailed Instructions

  • Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
  • Make sure your Raspberry Pi is connected to the same local network as your PC.
  • Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
  • If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
  • Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
  • Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Install the Adafruit_ADS1x15 library by running the following commands on your Raspberry Pi terminal:
sudo pip install Adafruit-ADS1x15
  • Create a Python script file water_sensor.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-water-sensor import time import Adafruit_ADS1x15 # Create an ADS1115 ADC instance ADC = Adafruit_ADS1x15.ADS1115() # Specify the ADC channel (0-3) based on your connection ADC_CHANNEL = 3 # A3 of ADS1115 module # Set the gain (input voltage range) for your application GAIN = 1 # Gain of 1 corresponds to +/-4.096V # Define the conversion factor for water level calculation MIN_ADC_VALUE = 0 # Replace with the minimum ADC value for your sensor MAX_ADC_VALUE = 32767 # Replace with the maximum ADC value for your sensor try: while True: # Read the raw ADC value adc_value = ADC.read_adc(ADC_CHANNEL, gain=GAIN) # Convert the raw ADC value to a water level percentage water_level = (adc_value - MIN_ADC_VALUE) / (MAX_ADC_VALUE - MIN_ADC_VALUE) * 100 print(f"ADC Value: {adc_value} | Water Level: {water_level:.2f}%") time.sleep(1) # Wait for a second before the next reading except KeyboardInterrupt: print("\nScript terminated by user.")
  • Save the file and run the Python script by executing the following command in the terminal:
python3 water_sensor.py
  • Gently lower the sensor into a glass of water.
  • Check the output on the Terminal; it should be 0 when the sensor is not in contact with anything.
PuTTY - Raspberry Pi
ADC Value: 0 | Water Level: 0.00% ADC Value: 8192 | Water Level: 10.00% ADC Value: 8192 | Water Level: 10.00% ADC Value: 13503 | Water Level: 22.30% ADC Value: 13503 | Water Level: 22.30% ADC Value: 18714 | Water Level: 34.59% ADC Value: 18714 | Water Level: 34.59% ADC Value: 23925 | Water Level: 46.89% ADC Value: 29136 | Water Level: 59.18% ADC Value: 29136 | Water Level: 59.18%

The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.

※ NOTE THAT:

The sensor is not meant to be completely submerged; only the exposed traces on the printed circuit board should come into contact with water. Exercise caution when installing it.

How To Detect Water Leakage

To detect water leakage, rainfall, and tank overflow, we only need to compare the reading value with a threshold value that is determined during the calibration portion of this tutorial.

Let us consider a particular instance. If water is detected, Raspberry Pi will activate an LED. For wiring, see how to connect LED to Raspberry Pi

Raspberry Pi Code - Detecting Water Leakage

Detailed Instructions

  • Create a Python script file water_sensor_led.py and add the following code:
# This Raspberry Pi code was developed by newbiely.com # This Raspberry Pi code is made available for public use without any restriction # For comprehensive instructions and wiring diagrams, please visit: # https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-water-sensor import time import Adafruit_ADS1x15 import RPi.GPIO as GPIO # Create an ADS1115 ADC instance ADC = Adafruit_ADS1x15.ADS1115() # Specify the ADC channel (0-3) based on your connection ADC_CHANNEL = 3 # A3 of ADS1115 module # Set the gain (input voltage range) for your application GAIN = 1 # Gain of 1 corresponds to +/-4.096V # Define the conversion factor for water level calculation MIN_ADC_VALUE = 0 # Replace with the minimum ADC value for your sensor MAX_ADC_VALUE = 32767 # Replace with the maximum ADC value for your sensor # Define the threshold ADC value for triggering the LED LED_THRESHOLD = 20000 # Replace with your desired threshold # Define the LED GPIO pin LED_PIN = 16 # Replace with the GPIO pin number you're using # Set up the GPIO mode and LED pin GPIO.setmode(GPIO.BCM) GPIO.setup(LED_PIN, GPIO.OUT) try: while True: # Read the raw ADC value adc_value = ADC.read_adc(ADC_CHANNEL, gain=GAIN) # Convert the raw ADC value to a water level percentage water_level = (adc_value - MIN_ADC_VALUE) / (MAX_ADC_VALUE - MIN_ADC_VALUE) * 100 print(f"ADC Value: {adc_value} | Water Level: {water_level:.2f}%") # Turn on the LED if the water level is greater than the threshold if adc_value > LED_THRESHOLD: GPIO.output(LED_PIN, GPIO.HIGH) else: GPIO.output(LED_PIN, GPIO.LOW) time.sleep(1) # Wait for a second before the next reading except KeyboardInterrupt: print("\nScript terminated by user.") GPIO.cleanup() # Clean up GPIO settings on exit
  • Save the file and run the Python script by executing the following command in the terminal:
python3 water_sensor_led.py
  • Gently lower the sensor into a glass of water.
  • Check the output the LED state

Water Level Sensor Calibration

The output of the sensor is not only affected by the water level, but also by the conductivity of the water. Pure water is not conductive, whereas water with minerals and impurities is. The higher the conductivity of the water, the more sensitive the sensor is. Additionally, the output value is also dependent on the voltage supplied to the VCC pin of the sensor.

To ensure accuracy when reading the water sensor, we suggest calibrating the sensor for the specific type of water to be monitored.

Instructions for the calibration:

  • Utilize the sketch provided above to read the sensor value.
  • Immerse the sensor in the water at the desired level to set the threshold.
  • Record the value that the sensor displays in the Terminal.
  • Use this value as the threshold to trigger an action.

The test can also be used to discover:

  • MIN_ADC_VALUE value, when the sensor is not submerged in the liquid
  • MAX_ADC_VALUE value, when the sensor is completely submerged in the liquid

Video Tutorial

Challenge Yourself

  • When water leakage is detected:
  • Send an email
  • Send a SMS message
  • Activate a sound alarm

※ 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!