Skip to main content
Yanked

This release has been yanked by its maintainers, and will be ignored by installers, except when explicitly specified.
Reason given by maintainers: This version deprecated in favour of Pinsource v1.0 https://www.linuxnorth.org/pinsource

HC-SR04 Ultrasonic Sensor on Raspberry Pi

Hcsr04sensor Web Page

Calculate distance and depth measurements with an HCSR04 Ultrasonic Sound Sensor and a Raspberry Pi. Instructions assume that you are using Raspbian Linux.

This module also works with the JSN-SR04T waterproof sound sensor.

wiring

Python3 Install

Install dependencies;

sudo apt install python3-pip python3-rpi.gpio

The above installs RPi.GPIO 0.7.0. If using Raspberry Pi OS (Bullseye) you may prefer to install RPi.GPIO 0.7.1 with Pip. Either one should work.

To get the latest install it with Pip as follows;

sudo apt remove python3-rpi.gpio  <--0.7.0
sudo pip3 install RPi.GPIO <--0.7.1

Install the hcsr04sensor module

sudo pip3 install hcsr04sensor

Description

The module does the following;

  • Returns an error corrected distance by using the median reading of a sorted sample. NOTE - The default sample size is 11 readings.

    You can specify a different sample size by passing sample_size=x to raw_distance (where x is your desired number of readings). This is useful if you need to lower the sample size to take quicker readings. Beware that the probability of getting erroneous readings increases as sample size is reduced. For my purposes a sample of 11 readings gives a consistent value that I can trust and takes approximately 3 seconds to run with a 0.1 second wait time between individual samples.

    It is also possible to speed up the readings by passing a lower value to sample_wait in raw_distance.

    The lower the value the quicker the invidual samples will be taken. A default of 0.1 is a safe wait time but this can be reduced further. CPU usage increases as faster readings are taken as well as the chance for errors.

    This module uses BCM pin values. See the Raspberry Pi pin layout documentation for your model.

  • Uses BCM pin values by default. BOARD pin values are supported.

  • Adjusts the reading based on temperature by adjusting the speed of sound.

  • Allows measuring distance and depth in metric and imperial units. See;

    pydoc hcsr04sensor.sensor

  • Raises an exception if a faulty cable or sensor prevents an echo pulse from being received.

  • Calculate the volume of different types of containers. See recipes for examples.

Accuracy of Readings

If you need highly accurate readings then this module would not be suitable for your project. In that case you should probably use an Arduino instead of a Raspberry Pi.

Linux is not a Real Time OS so you can expect to get a small variance on each reading, usually within a half cm of the actual value. I say "usually" because every once in a while you can get a reading that is way out of range. This is due to the OS executing other tasks before getting your distance reading. It is why I use a sample of readings. I can always trust that the median of my sample of 11 readings is good.

Highly accurate readings are not required for some applications. For example I use this module in an application I wrote for a sump pump monitor. I am not worried about millimeter accuracy for that application. 1 cm variance on a meter deep pit is close enough to alert me to problems.

Another example would be to calculate the water volume of a drinking water well (standing cylinder shape). It would not matter if you are a gallon off on a 1000 gallon reading.

Distance Limitations

The HCSR04 sensor is suited for short distance readings. The specification manual says it is suitable up to 13 feet. I have tested it to go further than that, but anything over 12 feet starts having periodic strange readings. This module is not suitable for long distances.

Usage

See example scripts in https://github.com/alaudet/hcsr04sensor/tree/master/recipes

Access to Raspberry Pi GPIO pins requires the user account to be in the Linux gpio group.

To add your user account name to the gpio group;

sudo usermod -aG gpio username

Testing the Module

Added a script that is installed to /usr/local/bin called hcsr04.py. This utility does not presently support BOARD pin values. Use BCM pin values.

usage: hcsr04.py [-h] -t TRIG -e ECHO [-sp SPEED] [-ss SAMPLES]

Take a Basic Reading

If you don't want to use a sample of readings for error handling, warnings or pin cleanups I have included a static method in the Measurement class that will return a basic metric reading. It returns the exact one-time reading that is provided by the sensor and RPi.GPIO. This allows you to handle all of these things to your own preference in your code.

See the example basic_reading.py script in https://github.com/alaudet/hcsr04sensor/tree/master/recipes

Contributing

Read the contributing guidelines if you are interested in collaborating on this project.

Donate

Your Donation is Appreciated

Metadata

Release files for hcsr04sensor 1.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for hcsr04sensor 1.7
File Size Uploaded Provenance Actions
hcsr04sensor-1.7.tar.gz 10.6 kB Without provenance Details for hcsr04sensor-1.7.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for hcsr04sensor 1.7
File Interpreter ABI Platform Actions
hcsr04sensor-1.7-py3-none-any.whl Python 3 none any Details for hcsr04sensor-1.7-py3-none-any.whl

Total release size: 18.4 kB

Release files / hcsr04sensor-1.7.tar.gz

Download URL hcsr04sensor-1.7.tar.gz
Size 10.6 kB
Tags Source
SHA-256 checksum
How to use checksums
40bea4112d141ee92f83cfcf7cb31e53d9e288599462cb88d5461e594445b87e
BLAKE2b-256 checksum
How to use checksums
4b56ac541a5b28c35b227b8ad143f610fbf32b5db5dd20785ccf10f545cff1bb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Release files / hcsr04sensor-1.7-py3-none-any.whl

Download URL hcsr04sensor-1.7-py3-none-any.whl
Size 7.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
318392d7c7431d29e8995ac71ddfd9970f57fbb0c52d03a74f332c87f145c844
BLAKE2b-256 checksum
How to use checksums
7d4a70ad3d0027ff611a040fb466407d07de5a32c8e24a34f0748e377aab068d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.11.3

Security for hcsr04sensor 1.7

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.7 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page