Raspberry Pi Tutorials

Building an Underwater Depth Measurement System with Raspberry Pi Zero 2 W and HX710B Pressure Sensor

Welcome to Engineers Butty Lab BD

Phone: 01888-011500

Address: 31 A/B, Seyed Villa, Jobbar Housing Society, Mazar Road, Mirpur -1, Dhaka.

 

 

Introduction

Accurate underwater depth measurement is an essential requirement for many marine applications, including remotely operated vehicles (ROVs), autonomous underwater vehicles (AUVs), mini submarines, water level monitoring systems, and environmental research projects. One of the most reliable methods of measuring underwater depth is by monitoring water pressure, as pressure increases proportionally with depth.

In this tutorial, we will build a real-time underwater depth measurement system using a Raspberry Pi Zero 2 W and an HX710B pressure sensor module. The project is divided into two stages to simplify development and troubleshooting.

The first program focuses on reading and displaying pressure values from the sensor to verify that the hardware is working correctly. After confirming proper sensor operation, the second program converts those pressure readings into real-time underwater depth measurements.

By separating the project into two stages, debugging becomes much easier, and the system can later be expanded with features such as wireless telemetry, data logging, OLED displays, LCD interfaces, or autonomous depth control.


Project Features

  • Read pressure values from an HX710B sensor
  • Automatically calibrate the sensor during startup
  • Convert pressure into underwater depth
  • Display real-time pressure and depth
  • Simple Python implementation
  • Compatible with Raspberry Pi Zero 2 W
  • Ideal for underwater robots, ROVs, submarines, and water monitoring projects

Components Required

Before starting, gather the following components.

Component Quantity
Raspberry Pi Zero 2 W 1
HX710B Pressure Sensor Module 1
Pressure Sensor (MPS20N0040D or compatible) 1
MicroSD Card with Raspberry Pi OS 1
Jumper Wires As Required
3.3V Power Supply 1

Hardware Connections

Connect the HX710B module to the Raspberry Pi as shown below.

HX710B Pin Raspberry Pi Zero 2 W
VCC 3.3V
GND GND
DOUT GPIO17
SCK GPIO27

Important: The Raspberry Pi GPIO operates at 3.3V logic. Ensure that your HX710B module supports 3.3V operation before connecting it.


Installing Required Libraries

Update your Raspberry Pi system before installing the required GPIO library.

sudo apt update
sudo apt install python3-rpi.gpio

Alternatively, install the library using pip.

pip3 install RPi.GPIO

Stage 1 – Reading Pressure Values

Before calculating underwater depth, it is important to verify that the pressure sensor is working correctly.

Create a new Python file.

nano pressure_sensor.py

Paste the pressure measurement program into the file and save it.

 

Run the program using:

python3 pressure_sensor.py

How the Pressure Measurement Program Works

The pressure measurement software performs the following tasks:

  • Initializes the Raspberry Pi GPIO pins.
  • Waits until the HX710B sensor has data available.
  • Reads the 24-bit digital output from the sensor.
  • Automatically performs zero-pressure calibration.
  • Converts raw sensor values into pressure values measured in kilopascals (kPa).
  • Continuously displays pressure readings every 200 milliseconds.

This stage allows you to verify that both the Raspberry Pi and the HX710B module are communicating correctly before attempting depth calculations.


Example Output

Calibrating...
Calibration Complete

Pressure: 0.00 kPa
Pressure: 1.82 kPa
Pressure: 12.74 kPa
Pressure: 18.95 kPa
Pressure: 26.41 kPa

If your output changes when pressure is applied to the sensor, the hardware is functioning correctly.


Why Pressure Can Be Used to Measure Depth

As an object moves deeper underwater, the surrounding water pressure increases.

The relationship between pressure and depth can be expressed as:

Depth (meters) = Pressure (kPa) ÷ 9.81

For freshwater, the pressure increases by approximately 9.81 kPa for every meter of depth.

Water Depth Pressure
1 Meter 9.81 kPa
2 Meters 19.62 kPa
3 Meters 29.43 kPa
4 Meters 39.24 kPa

