Simple Test¶
Ensure your device works with this simple test.
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
15# Create I2C bus.
16i2c = busio.I2C(board.SCL, board.SDA)
17
18# Create sensor instance.
19sensor = adafruit_vl6180x.VL6180X(i2c)
20# You can add an offset to distance measurements here (e.g. calibration)
21# Swapping for the following would add a +10 millimeter offset to measurements:
22# sensor = adafruit_vl6180x.VL6180X(i2c, offset=10)
23
24# Main loop prints the range and lux every second:
25while True:
26 # Read the range in millimeters and print it.
27 range_mm = sensor.range
28 print("Range: {0}mm".format(range_mm))
29 # Read the light, note this requires specifying a gain value:
30 # - adafruit_vl6180x.ALS_GAIN_1 = 1x
31 # - adafruit_vl6180x.ALS_GAIN_1_25 = 1.25x
32 # - adafruit_vl6180x.ALS_GAIN_1_67 = 1.67x
33 # - adafruit_vl6180x.ALS_GAIN_2_5 = 2.5x
34 # - adafruit_vl6180x.ALS_GAIN_5 = 5x
35 # - adafruit_vl6180x.ALS_GAIN_10 = 10x
36 # - adafruit_vl6180x.ALS_GAIN_20 = 20x
37 # - adafruit_vl6180x.ALS_GAIN_40 = 40x
38 light_lux = sensor.read_lux(adafruit_vl6180x.ALS_GAIN_1)
39 print("Light (1x gain): {0}lux".format(light_lux))
40 # Delay for a second.
41 time.sleep(1.0)
Calibration Test¶
Demo of calibrating the part to part range offset per Application Note 4545 for the VL6180X sensor.
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
15# Create I2C bus.
16i2c = busio.I2C(board.SCL, board.SDA)
17
18# Create sensor instance, with explicit offset of 0 to clear the system offset
19sensor = adafruit_vl6180x.VL6180X(i2c, offset=0)
20
21# Place a target at 50mm away from VL6180X Collect a number of range measurements
22# with the target in place and calculate mean of the range results. For a
23# reliable measurement, take at least 10 measurements.
24measurements = []
25for msmt in range(10):
26 range_mm = sensor.range
27 measurements.append(range_mm)
28 time.sleep(1.0)
29average_msmt = sum(measurements) / 10
30
31# Calculate the offset required:
32calibration_offset = 50 - average_msmt
33
34# Apply offset
35sensor.offset = calibration_offset
Continuous Test¶
Demo of reading the range from the VL6180x distance sensor in continuous mode.
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
15# Create I2C bus.
16i2c = busio.I2C(board.SCL, board.SDA)
17
18# Create sensor instance.
19sensor = adafruit_vl6180x.VL6180X(i2c)
20
21# Starting continuous mode
22print("Starting continuous mode")
23sensor.start_range_continuous(20)
24
25# Main loop prints the range and lux every 0.01 seconds
26for _ in range(100):
27 # Read the range in millimeters and print it.
28 range_mm = sensor.range
29 print("Range: {0}mm".format(range_mm))
30
31 # Delay for 10 ms
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()
History Test¶
Demo of reading the range from the history buffer of the VL6180x distance sensor.
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
15# Create I2C bus.
16i2c = busio.I2C(board.SCL, board.SDA)
17
18# Create sensor instance.
19sensor = adafruit_vl6180x.VL6180X(i2c)
20
21# Starting continuous mode
22print("Starting continuous mode")
23sensor.start_range_continuous()
24
25# Main loop prints the ranges every 0.01 seconds for about 5 seconds
26# You should see changes 'ripple through' the history array
27for _ in range(500):
28 # Read the last 16 ranges from the history buffer as a List[int]
29 ranges_mm = sensor.ranges_from_history
30 print(ranges_mm)
31
32 # Delay for 10 ms so that the loop is not too fast
33 time.sleep(0.01)
34
35# Stop continuous mode. This is advised as the sensor
36# wouldn't stop measuring after the program has ended
37sensor.stop_range_continuous()
Performance Test¶
Demo of reading the range from the VL6180x distance sensor in different access modes (single shot, continuous, history).
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
15# Create I2C bus.
16i2c = busio.I2C(board.SCL, board.SDA)
17
18# Create sensor instance.
19sensor = adafruit_vl6180x.VL6180X(i2c)
20
21# Define the number of measurements
22# n_measurements = 1000 will run for about 2 minutes
23n_measurements: int = 100
24
25# Single shot
26print("Starting single-shot measurement...")
27start = time.time()
28for i in range(n_measurements):
29 range_mm = sensor.range
30print(
31 "Performed {} measurements in single-shot mode in {}s\n".format(
32 n_measurements, time.time() - start
33 )
34)
35
36# Sleep is required, otherwise the sensor might freeze when switching to
37# continuous mode too quickly after the last single shot
38time.sleep(2)
39
40# Continuous with no delay between measurements
41print("Starting continuous measurement...")
42sensor.start_range_continuous(20)
43start = time.time()
44for i in range(n_measurements):
45 range_mm = sensor.range
46print(
47 "Performed {} measurements in continuous mode in {}s\n".format(
48 n_measurements, time.time() - start
49 )
50)
51
52# Continuous, reading data from history.
53# Note: This is fast, since you don't have to wait for the measurement to be
54# finished. On the downside, you will read the same value multiple times
55print("Starting continuous measurement with history enabled...")
56start = time.time()
57for i in range(n_measurements):
58 range_mm = sensor.range_from_history
59print(
60 "Performed {} measurements in continuous mode, reading form history, in {}s\n".format(
61 n_measurements, time.time() - start
62 )
63)
64
65sensor.stop_range_continuous()
66print("Finished")