I am working on an object detection system for an inventory drone and would like to use data from multiple VL53L1X ToF sensors to determine distance. To use the data from the i2c interface, I wanted to switch the sensors via the XSHUT pin to differentiate the sensors. Unfortunately, I find relatively little / nothing at all to implement this and have derived the basic logic based on the data sheet. Only unfortunately my solution does not work and does not generate me the desired result.
Therefore I have the following questions:
1. Is there a logic error in my code, why the change does not work? (Code and output are a little deeper)
2. Is my my circuit/use of the pins not correct?
3. Is there a way to change the address of the sensor during the boot process? So each sensor could write directly to the bus via its own address.
I appreciate any feedback and thank you in advance.
Code: Select all
import time
import board
import adafruit_vl53l1x
import RPi.GPIO as GPIO
# define XSHUT pins to switch between the sensors
GPIO.setmode(GPIO.BCM)
GPIO_XSHUT_1 = 20
GPIO_XSHUT_2 = 21
GPIO.setup(GPIO_XSHUT_1, GPIO.OUT)
GPIO.setup(GPIO_XSHUT_2, GPIO.OUT)
# set pins on HIGH to allow a i2c connection
GPIO.output(GPIO_XSHUT_1, GPIO.HIGH )
GPIO.output(GPIO_XSHUT_2, GPIO.HIGH )
time.sleep(1.0)
i2c = board.I2C()
vl53 = adafruit_vl53l1x.VL53L1X(i2c)
# define sensor mode
vl53.distance_mode = 2
vl53.timing_budget = 100
vl53.start_ranging()
try:
while True:
GPIO.output(GPIO_XSHUT_1, GPIO.HIGH )
if vl53.data_ready:
# read first sensor (1->HIGH ; 2->LOW)
GPIO.output(GPIO_XSHUT_2, GPIO.LOW )
time.sleep(1.0)
distance = vl53.distance
print("Distance: {} cm at pin {}".format(vl53.distance, GPIO_XSHUT_1))
vl53.clear_interrupt()
GPIO.output(GPIO_XSHUT_1, GPIO.LOW )
# read second sensor (1->LOW ; 2->HIGH)
GPIO.output(GPIO_XSHUT_2, GPIO.HIGH )
distance = vl53.distance
print("Distance: {} cm at pin {}".format(vl53.distance, GPIO_XSHUT_2))
vl53.clear_interrupt()
GPIO.output(GPIO_XSHUT_2, GPIO.LOW )
time.sleep(1.0)
else:
print("waiting for sensor data...")
time.sleep(1.0)
except KeyboardInterrupt:
print("Stop")
GPIO.cleanup()
waiting for sensor data...
Distance: 7.0 cm at pin 12
Distance: 0.0 cm at pin 16
waiting for sensor data...
waiting for sensor data...
waiting for sensor data...