From 02d3d853f8ce27a1160dab2f6e5480013ea37844 Mon Sep 17 00:00:00 2001 From: Phil Howard Date: Wed, 20 Apr 2022 19:19:51 +0100 Subject: [PATCH] VL53L5CX: MicroPython example. --- .../breakout_vl53l5cx/vl53l5cx_demo.py | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 micropython/examples/breakout_vl53l5cx/vl53l5cx_demo.py diff --git a/micropython/examples/breakout_vl53l5cx/vl53l5cx_demo.py b/micropython/examples/breakout_vl53l5cx/vl53l5cx_demo.py new file mode 100644 index 00000000..d38296ba --- /dev/null +++ b/micropython/examples/breakout_vl53l5cx/vl53l5cx_demo.py @@ -0,0 +1,32 @@ +import pimoroni_i2c +import breakout_vl53l5cx +import time + +PINS_BREAKOUT_GARDEN = {"sda": 4, "scl": 5} +PINS_PICO_EXPLORER = {"sda": 20, "scl": 21} + +# Sensor startup time is proportional to i2c baudrate +# HOWEVER many sensors may not run at > 400KHz (400000) +i2c = pimoroni_i2c.PimoroniI2C(**PINS_BREAKOUT_GARDEN, baudrate=2_000_000) + +print("Starting up sensor...") +t_sta = time.ticks_ms() +sensor = breakout_vl53l5cx.VL53L5CX(i2c) +t_end = time.ticks_ms() +print("Done in {}ms...".format(t_end - t_sta)) + +# Make sure to set resolution and other settings *before* you start ranging +sensor.set_resolution(breakout_vl53l5cx.RESOLUTION_4X4) +sensor.start_ranging() + +while True: + if sensor.data_ready(): + # "data" is a namedtuple (attrtuple technically) + # it includes average readings as "distance_avg" and "reflectance_avg"a + # plus a full 4x4 or 8x8 set of readings (as a 1d tuple) for both values. + data = sensor.get_data() + print("{}mm {}% (avg: {}mm {}%)".format( + data.distance[0], + data.reflectance[0], + data.distance_avg, + data.reflectance_avg))