-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathadafruit_us100.py
106 lines (85 loc) · 3.64 KB
/
adafruit_us100.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# SPDX-FileCopyrightText: 2018 ladyada for Adafruit Industries
#
# SPDX-License-Identifier: MIT
"""
`adafruit_us100`
====================================================
CircuitPython library for reading distance and temperature via US-100 ultra-sonic sensor
* Author(s): ladyada
Implementation Notes
--------------------
**Software and Dependencies:**
* Adafruit CircuitPython firmware for the supported boards:
https://github.com/adafruit/circuitpython/releases
"""
__version__ = "0.0.0+auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_US100.git"
import time
try:
from typing import Optional
from busio import UART
except ImportError:
pass
class US100:
"""Control a US-100 ultrasonic range sensor."""
def __init__(self, uart: UART) -> None:
self._uart = uart
# Some chips, such as ESP32-S3, may have junk input bytes immediately after UART creation.
# Wait enough time for those to appear, and then clear the buffer.
time.sleep(0.1)
uart.reset_input_buffer()
@property
def distance(self) -> Optional[float]:
"""Return the distance measured by the sensor in cm.
This is the function that will be called most often in user code.
If no signal is received, return ``None``. This can happen when the
object in front of the sensor is too close, the wiring is incorrect or the
sensor is not found. If the signal received is not 2 bytes, return ``None``.
This means either the sensor was moving too fast to be pointing in the right
direction to pick up the ultrasonic signal when it bounced back (less
likely), or the object off of which the signal bounced is too far away
for the sensor to handle. In my experience, the sensor can not detect
objects over 460 cm away.
:return: Distance in centimeters.
:rtype: float or None
May block for up to 400 msecs while attempting a read.
Will always block for at least 100 msecs.
"""
for _ in range(2): # Attempt to read twice.
self._uart.write(bytes([0x55]))
time.sleep(0.1)
data = self._uart.read(2) # 2 byte return for distance.
if data: # If there is a reading, exit the loop.
break
time.sleep(0.1) # You need to wait between readings, so delay is included.
else:
# Loop exited normally, so read failed twice.
# This can happen when the object in front of the sensor is too close, if the wiring
# is incorrect or the sensor is not found.
return None
if len(data) != 2:
return None
dist = (data[1] + (data[0] << 8)) / 10
return dist
@property
def temperature(self) -> Optional[int]:
"""Return the on-chip temperature, in Celsius
May block for up to 200 msecs while attempting a read.
Will always block for at least 100 msecs.
"""
for _ in range(2): # Attempt to read twice.
self._uart.write(bytes([0x50]))
time.sleep(0.1)
data = self._uart.read(1) # 1 byte return for temp
if data: # If there is a reading, exit the loop.
break
time.sleep(0.1) # You need to wait between readings, so delay is included.
else:
# Loop exited normally, so read failed twice.
# This can happen when the object in front of the sensor is too close, if the wiring
# is incorrect or the sensor is not found.
return None
if len(data) != 1:
return None
temp = data[0] - 45
return temp