adafruit-circuitpython-hcsr04


Nameadafruit-circuitpython-hcsr04 JSON
Version 0.4.17 PyPI version JSON
download
home_page
SummaryCircuitPython library for controlling HC-SR04 ultrasonic range sensors.
upload_time2023-09-25 15:41:16
maintainer
docs_urlNone
author
requires_python
licenseMIT
keywords adafruit hcsr04 hardware micropython circuitpython ultrasonic
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            Introduction
============

.. image:: https://readthedocs.org/projects/adafruit-circuitpython-hcsr04/badge/?version=latest
    :target: https://docs.circuitpython.org/projects/hcsr04/en/latest/
    :alt: Documentation Status

.. image:: https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_Bundle/main/badges/adafruit_discord.svg
    :target: https://adafru.it/discord
    :alt: Discord

.. image:: https://github.com/adafruit/Adafruit_CircuitPython_HCSR04/workflows/Build%20CI/badge.svg
    :target: https://github.com/adafruit/Adafruit_CircuitPython_HCSR04/actions
    :alt: Build Status

.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
    :target: https://github.com/psf/black
    :alt: Code Style: Black

.. image:: ../docs/_static/3942-02.jpg
    :alt: 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:

* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_

Please ensure all dependencies are available on the CircuitPython filesystem.
This is easily achieved by downloading
`the Adafruit library and driver bundle <https://github.com/adafruit/Adafruit_CircuitPython_Bundle>`_.

Installing from PyPI
====================

On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
PyPI <https://pypi.org/project/adafruit-circuitpython-hcsr04/>`_. To install for current user:

.. code-block:: shell

    pip3 install adafruit-circuitpython-hcsr04

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

.. code-block:: shell

    sudo pip3 install adafruit-circuitpython-hcsr04

To install in a virtual environment in your current project:

.. code-block:: shell

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

Usage Example
=============

.. warning::

    The HC-SR04 uses 5V logic, so you will have to use a `level shifter
    <https://www.adafruit.com/product/2653?q=level%20shifter&>`_ between it
    and your CircuitPython board (which uses 3.3V logic).

.. note::

    If you want to use an HC-SR04 with `MicroPython <http://micropython.org/>`_, I recommend checking out `this library
    <https://github.com/andrey-git/micropython-hcsr04>`_.

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.

.. note::

    It is technically possible to communicate with the HC-SR04 using only one wire since the trigger and echo signals
    aren't ever active at the same time. Once I have a chance to determine a safe way to do this, I plan to add this as
    a feature to the library.

See Also:

    `Adafruit's guide on Lifetime and ContextManagers <https://docs.circuitpython.org/en/latest/docs/design_guide.html#lifetime-and-contextmanagers>`_
        Gives more info on using context managers with CircuitPython drivers.

    `board <https://docs.circuitpython.org/en/latest/shared-bindings/board/__init__.html#module-board>`_
        A list of pins available on your device. To view this list, first `get a REPL
        <http://docs.circuitpython.org/en/latest/docs/pyboard/tutorial/repl.html>`_ (the guide linked was written
        for the pyboard, but it still works), then input the following:

        .. code-block:: python

            import board
            dir(board)

Without a Context Manager
-------------------------

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

.. code-block:: python

    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 <https://docs.python.org/3.4/reference/compound_stmts.html#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.

.. code-block:: python

    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 <https://docs.circuitpython.org/projects/hcsr04/en/latest/>`_.

For information on building library documentation, please check out `this guide <https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/sharing-our-docs-on-readthedocs#sphinx-5-1>`_.

Contributing
============

