Skip to main content

Raspberry Pi Python Library for Ultrasonic Module HC-SR04 Distance Measuring Transducer.

Project description

This sensor uses sound waves to provide a means to measure the distance between the sensor and an object. It is not the most accurate distance sensor available, but many projects do not need pinpoint accuracy. After a quick look at Banggood website, you can get five HC-SR04 sensors for 5.07 GBP (6.86 USD). And while the sensor is not the most compact, its low price means a robot vehicle can have a full sensor kit fitted very cheaply.

You can find the article here for more details:

Article.

Example Code

Simplest Program for Distance Measuring:

# Import necessary libraries.
from Bluetin_Echo import Echo

# Define GPIO pin constants.
TRIGGER_PIN = 16
ECHO_PIN = 12
# Initialise Sensor with pins, speed of sound.
speed_of_sound = 315
echo = Echo(TRIGGER_PIN, ECHO_PIN, speed_of_sound)
# Measure Distance 5 times, return average.
samples = 5
result = echo.read('cm', samples)
# Print result.
print(result, 'cm')
# Reset GPIO Pins.
echo.stop()

Example for Taking Multiple Distance Measurements:

"""File: echo_loop.py"""
# Import necessary libraries.
from Bluetin_Echo import Echo

# Define GPIO pin constants.
TRIGGER_PIN = 16
ECHO_PIN = 12
# Initialise Sensor with pins, speed of sound.
speed_of_sound = 315
echo = Echo(TRIGGER_PIN, ECHO_PIN, speed_of_sound)
# Measure Distance 5 times, return average.
samples = 5
# Take multiple measurements.
for counter in range(0, 10):
    result = echo.read('cm', samples)
    # Print result.
    print(result, 'cm')

# Reset GPIO Pins.
echo.stop()

Loop Through Multiple Sensors

"""File: echo_multi_sensor.py"""
# Import necessary libraries.
from time import sleep

from Bluetin_Echo import Echo

# Define pin constants
TRIGGER_PIN_1 = 16
ECHO_PIN_1 = 12
TRIGGER_PIN_2 = 26
ECHO_PIN_2 = 19

# Initialise two sensors.
echo = [Echo(TRIGGER_PIN_1, ECHO_PIN_1)
        , Echo(TRIGGER_PIN_2, ECHO_PIN_2)]

def main():
    sleep(0.1)
    for counter in range(1, 6):
        for counter2 in range(0, len(echo)):
            result = echo[counter2].read('cm', 3)
            print('Sensor {} - {} cm'.format(counter2, round(result,2)))

    echo[0].stop()

if __name__ == '__main__':
    main()

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Bluetin_Echo-0.1.1.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

Bluetin_Echo-0.1.1-py3-none-any.whl (6.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page