This simple relationship allows us to calculate underwater depth directly from pressure measurements.


Stage 2 – Building the Depth Measurement Program

Once pressure readings have been verified, create another Python file for depth measurement.

nano depth_meter.py

Paste the depth measurement program into the file and save it.

Run the program.

python3 depth_meter.py

How the Depth Measurement Program Works

The depth measurement software follows several steps automatically.

1. Automatic Sensor Calibration

When the program starts, twenty pressure samples are collected.

The average of these readings becomes the zero-pressure reference, helping eliminate sensor offsets and improving measurement accuracy.


2. Surface Pressure Detection

The software measures atmospheric pressure while the sensor remains at the water surface.

This value becomes the reference pressure.

Any pressure measured below the surface is treated as gauge pressure.


3. Pressure Averaging

Instead of using a single sensor reading, the program averages multiple readings.

This reduces electrical noise and provides much smoother measurements.


4. Pressure-to-Depth Conversion

The software calculates underwater depth using the freshwater pressure equation:

Depth = Gauge Pressure ÷ 9.81

The calculated depth is then displayed continuously in meters.


Example Output

==========================
 HX710B DEPTH SENSOR
==========================

Keep sensor at surface...
Calibrating...

Zero Calibration Complete

Zero Raw Value = 9417740

Surface Pressure = 0.00 kPa

Depth Measurement Started

Pressure:  0.00 kPa | Depth: 0.00 m
Pressure:  4.90 kPa | Depth: 0.50 m
Pressure:  9.81 kPa | Depth: 1.00 m
Pressure: 19.62 kPa | Depth: 2.00 m
Pressure: 29.43 kPa | Depth: 3.00 m

As the sensor moves deeper underwater, both pressure and calculated depth increase accordingly.


Understanding the Sensor's Maximum Depth

Many hobbyist projects use the MPS20N0040D pressure sensor, which has an operating range of approximately 40 kPa.

Using the freshwater conversion formula:

Maximum Depth = 40 ÷ 9.81
≈ 4.08 meters

Therefore, this sensor is suitable for shallow-water applications up to approximately 4 meters.

For deeper underwater projects, consider using higher-pressure sensors.

Sensor Range Approximate Maximum Depth
40 kPa 4 meters
100 kPa 10 meters
200 kPa 20 meters
500 kPa 50 meters

Selecting the appropriate pressure sensor depends on the intended operating depth of your underwater vehicle.


Practical Applications

This underwater depth measurement system can be integrated into a wide range of projects, including:

  • Underwater ROVs (Remotely Operated Vehicles)
  • Mini Submarines
  • Autonomous Underwater Vehicles (AUVs)
  • Water Tank Level Monitoring
  • River and Canal Depth Measurement
  • Reservoir Monitoring Systems
  • Oceanographic Research Equipment
  • Underwater Data Logging Systems
  • Marine Robotics
  • Environmental Monitoring Projects

Future Improvements

After successfully measuring depth, the project can be expanded by adding additional features such as:

  • OLED or LCD display
  • Wireless Wi-Fi monitoring
  • MQTT cloud connectivity
  • Data logging to CSV files
  • Real-time web dashboard
  • Automatic depth control
  • Leak detection
  • Temperature compensation
  • GPS integration for surface tracking
  • Battery monitoring system

These enhancements make the project suitable for professional underwater robotics and research applications.


Conclusion

In this project, we successfully developed an underwater depth measurement system using a Raspberry Pi Zero 2 W and an HX710B pressure sensor module.

The project was divided into two independent stages. The first stage verified the sensor by measuring pressure values, while the second stage converted those pressure readings into accurate underwater depth measurements.

This modular design makes troubleshooting easier and provides a solid foundation for future upgrades. Whether you are building an underwater ROV, a mini submarine, or a water-level monitoring system, this project demonstrates a practical and reliable method for measuring underwater depth using affordable hardware and Python programming.

By extending this system with wireless communication, cloud monitoring, and autonomous control algorithms, it can easily become the core sensing module of a complete underwater robotics platform.

Leave a Reply

Your email address will not be published. Required fields are marked *