Example 1: Basic Distance Measurement

examples/Example1_ReadDistance.py
 1#-----------------------------------------------------------------------
 2# VL53L1X - Example 1
 3#-----------------------------------------------------------------------
 4#
 5# Ported by  SparkFun Electronics, October 2019
 6# Author: Nathan Seidle
 7# Ported: Wes Furuya
 8# SparkFun Electronics
 9# 
10# License: This code is public domain but you buy me a beer if you use
11# this and we meet someday (Beerware license).
12#
13# Compatibility: https://www.sparkfun.com/products/14722
14# 
15# Do you like this library? Help support SparkFun. Buy a board!
16# For more information on VL53L1x (ToF), check out the product page
17# linked above.
18#
19# This program is distributed in the hope that it will be useful, but
20# WITHOUT ANY WARRANTY without even the implied warranty of
21# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22# General Public License for more details.
23#
24# You should have received a copy of the GNU General Public License
25# along with this program.  If not, see <http:www.gnu.org/licenses/>.
26#
27#=======================================================================
28# Copyright (c) 2019 SparkFun Electronics
29#
30# Permission is hereby granted, free of charge, to any person obtaining
31# a copy of this software and associated documentation files (the
32# "Software"), to deal in the Software without restriction, including
33# without limitation the rights to use, copy, modify, merge, publish,
34# distribute, sublicense, and/or sell copies of the Software, and to
35# permit persons to whom the Software is furnished to do so, subject to
36# the following conditions:
37#
38# The above copyright notice and this permission notice shall be
39# included in all copies or substantial portions of the Software.
40#
41# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
42# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
43# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
44# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
45# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
46# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
47# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
48#=======================================================================
49
50"""
51	Reading distance from the laser based VL53L1X
52
53	This example prints the distance to an object. If you are getting weird
54	readings, be sure the vacuum tape has been removed from the sensor.
55"""
56
57import qwiic
58import time
59
60print("VL53L1X Qwiic Test\n")
61ToF = qwiic.QwiicVL53L1X()
62if (ToF.sensor_init() == None):					 # Begin returns 0 on a good init
63	print("Sensor online!\n")
64
65while True:
66	try:
67		ToF.start_ranging()						 # Write configuration bytes to initiate measurement
68		time.sleep(.005)
69		distance = ToF.get_distance()	 # Get the result of the measurement from the sensor
70		time.sleep(.005)
71		ToF.stop_ranging()
72
73		distanceInches = distance / 25.4
74		distanceFeet = distanceInches / 12.0
75
76		print("Distance(mm): %s Distance(ft): %s" % (distance, distanceFeet))
77
78	except Exception as e:
79		print(e)