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.

Ultrasonic Module Circuit

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.

`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`

`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.