Contributions are welcome! Please read our `Code of Conduct
<https://github.com/adafruit/Adafruit_CircuitPython_HCSR04/blob/main/CODE_OF_CONDUCT.md>`_
before contributing to help this project stay welcoming.

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "adafruit-circuitpython-hcsr04",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "adafruit,hcsr04,hardware,micropython,circuitpython,ultrasonic",
    "author": "",
    "author_email": "Adafruit Industries <circuitpython@adafruit.com>",
    "download_url": "https://files.pythonhosted.org/packages/e3/79/ac29f89344ada0288fedf611d28fb061aae6e794f42c18916696c4cfa6c3/adafruit-circuitpython-hcsr04-0.4.17.tar.gz",
    "platform": null,
    "description": "Introduction\n============\n\n.. image:: https://readthedocs.org/projects/adafruit-circuitpython-hcsr04/badge/?version=latest\n    :target: https://docs.circuitpython.org/projects/hcsr04/en/latest/\n    :alt: Documentation Status\n\n.. image:: https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_Bundle/main/badges/adafruit_discord.svg\n    :target: https://adafru.it/discord\n    :alt: Discord\n\n.. image:: https://github.com/adafruit/Adafruit_CircuitPython_HCSR04/workflows/Build%20CI/badge.svg\n    :target: https://github.com/adafruit/Adafruit_CircuitPython_HCSR04/actions\n    :alt: Build Status\n\n.. image:: https://img.shields.io/badge/code%20style-black-000000.svg\n    :target: https://github.com/psf/black\n    :alt: Code Style: Black\n\n.. image:: ../docs/_static/3942-02.jpg\n    :alt: HC-SR04 Product Image\n\nThe HC-SR04 is an inexpensive solution for measuring distances using microcontrollers. This library provides a simple\ndriver for controlling these sensors from CircuitPython.\n\nDependencies\n=============\nThis driver depends on:\n\n* `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_\n\nPlease ensure all dependencies are available on the CircuitPython filesystem.\nThis is easily achieved by downloading\n`the Adafruit library and driver bundle <https://github.com/adafruit/Adafruit_CircuitPython_Bundle>`_.\n\nInstalling from PyPI\n====================\n\nOn supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from\nPyPI <https://pypi.org/project/adafruit-circuitpython-hcsr04/>`_. To install for current user:\n\n.. code-block:: shell\n\n    pip3 install adafruit-circuitpython-hcsr04\n\nTo install system-wide (this may be required in some cases):\n\n.. code-block:: shell\n\n    sudo pip3 install adafruit-circuitpython-hcsr04\n\nTo install in a virtual environment in your current project:\n\n.. code-block:: shell\n\n    mkdir project-name && cd project-name\n    python3 -m venv .venv\n    source .venv/bin/activate\n    pip3 install adafruit-circuitpython-hcsr04\n\nUsage Example\n=============\n\n.. warning::\n\n    The HC-SR04 uses 5V logic, so you will have to use a `level shifter\n    <https://www.adafruit.com/product/2653?q=level%20shifter&>`_ between it\n    and your CircuitPython board (which uses 3.3V logic).\n\n.. note::\n\n    If you want to use an HC-SR04 with `MicroPython <http://micropython.org/>`_, I recommend checking out `this library\n    <https://github.com/andrey-git/micropython-hcsr04>`_.\n\nYou'll need to dedicate two pins to communicating with the HC-SR04. The sensor communicates in a very rudimentary\nmanner, so it doesn't matter which pins you choose, as long as they're digital IO pins (pins that start with \"``D``\"\nare digital).\n\nThere are two ways of instantiating a `HCSR04` object: with or without using a context manager.\n\n.. note::\n\n    It is technically possible to communicate with the HC-SR04 using only one wire since the trigger and echo signals\n    aren't ever active at the same time. Once I have a chance to determine a safe way to do this, I plan to add this as\n    a feature to the library.\n\nSee Also:\n\n    `Adafruit's guide on Lifetime and ContextManagers <https://docs.circuitpython.org/en/latest/docs/design_guide.html#lifetime-and-contextmanagers>`_\n        Gives more info on using context managers with CircuitPython drivers.\n\n    `board <https://docs.circuitpython.org/en/latest/shared-bindings/board/__init__.html#module-board>`_\n        A list of pins available on your device. To view this list, first `get a REPL\n        <http://docs.circuitpython.org/en/latest/docs/pyboard/tutorial/repl.html>`_ (the guide linked was written\n        for the pyboard, but it still works), then input the following:\n\n        .. code-block:: python\n\n            import board\n            dir(board)\n\nWithout a Context Manager\n-------------------------\n\nIn the example below, we create the `HCSR04` object directly, get the distance every 2 seconds.\n\n.. code-block:: python\n\n    import time\n    import board\n    import adafruit_hcsr04\n\n    sonar = adafruit_hcsr04.HCSR04(trigger_pin=board.D5, echo_pin=board.D6)\n\n    while True:\n        try:\n            print((sonar.distance,))\n        except RuntimeError:\n            print(\"Retrying!\")\n        time.sleep(2)\n\n\nWith a Context Manager\n----------------------\n\nIn the example below, we use a context manager (the `with <https://docs.python.org/3.4/reference/compound_stmts.html#with>`_ statement) to create the `HCSR04`\ninstance, again get the distance every 2 seconds, but then the context manager handles de-initializing the device for\nus.\n\n.. code-block:: python\n\n    import board\n    from adafruit_hcsr04 import HCSR04\n    with HCSR04(trigger_pin=board.D5, echo_pin=board.D6) as sonar:\n        try:\n            while True:\n                print(sonar.distance)\n                sleep(2)\n        except KeyboardInterrupt:\n            pass\n\n\nDocumentation\n=============\n\nAPI documentation for this library can be found on `Read the Docs <https://docs.circuitpython.org/projects/hcsr04/en/latest/>`_.\n\nFor information on building library documentation, please check out `this guide <https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/sharing-our-docs-on-readthedocs#sphinx-5-1>`_.\n\nContributing\n============\n\nContributions are welcome! Please read our `Code of Conduct\n<https://github.com/adafruit/Adafruit_CircuitPython_HCSR04/blob/main/CODE_OF_CONDUCT.md>`_\nbefore contributing to help this project stay welcoming.\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "CircuitPython library for controlling HC-SR04 ultrasonic range sensors.",
    "version": "0.4.17",
    "project_urls": {
        "Homepage": "https://github.com/adafruit/Adafruit_CircuitPython_HCSR04"
    },
    "split_keywords": [
        "adafruit",
        "hcsr04",
        "hardware",
        "micropython",
        "circuitpython",
        "ultrasonic"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "411deab671a31d19958b041aeb538fffe27368799ea36a8c61bedc57a4f56835",
                "md5": "e95be27b0adf57d7644bdc99adfa3b9d",
                "sha256": "8b618a759afcd86cdac234fec9d8d23ac41b04af266cdfa6abc081316eac8069"
            },
            "downloads": -1,
            "filename": "adafruit_circuitpython_hcsr04-0.4.17-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e95be27b0adf57d7644bdc99adfa3b9d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6912,
            "upload_time": "2023-09-25T15:41:15",
            "upload_time_iso_8601": "2023-09-25T15:41:15.306868Z",
            "url": "https://files.pythonhosted.org/packages/41/1d/eab671a31d19958b041aeb538fffe27368799ea36a8c61bedc57a4f56835/adafruit_circuitpython_hcsr04-0.4.17-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e379ac29f89344ada0288fedf611d28fb061aae6e794f42c18916696c4cfa6c3",
                "md5": "7b25815d48450ae6b8a0e4d583e1974f",
                "sha256": "423c307f50de91a77531819e07d22acb3d9dc8a6d9dffb34537c022ed766e4ac"
            },
            "downloads": -1,
            "filename": "adafruit-circuitpython-hcsr04-0.4.17.tar.gz",
            "has_sig": false,
            "md5_digest": "7b25815d48450ae6b8a0e4d583e1974f",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 283981,
            "upload_time": "2023-09-25T15:41:16",
            "upload_time_iso_8601": "2023-09-25T15:41:16.284309Z",
            "url": "https://files.pythonhosted.org/packages/e3/79/ac29f89344ada0288fedf611d28fb061aae6e794f42c18916696c4cfa6c3/adafruit-circuitpython-hcsr04-0.4.17.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-09-25 15:41:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "adafruit",
    "github_project": "Adafruit_CircuitPython_HCSR04",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "lcname": "adafruit-circuitpython-hcsr04"
}
        
Elapsed time: 0.12531s