Donate Your donations keep this site ad free -- Thank You
Fork Star Issue Watch
Use Python to calculate distance and depth measurements with an HCSR04 Ultrasonic Sound Sensor and a Raspberry Pi. The module also works with a waterproof JSN-SR04T sensor.
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.
This module supports BCM and BOARD pin values but uses BCM by default. See the Raspberry Pi pin layout documentation for your model.
Rounds the value to a specified decimal place.
Adjusts the reading based on temperature by adjusting the speed of sound.
Allows measuring distance or depth in metric and imperial units.
Raises an exception if a faulty cable or sensor prevents an echo pulse from being received.
GPIO pins are rated for 3.3V so you must insert a voltage divider as the power pin on the PI is 5V. In the above diagram a 470 Ohm resistor is soldered on the echo wire. A 1000 Ohm resistor is soldered between the echo and ground wires. This reduces voltage to GPIO pin 27 to 3.4V which is within a tolerable level. Failure to do this can damage your board.
sudo pip3 install hcsr04sensorPython 2
sudo pip install hcsr04sensor
pi@raspberry:~ $ hcsr04.py -h usage: hcsr04.py [-h] -t TRIG -e ECHO [-sp SPEED] [-ss SAMPLES] Script tests the HCSR04 sensor under different configurations optional arguments: -h, --help show this help message and exit -t TRIG, --trig TRIG Trig Pin (Required - must be an integer) -e ECHO, --echo ECHO Echo Pin (Required - must be an integer) -sp SPEED, --speed SPEED Time between individual reading samples (Optional - must be a float, default is 0.1 seconds) -ss SAMPLES, --samples SAMPLES Reading Sample Size (Optional - must be an integer, default is 11)Sample Output
pi@raspberry:~ $ hcsr04.py -t 17 -e 27 trig pin = gpio 17 echo pin = gpio 27 speed = 0.1 samples = 11 The imperial distance is 12.3 inches. The metric distance is 31.2 centimetres.
Python 3.4.2 (default, Oct 19 2014, 13:31:11) [GCC 4.9.1] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from hcsr04sensor import sensor >>> value = sensor.Measurement(17, 27) >>> value.raw_distance() 31.761826466140448Measure a rounded distance in centimeters.
>>> raw_distance = value.raw_distance() >>> distance_cm = value.distance_metric(raw_distance) >>> distance_cm 31.8Measure a rounded distance in inches.
>>> distance_inches = value.distance_imperial(raw_distance) >>> distance_inches 12.5Measure the depth of a liquid in a hole 50 centimeters deep.
>>> depth_metric = value.depth_metric(raw_distance, 50) >>> depth_metric 18.2Measure the depth of a liquid in a hole 20 inches deep.
>>> depth_imperial = value.depth_imperial(raw_distance, 20) >>> depth_imperial 7.5You can also use a stripped down function that does not use the median reading for error correction, does not set the warnings and does not clean up the pins. You do all of this yourself in your code. The basic_distance function can be used for this.
>>> import RPi.GPIO as GPIO >>> from hcsr04sensor import sensor >>> GPIO.setmode(GPIO.BCM) >>> sensor.basic_distance(17, 27) 27.964972806398293 >>> GPIO.cleanup((17, 27))
Various recipes for measuring distance or liquid depth.
HCSR04 Specification Manual