Simple Test

Ensure your device works with this simple test.

examples/vl6180x_simpletest.py
 1# SPDX-FileCopyrightText: 2018 Tony DiCola for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# Demo of reading the range and lux from the VL6180x distance sensor and
 5# printing it every second.
 6
 7import time
 8
 9import board
10import busio
11
12import adafruit_vl6180x
13
14# Create I2C bus.
15i2c = busio.I2C(board.SCL, board.SDA)
16
17# Create sensor instance.
18sensor = adafruit_vl6180x.VL6180X(i2c)
19# You can add an offset to distance measurements here (e.g. calibration)
20# Swapping for the following would add a +10 millimeter offset to measurements:
21# sensor = adafruit_vl6180x.VL6180X(i2c, offset=10)
22
23# Main loop prints the range and lux every second:
24while True:
25    # Read the range in millimeters and print it.
26    range_mm = sensor.range
27    print(f"Range: {range_mm}mm")
28    # Read the light, note this requires specifying a gain value:
29    # - adafruit_vl6180x.ALS_GAIN_1 = 1x
30    # - adafruit_vl6180x.ALS_GAIN_1_25 = 1.25x
31    # - adafruit_vl6180x.ALS_GAIN_1_67 = 1.67x
32    # - adafruit_vl6180x.ALS_GAIN_2_5 = 2.5x
33    # - adafruit_vl6180x.ALS_GAIN_5 = 5x
34    # - adafruit_vl6180x.ALS_GAIN_10 = 10x
35    # - adafruit_vl6180x.ALS_GAIN_20 = 20x
36    # - adafruit_vl6180x.ALS_GAIN_40 = 40x
37    light_lux = sensor.read_lux(adafruit_vl6180x.ALS_GAIN_1)
38    print(f"Light (1x gain): {light_lux}lux")
39    # Delay for a second.
40    time.sleep(1.0)

Calibration Test

Demo of calibrating the part to part range offset per Application Note 4545 for the VL6180X sensor.

examples/vl6180x_calibrationtest.py
 1# SPDX-FileCopyrightText: 2018 Tony DiCola for Adafruit Industries
 2# SPDX-License-Identifier: MIT
 3
 4# Demo of calibrating the part to part range offset per Application Note 4545
 5# for the VL6180X sensor
 6
 7import time
 8
 9import board
10import busio
11
12import adafruit_vl6180x
13
14# Create I2C bus.
15i2c = busio.I2C(board.SCL, board.SDA)
16
17# Create sensor instance, with explicit offset of 0 to clear the system offset
18sensor = adafruit_vl6180x.VL6180X(i2c, offset=0)
19
20# Place a target at 50mm away from VL6180X Collect a number of range measurements
21# with the target in place and calculate mean of the range results.  For a
22# reliable measurement, take at least 10 measurements.
23measurements = []
24for msmt in range(10):
25    range_mm = sensor.range
26    measurements.append(range_mm)
27    time.sleep(1.0)
28average_msmt = sum(measurements) / 10
29
30# Calculate the offset required:
31calibration_offset = 50 - average_msmt
32
33# Apply offset
34sensor.offset = calibration_offset

Continuous Test

Demo of reading the range from the VL6180x distance sensor in continuous mode.

examples/vl6180x_continuoustest.py
 1# SPDX-FileCopyrightText: 2018 Jonas Schatz
 2# SPDX-License-Identifier: MIT
 3
 4# Demo of reading the range from the VL6180x distance sensor in
 5# continuous mode
 6
 7import time
 8
 9import board
10import busio
11
12import adafruit_vl6180x
13
14# Create I2C bus.
15i2c = busio.I2C(board.SCL, board.SDA)
16
17# Create sensor instance.
18sensor = adafruit_vl6180x.VL6180X(i2c)
19
20# Starting continuous mode
21print("Starting continuous mode")
22sensor.start_range_continuous(20)
23
24# Main loop prints the range and lux every 0.01 seconds
25for _ in range(100):
26    # Read the range in millimeters and print it.
27    range_mm = sensor.range
28    print(f"Range: {range_mm}mm")
29
30    # Delay for 10 ms
31    time.sleep(0.01)
32
33# Stop continuous mode. This is advised as the sensor
34# wouldn't stop measuring after the program has ended
35sensor.stop_range_continuous()

History Test

Demo of reading the range from the history buffer of the VL6180x distance sensor.

examples/vl6180x_historytest.py
 1# SPDX-FileCopyrightText: 2022 Jonas Schatz
 2# SPDX-License-Identifier: MIT
 3
 4# Demo of reading the range from the history buffer of the VL6180x
 5# distance sensor
 6
 7import time
 8
 9import board
