Raspberry Pi Zero W Ultrasonic Distance Server

Boating on the Broads is generally very easy but larger boat owners often have to contend with limited bridge heights. Most bridges have easy to read height boards but it can be tricky to accurately assess the available clearance, particularly when your boat can fold down one or more parts of the superstructure. It's also possible for the boat's air draft to vary according to the amount of fuel, water, passengers, and equipment carried.

I thought it would be useful to be able to accurately measure the clearance height available when passing under bridges so that these could be compared with their height boards. Once these values are obtained, the data can then be used along with the sensor as a realtime check that there is sufficient clearance. For example, the sensor could be mounted on the bow, which is usually lower than the superstructure, and give advance warning if there's a possibility of insufficient clearance.

I attempted this initially using a standard pocket ultrasonic measuring device, but was unsuccessful because I was unable to read the display in bright daylight. It was also apparent that it would be far better to obtain a continuous log of readings instead of relying on a couple of spot checks. I therefore decided to build a distance logger based around a Raspberry Pi Zero W and an ultrasonic distance sensor (RCWL-1601).

I didn't really want to permanently attach this device to my boat as it would be useful to be able to move it around, and I didn't want the hassle of any wires, so I opted to include a simple 4xAA battery holder and switch. For the display, I thought it would be ideal to broadcast the readings and collect these using my mobile phone. That way, I didn't need to be near the device to take readings. 

To build this project, the following parts are required:
I also found that these jumper leads were useful to allow the components to be separated if needed.

The circuit connections are really simple:


You could also use an HC-SR04 instead of the RCWL-1601, but then you'd need to use a couple of resistors as a voltage divider on the echo pulse output, so it's worth spending an extra £1.40 to keep things simple.

Reading and broadcasting the output is really simple with a small Python program:


MYPORT = 50000
from socket import *
s = socket(AF_INET, SOCK_DGRAM)
s.bind((
'', 0))
s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)
from time import sleep
from gpiozero import DistanceSensor
dist_sensor = DistanceSensor(echo=24, trigger=23, max_distance=4)
print("Press CTRL-C to exit.\n")
while True:
    myout=
"Distance sensor read %.3f m." % (dist_sensor.distance)
    
print(myout)
    udpoutput=
"%.3f" % (dist_sensor.distance)
    
try:
        s.sendto(
bytes(udpoutput.encode('utf-8')), ('<broadcast>', MYPORT))
    
except:
        
print("UDP out error")
    sleep(0.5)


Note that the output is broadcast to anything that wants to listen on the default network. You have to ensure that the Raspberry Pi Zero W is set up to connect to a suitable network. This could be your boat's WiFi router, as in my case, but it could just be a hotspot share on your phone.

Once it's working, the Python script on the Pi is set to be executable and to run on startup.

There are various ways you can do this, but I used Method 4 (SYSTEMD).

To read the data on your phone or tablet, you can use another Python program on that via Pydroid3 for Android:


import socket
print("Enter the offset value to be added")
myval=
input()
if myval == "":
    myval=
"0"
dx=float(myval)
client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
client.bind((
"", 50000))
while True:
    data, addr = client.recvfrom(1024)
    distance=data.decode(
'utf-8')
    x=
float(distance) + dx
    
print(x)


(There are several Python interpreters for iPhone/iPad but I'm not sure how compatible the socket library is. They may work but someone would need to test it)

Or alternatively, you could just install a UDP Terminal for Android or UDP Terminal for iPhone and tell it to listen on the same port (50000). One advantage is that you can save the log for examination later.

Once everything is working, how you use the device is up to you. I'm going to use mine to check the height of different parts of my boat against bridge height boards.

Obviously, you need a case to put everything in to make this practical. See this article for a 3D printed one.


24th October 2021