In order to measure the water level in my water butt I decided to try a waterproof ultrasonic distance measuring module. This operates in the same way as the standard ultrasonic modules I used in my previous Ultrasonic Distance Measurement Using Python tutorial.
The waterproof ultrasonic sensor I ordered looks like this :
It consists of :
- Waterproof transducer
- Cable
- Control board
The control board has the same four connections as the HR-SR04 modules :
- Trigger
- Echo
- 5V
- Ground
The module provides measurements within a range of 25cm – 450cm. The transducer itself looks like the device you would find in a car reversing sensor system rather than the twin-transducer arrangement of the classic HR-SR04 module.
Connecting To The Pi
The module can be powered from the Pi’s 5V and Ground pins. I used Pin 2 and Pin 6 on the Pi’s GPIO header.
The input pin on the module is called the “trigger” as it is used to trigger the sending of the ultrasonic pulse. It works just fine with a 3.3V signal from the GPIO so I connected the trigger directly to Pin 16 (GPIO23).
The module’s output is called the “echo” and needs a bit more thought. The output pin is low (0V) until the module has taken its distance measurement. It then sets this pin high (+5V) for the same amount of time that it took the pulse to return. So our script needs to measure the time this pin stays high. The module uses a +5V level for a “high” but this is too high for the inputs on the Pi which only like 3.3V. In order to ensure the Pi only gets hit with 3.3V we can use a basic voltage divider. This is formed with two resistors.
If R1 and R2 are the same then the voltage is split in half. This would give us 2.5V. If R2 is twice the value of R1 then we get 3.33V which is fine. So ideally you want R2 to be between R1 and R1 x 2. In my example circuit I used 330 and 470 ohm resistors. An alternative would be to use 1K and 1K5 values.
Here is a diagram of my final circuit. I chose GPIO23 and GPIO24 but you can use any of the 17 available GPIO pins on the GPIO header. Just remember to update the Python script accordingly.
I used a small piece of breadboard to create the voltage divider :
The White wire is connected to the Echo pin, the Purple wire to GPIO24 and the Blue/Black wires to Ground. Ignore the blue resistors as they are not used in this case.
Python Scripts
To test my module I used the same script as I used in my previous Ultrasonic sensor articles. Although this time I made some improvements.
There are two example Python scripts which you can download and try.
ultrasonic_1.py
A script to take a single reading and display the result in centimetres.
ultrasonic_2.py
A script to take continuous readings and display the result in centimetres.
You can download them directly to your Pi using :
wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/ultrasonic_1.py
and :
wget https://bitbucket.org/MattHawkinsUK/rpispy-misc/raw/master/python/ultrasonic_2.py
Once downloaded they can be run using :
python ultrasonic_1.py
and :
python ultrasonic_2.py
If you have it installed the scripts should also work under Python 3 :
python3 ultrasonic_1.py
You can use any GPIO pins you like amend your Python script accordingly.
Speed of Sound
The calculation that is used to find the distance relies on the speed of sound. This varies with temperature so I updated these scripts to calculate the correct value to use based on a defined temperature. You can change this value if required or perhaps measure it dynamically using a temperature sensor.
Water Butts
I like big water butts and I can not lie. I’m in the process of laying down some concrete to form a more suitable base for my water butts but once they are setup I will be fixing this sensor into the lid of one of them. I’ll write a new post detailing that but it is most likely to be a spring project next year.
Accuracy
Here are some points about accuracy :
- You get better readings from a solid flat surface with no other nearby objects. The sensor sends out pulses that form a “cone” so anything that gets in the way will affect the results.
- The accuracy of the distance measurement is dependent on timing. Python under Linux is not ideal for precise timing but for general messing about it will work OK. To improve accuracy you would need to start looking at using C instead.
- When the GPIOs are configured the module needs some time before it is ready to take its first reading so I added a 0.5 second delay to the start of the script.
Buying This Waterproof Ultrasonic Sensor
Here is a link to the Waterproof Ultrasonic Module Distance Measuring Transducer Sensor I ordered from eBay. These devices are available from other sellers and you can view the selection by searching eBay for “Waterproof ultrasonic module sensor”.
9 Comments
Why not link to where you ordered the sensor?
What is the sensor’s reasonable effecive distance range?
I’ve updated the post to include a link to the sensor I ordered from eBay. The range of the device is 4.5m but that assumes there isn’t any clutter in the surrounding area that will create sound wave reflections. I tested it up to around 2m.
Does this system work if submerged (obly the sensor)for more than half an hour underwater?(for navigation of an AUV)
I’m not sure but I don’t think this sort of sensor is designed to be used underwater.
Matt, I am not an electrical engineer, but I love to create new things. I was wondering if such a transducer can be used with the PI on my boat to identify objects at night time. For example getting closer and closer to the dock or another boat.
Over a limited range it would probably work. The only way to be sure is to test it. It would not be too similar from car parking sensors.
This is great – do you think it would work if housed in a cap of a PVC pipe? Shooting downward to water as it rises / lowers in the pipe? Or do you think there would be interference from the sides of the PVC? I’m thinking 4″ dia max pipe – ~5′ in length.
So it sounds too that the sound will echo off water? Or would I need a float to provide a hard surface to echo?
I’m completely new to any of this Raspberry stuff, but I really need a come up with a more affordable sensor to monitor stream levels. Thinking I’d ziptie a pvc pipe to a T-post driven into the streambed. Add a cap containing the sensor components (sealed in some way). Problem is, I’d need to have this run during the winter months (cold, wet environment). Battery powered… hrmm Think that’s even possible? I’d love to give it a try.
Thanks for your site!
The sides of pipe could cause reflections. However they would be fairly consistent. The distance measurement would be incorrect but that doesn’t matter as long as it gave you predictable results for a set water level. The only way to be sure is to try it!
Hy Matt
Many thanks for the good tutorial
I’ve found a little mistake in the code (both ultrasonic_1.py and ultrasonic_2.py)
The speed of sound is in cm/s so the multiplier for temperature difference has to be multiplied itself by 100. Because the temperature offset multiplied by 0.6 gives you the offset in meters.
speedSound = 33150 + (0.6*temperature*100)
Best regards Sam