Skip to main content

CircuitPython library for controlling HC-SR04 ultrasonic range sensors.

Project description

Introduction

Documentation Status Discord Build Status Code Style: Ruff HC-SR04 Product Image

The HC-SR04 is an inexpensive solution for measuring distances using microcontrollers. This library provides a simple driver for controlling these sensors from CircuitPython.

Dependencies

This driver depends on:

Please ensure all dependencies are available on the CircuitPython filesystem. This is easily achieved by downloading the Adafruit library and driver bundle.

Installing from PyPI

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally from PyPI. To install for current user:

pip3 install adafruit-circuitpython-hcsr04

To install system-wide (this may be required in some cases):

sudo pip3 install adafruit-circuitpython-hcsr04

To install in a virtual environment in your current project:

mkdir project-name && cd project-name
python3 -m venv .venv
source .venv/bin/activate
pip3 install adafruit-circuitpython-hcsr04

Usage Example

You’ll need to dedicate two pins to communicating with the HC-SR04. The sensor communicates in a very rudimentary manner, so it doesn’t matter which pins you choose, as long as they’re digital IO pins (pins that start with “D” are digital).

There are two ways of instantiating a HCSR04 object: with or without using a context manager.

See Also:

Adafruit’s guide on Lifetime and ContextManagers

Gives more info on using context managers with CircuitPython drivers.

board

A list of pins available on your device. To view this list, first get a REPL (the guide linked was written for the pyboard, but it still works), then input the following:

import board
dir(board)

Without a Context Manager

In the example below, we create the HCSR04 object directly, get the distance every 2 seconds.

import time
import board
import adafruit_hcsr04

sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.D5, echo_pin=board.D6)

while True:
    try:
        print((sonar.distance,))
    except RuntimeError:
        print("Retrying!")
    time.sleep(2)

With a Context Manager

In the example below, we use a context manager (the with statement) to create the HCSR04 instance, again get the distance every 2 seconds, but then the context manager handles de-initializing the device for us.

import board
from adafruit_hcsr04 import HCSR04
with HCSR04(trigger_pin=board.D5, echo_pin=board.D6) as sonar:
    try:
        while True:
            print(sonar.distance)
            sleep(2)
    except KeyboardInterrupt:
        pass

Documentation

API documentation for this library can be found on Read the Docs.

For information on building library documentation, please check out this guide.

Contributing

Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.

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

adafruit_circuitpython_hcsr04-0.4.24.tar.gz (281.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file adafruit_circuitpython_hcsr04-0.4.24.tar.gz.

File metadata

File hashes

Hashes for adafruit_circuitpython_hcsr04-0.4.24.tar.gz
Algorithm Hash digest
SHA256 9f173cd2dcc943ff6e21ff01b91930392bf1ee8d06ca18b680a7458aaed50dcc
MD5 9b4c0b494a54c8a4a83f92e9ee475421
BLAKE2b-256 b8e2504f6576370de41a96b79350d37869b759e82aafe882fa6fc7168eeba924

See more details on using hashes here.

File details

Details for the file adafruit_circuitpython_hcsr04-0.4.24-py3-none-any.whl.

File metadata

File hashes

Hashes for adafruit_circuitpython_hcsr04-0.4.24-py3-none-any.whl
Algorithm Hash digest
SHA256 7fef6fca3a7f551d2b01036d1606a51994f0c96537761e1f025548f941ab2485
MD5 28a0c3fe49ba522a9c3a4c8d3a2fe79c
BLAKE2b-256 1a7a56e9453c7fad3e6dcbc21738a99876a586ee8ac2e8ce65616e5ef173b28d

See more details on using hashes here.

Supported by

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