Skip to main content

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.

Release files for adafruit-circuitpython-hcsr04 0.4.25

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

Source distribution (sdist)

Source distribution for adafruit-circuitpython-hcsr04 0.4.25
File Size Uploaded
adafruit_circuitpython_hcsr04-0.4.25.tar.gz 281.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for adafruit-circuitpython-hcsr04 0.4.25
File Interpreter ABI Platform
adafruit_circuitpython_hcsr04-0.4.25-py3-none-any.whl Python 3 none any Details

Total release size: 288.1 kB

Release files / adafruit_circuitpython_hcsr04-0.4.25.tar.gz

Download URL adafruit_circuitpython_hcsr04-0.4.25.tar.gz
Size 281.2 kB
Tags Source
SHA-256 checksum
How to use checksums
dc38f4cdc4ed900e7879ee6256cb1f72b3be977ef89e6a50ec3da208f218f33c
BLAKE2b-256 checksum
How to use checksums
852ff641f94931ac3215985042cb389ee7fe25e785fa9eda0d8e489e485e8bfa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release files / adafruit_circuitpython_hcsr04-0.4.25-py3-none-any.whl

Download URL adafruit_circuitpython_hcsr04-0.4.25-py3-none-any.whl
Size 6.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
b7aab62bbbb883569d8ed1afcc01217ba30d27922698cd0d35cc546b22569bab
BLAKE2b-256 checksum
How to use checksums
23cfdc7cd9118b8c431d5c0d21c0b52e1d7c8b19a53b2a493a233baff6d0fb6c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.11.15

Release history Release notifications | RSS feed

This release

0.4.25 This release

2 release files

0.4.24

2 release files

0.4.23

2 release files

0.4.22

2 release files

0.4.21

2 release files

0.4.20

2 release files

0.4.17

2 release files

0.4.16

2 release files

0.4.15

2 release files

0.4.14

2 release files

0.4.12

2 release files

0.4.10

1 release file

0.4.9

1 release file

0.4.8

1 release file

0.4.7

1 release file

0.4.6

1 release file

0.4.5

1 release file

0.4.4

1 release file

0.4.3

1 release file

0.4.2

1 release file

0.4.1

1 release file

0.4.0

1 release file

0.3.6

1 release file

0.3.4

1 release file

0.3.3

1 release file

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