Simple test

Ensure your device works with this simple test.

examples/vl53lxcx_simpletest.py
 1# SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2# SPDX-FileCopyrightText: Copyright (c) 2025 senseBox for senseBox
 3#
 4# SPDX-License-Identifier: Unlicense
 5
 6import board
 7import busio
 8from digitalio import DigitalInOut, Direction
 9
10from vl53lxcx import (
11    DATA_DISTANCE_MM,
12    DATA_TARGET_STATUS,
13    RESOLUTION_8X8,
14    STATUS_VALID,
15    VL53L8CX,
16)
17
18lpn_pin = board.D3
19i2c = busio.I2C(board.SCL, board.SDA, frequency=1_000_000)
20
21lpn = DigitalInOut(lpn_pin)
22lpn.direction = Direction.OUTPUT
23lpn.value = True
24
25tof = VL53L8CX(i2c, lpn=lpn)
26
27
28def main():
29    tof.reset()
30
31    if not tof.is_alive():
32        raise ValueError("VL53L8CX not detected")
33
34    tof.init()
35
36    tof.resolution = RESOLUTION_8X8
37    grid = 7
38
39    tof.ranging_freq = 2
40
41    tof.start_ranging({DATA_DISTANCE_MM, DATA_TARGET_STATUS})
42
43    while True:
44        if tof.check_data_ready():
45            results = tof.get_ranging_data()
46            distance = results.distance_mm
47            status = results.target_status
48
49            for i, d in enumerate(distance):
50                if status[i] == STATUS_VALID:
51                    print(f"{d:4}", end=" ")
52                else:
53                    print("xxxx", end=" ")
54
55                if (i & grid) == grid:
56                    print("")
57
58            print("")
59
60
61main()