HC-SR04 Sensor driver in micropython
Micropython driver for the well-known untrasonic sensor HC-SR04
The driver has been tested on Wemos D1 mini PRO, but It should work on whatever other micropython board, if anyone find problems in other boards, please open an issue and we'll see.
Motivation
The existing drivers in micropython are a bit old and they don't use the relatively new method machine.time_pulse_us() which
Is more accurate that whatever other method using pure python, besides the code is compliant with "standard" micropython,
there is no code for specific boards.
Finally I've added a method, distance_mm() that don't use floating point operations, for environments where there is no
floating point capabilities.
Examples of use:
How to get the distance
The distance_cm() method returns a float with the distance measured by the sensor.
from hcsr04 import HCSR04
sensor = HCSR04(trigger_pin=Pin.P0, echo_pin=Pin.P1)
distance = sensor.distance_cm()
print('Distance:', distance, 'cm')
There is another method, distance_mm(), that returns the distance in milimeters (int type) and no floating point is used, designed
for environments that doesn't support floating point operations.
distance = sensor.distance_mm()
print('Distance:', distance, 'mm')
The default timeout is based on the sensor limit (4m), but we can also define a different timeout, passing the new value in microseconds to the constructor.
from hcsr04 import HCSR04
sensor = HCSR04(trigger_pin=16, echo_pin=0, echo_timeout_us=1000000)
distance = sensor.distance_cm()
print('Distance:', distance, 'cm')
Error management
When the driver reaches the timeout while is listening the echo pin the error is converted to OSError('Out of range')
from hcsr04 import HCSR04
sensor = HCSR04(trigger_pin=16, echo_pin=0, echo_timeout_us=10000)
try:
distance = sensor.distance_cm()
print('Distance:', distance, 'cm')
except OSError as ex:
print('ERROR getting distance:', ex)
Metadata
Release files for mPython-hcsr04 0.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| mPython-hcsr04-0.0.1.tar.gz | 2.3 kB | Without provenance | Details |
Release files / mPython-hcsr04-0.0.1.tar.gz
| Download URL | mPython-hcsr04-0.0.1.tar.gz |
|---|---|
| Size | 2.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6341540828220f8284cc0c95364467ea9fd3707a92e852d0aed1209d4937f1e4
|
|
BLAKE2b-256 checksum How to use checksums |
f38c8d6adb488e04f1630df3c985696e429bd4db61246720b234a1e03946d9c5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2
|
Security for mPython-hcsr04 0.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.