File tree Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Expand file tree Collapse file tree 2 files changed +50
-0
lines changed Original file line number Diff line number Diff line change 41
41
42
42
__version__ = "0.0.0-auto.0"
43
43
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_US100.git"
44
+
45
+ class US100 :
46
+ """Control a US-100 ultrasonic range sensor."""
47
+
48
+ def __init__ (self , uart ):
49
+ self ._uart = uart
50
+
51
+ @property
52
+ def distance (self ):
53
+ """Return the distance measured by the sensor in cm.
54
+ This is the function that will be called most often in user code.
55
+ If no signal is received, we'll throw a RuntimeError exception. This means
56
+ either the sensor was moving too fast to be pointing in the right
57
+ direction to pick up the ultrasonic signal when it bounced back (less
58
+ likely), or the object off of which the signal bounced is too far away
59
+ for the sensor to handle. In my experience, the sensor can detect
60
+ objects over 460 cm away.
61
+ :return: Distance in centimeters.
62
+ :rtype: float
63
+ """
64
+ self ._uart .write (bytes ([0x55 ]))
65
+ data = self ._uart .read (2 ) # 2 bytes return for distance
66
+ if len (data ) != 2 :
67
+ raise RuntimeError ("Did not receive distance response" )
68
+ dist = (data [1 ] + (data [0 ] << 8 )) / 10
69
+ return dist
70
+
71
+ @property
72
+ def temperature (self ):
73
+ """Return the on-chip temperature, in Celsius"""
74
+ self ._uart .write (bytes ([0x50 ]))
75
+ data = self ._uart .read (1 ) # 1 byte return for temp
76
+ if len (data ) != 1 :
77
+ raise RuntimeError ("Did not receive temperature response" )
78
+ temp = data [0 ] - 45
79
+ return temp
Original file line number Diff line number Diff line change
1
+ import time
2
+ import board
3
+ import busio
4
+ import adafruit_us100
5
+
6
+ uart = busio .UART (board .TX , board .RX , baudrate = 9600 )
7
+ # Create a US-100 module instance.
8
+ us100 = adafruit_us100 .US100 (uart )
9
+
10
+ while True :
11
+ print ("-----" )
12
+ print ("Temperature: " , us100 .temperature )
13
+ print ("Distance: " , us100 .distance )
14
+ time .sleep (0.5 )
You can’t perform that action at this time.
0 commit comments