CircuitPython library for controlling HC-SR04 ultrasonic range sensors.
Project description
Introduction
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 .env
source .env/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, then de-initialize the device.
from adafruit_hcsr04 import HCSR04 sonar = HCSR04(trig, echo) try: while True: print(sonar.dist_cm()) sleep(2) except KeyboardInterrupt: pass sonar.deinit()
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.
from adafruit_hcsr04 import HCSR04 with HCSR04(trig, echo) as sonar: try: while True: print(sonar.dist_cm()) sleep(2) except KeyboardInterrupt: pass
Contributing
Contributions are welcome! Please read our Code of Conduct before contributing to help this project stay welcoming.
Documentation
For information on building library documentation, please check out this guide.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file adafruit-circuitpython-hcsr04-0.3.6.tar.gz
.
File metadata
- Download URL: adafruit-circuitpython-hcsr04-0.3.6.tar.gz
- Upload date:
- Size: 277.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.8.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 03e1df204c5aa6fbf26356abe6780d6dc80d796fb7bd2a0c0f0ead39f2175706 |
|
MD5 | 555efd30d5018d51e102c073a5dadabe |
|
BLAKE2b-256 | 1d5d5187a1a75ec9ff9188622ead5cb308bf82ac889a515438be9fa6748f6457 |