10import busio
11
12import adafruit_vl6180x
13
14# Create I2C bus.
15i2c = busio.I2C(board.SCL, board.SDA)
16
17# Create sensor instance.
18sensor = adafruit_vl6180x.VL6180X(i2c)
19
20# Starting continuous mode
21print("Starting continuous mode")
22sensor.start_range_continuous()
23
24# Main loop prints the ranges every 0.01 seconds for about 5 seconds
25# You should see changes 'ripple through' the history array
26for _ in range(500):
27    # Read the last 16 ranges from the history buffer as a List[int]
28    ranges_mm = sensor.ranges_from_history
29    print(ranges_mm)
30
31    # Delay for 10 ms so that the loop is not too fast
32    time.sleep(0.01)
33
34# Stop continuous mode. This is advised as the sensor
35# wouldn't stop measuring after the program has ended
36sensor.stop_range_continuous()

Performance Test

Demo of reading the range from the VL6180x distance sensor in different access modes (single shot, continuous, history).

examples/vl6180x_performancetest.py
 1# SPDX-FileCopyrightText: 2022 Jonas Schatz
 2# SPDX-License-Identifier: MIT
 3
 4# Demo of reading the range from the VL6180x distance sensor in
 5# different access modes (single shot, continuous, history)
 6
 7import time
 8
 9import board
10import busio
11
12import adafruit_vl6180x
13
14# Create I2C bus.
15i2c = busio.I2C(board.SCL, board.SDA)
16
17# Create sensor instance.
18sensor = adafruit_vl6180x.VL6180X(i2c)
19
20# Define the number of measurements
21# n_measurements = 1000 will run for about 2 minutes
22n_measurements: int = 100
23
24# Single shot
25print("Starting single-shot measurement...")
26start = time.time()
27for i in range(n_measurements):
28    range_mm = sensor.range
29print(f"Performed {n_measurements} measurements in single-shot mode in {time.time() - start}s\n")
30
31# Sleep is required, otherwise the sensor might freeze when switching to
32# continuous mode too quickly after the last single shot
33time.sleep(2)
34
35# Continuous with no delay between measurements
36print("Starting continuous measurement...")
37sensor.start_range_continuous(20)
38start = time.time()
39for i in range(n_measurements):
40    range_mm = sensor.range
41print(f"Performed {n_measurements} measurements in continuous mode in {time.time() - start}s\n")
42
43# Continuous, reading data from history.
44# Note: This is fast, since you don't have to wait for the measurement to be
45# finished. On the downside, you will read the same value multiple times
46print("Starting continuous measurement with history enabled...")
47start = time.time()
48for i in range(n_measurements):
49    range_mm = sensor.range_from_history
50print(
51    f"Performed {n_measurements} measurements in continuous mode, reading form history, in {time.time() - start}s\n"  # noqa: E501
52)
53
54sensor.stop_range_continuous()
55print("Finished")

DisplayIO Simpletest

This is a simple test for boards with built-in display.

examples/vl6180x_displayio_simpletest.py
 1# SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries
 2# SPDX-FileCopyrightText: 2024 Jose D. Montoya
 3#
 4# SPDX-License-Identifier: MIT
 5
 6import time
 7
 8import board
 9from adafruit_display_text.bitmap_label import Label
10from displayio import Group
11from terminalio import FONT
12
13import adafruit_vl6180x
14
15# Simple demo of the vl6180x distance sensor.
16# create a main_group to hold anything we want to show on the display.
17main_group = Group()
18# Initialize I2C bus and sensor.
19i2c = board.I2C()  # uses board.SCL and board.SDA
20sensor = adafruit_vl6180x.VL6180X(i2c)
21
22# Create two Labels to show the readings. If you have a very small
23# display you may need to change to scale=1.
24range_output_label = Label(FONT, text="", scale=2)
25light_lux_output_label = Label(FONT, text="", scale=2)
26
27# place the labels in the middle of the screen with anchored positioning
28range_output_label.anchor_point = (0, 0)
29range_output_label.anchored_position = (
30    4,
31    board.DISPLAY.height // 2 - 40,
32)
33light_lux_output_label.anchor_point = (0, 0)
34light_lux_output_label.anchored_position = (4, board.DISPLAY.height // 2)
35
36
37# add the label to the main_group
38main_group.append(range_output_label)
39main_group.append(light_lux_output_label)
40
41# set the main_group as the root_group of the built-in DISPLAY
42board.DISPLAY.root_group = main_group
43
44# begin main loop
45while True:
46    # Update the label.text property to change the text on the display
47    range_output_label.text = f"Range:{sensor.range} mm"
48    light_lux_output_label.text = f"Lux:{sensor.read_lux(adafruit_vl6180x.ALS_GAIN_1)} lux"
49    # wait for a bit
50    time.sleep(1.0)