Skip to content

Interfacing Water Level Sensor with Raspberry Pi

Introduction

In this guide, we will explore the HW-038 water level sensor and its usage with a Raspberry Pi. The HW-038 water level sensor is designed to detect the level of water in a container or tank. By connecting the HW-038 sensor to the Raspberry Pi and utilizing Python programming, we can accurately measure the water level and monitor it in real-time. This sensor is commonly used in applications such as water level monitoring, automatic irrigation systems, and water storage management. The HW-038 water level sensor provides reliable and precise measurements, allowing us to effectively control and manage water resources. Let's dive in and discover how to wire the HW-038 water level sensor to the Raspberry Pi and retrieve water level data using Python.

Problem Statement

The objective is to create a circuit using a HW-038 sensor that can detect water levels.

Requirements

  1. Raspberry Pi
  2. Breadboard
  3. HW-038 sensor module
  4. MCP-3008
  5. Jumper wires

Working

To measure the water level using the HW-038 water level sensor with the Raspberry Pi, you can interface it with the MCP-3008 analog-to-digital converter (ADC) using the SPI (Serial Peripheral Interface) communication protocol.

Circuit Diagram

  • Connect the VDD(PIN 16) of MCP-3008 to 3V3(Pin 1) of RPi
  • Connect the VREF(PIN 15) of MCP-3008 to 3V3(Pin 17) of RPi
  • Connect the AGND(PIN 14) of MCP-3008 to GND(Pin 6) of RPi
  • Connect the CLK(PIN 13) of MCP-3008 to GPIO11(Pin 23-SCLK) of RPi
  • Connect the DOUT(PIN 12) of MCP-3008 to GPIO9(Pin 21-MISO) of RPi
  • Connect the DIN(PIN 11) of MCP-3008 to GPIO10(Pin 19-MOSI) of RPi
  • Connect the SHDN(PIN 10) of MCP-3008 to GPIO8(Pin 24-CEO) of RPi
  • Connect the DGND(PIN 9) of MCP-3008 to GND(Pin 34) of RPi
  • Connect the positive terminal of HW-038 sensor to same line of VDD(PIN 16) of MCP-3008/any 3V3 pin of raspberry pi
  • Connect the negative terminal of HW-038 sensor to same line of AGND(PIN 14) of MCP-3008/any GND(PIN 25) pin of raspberry pi
  • Connect the CHO(PIN 1) of MCP-3008 to S of HW-038
Raspberry Pi

Enabling I2C

Refer Soil Moisture.

Code

Now let's write the Python code to detect water level. Follow the steps below:

  1. Connect to your Raspberry Pi using VNC or Desktop environment.
  2. Launch the Geany Python IDE or open a text editor to write the code.
  3. Create a new file named waterlevel.py and save the following code into it
  4. Open the raspberrypi terminal and install the spidev using pip install spidev command
  5. You can copy and paste the following code from here into the file.
py

import spidev
import time

# Define SPI bus and device (chip enable pin)
spi = spidev.SpiDev()
spi.open(0, 0)  # (bus, device)

# Define water level thresholds (adjust these values as needed)
LOW_WATER_THRESHOLD = 50

def read_soil_moisture(channel):
    # Sending start bit, single-ended channel selection, and null bits
    adc_data = spi.xfer2([1, (8 + channel) << 4, 0])
    raw_value = ((adc_data[1] & 3) << 8) + adc_data[2]  # Combining the received bits
    # Calculating the percentage value
    percentage = int(((1023 - raw_value) / 1023.0) * 100)
    return percentage

def get_water_level(percentage):
    if percentage <= LOW_WATER_THRESHOLD:
        return "Low water level"
    else:
        return "High water level"

try:
    while True:
        # Assuming the soil moisture sensor is connected to channel 0
        moisture_level = read_soil_moisture(channel=0)
        water_level = get_water_level(moisture_level)
        print("Water Level: {}".format(water_level))
        time.sleep(0.1)  # Delay between readings

except KeyboardInterrupt:
    spi.close()
  1. After saving the file execute it by clicking on messenger like icon on geany editor or open terminal and navigate to the folder you saved the file, execute it by typing python waterlevel.py

Output

The HW-038 sensor provides the water level of the vessel with Raspberry Pi.

Conclusion

By following this guide, you have learned how to interface a water level sensor HW-038 with a Raspberry Pi. You have successfully wired the sensor to the Raspberry Pi and written Python code to read the water level data. With this knowledge, you can now monitor and detect the water level in various applications. Whether you're interested in water level monitoring in tanks, water leakage detection, or irrigation systems, the HW-038 sensor provides reliable and accurate data. You can further enhance your projects by integrating the water level sensor with other sensors or implementing actions based on the water level conditions. Enjoy exploring and experimenting with the HW-038 water level sensor on your Raspberry Pi to create innovative solutions for your projects!