VL53L1X ToF Sensor - usage of XSHUT-Pin/change default addr

General project help for Adafruit customers

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
User avatar
hespHB
 
Posts: 5
Joined: Tue May 10, 2022 8:22 am

VL53L1X ToF Sensor - usage of XSHUT-Pin/change default addr

Post by hespHB »

Hello,
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()
Generated output from the code:
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...
Attachments
Schematic_adaForum.png
Schematic_adaForum.png (41.27 KiB) Viewed 298 times

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: VL53L1X ToF Sensor - usage of XSHUT-Pin/change default

Post by adafruit_support_carter »

Highly recommend using an I2C multiplexer to deal with this issue:
https://learn.adafruit.com/adafruit-tca ... r-breakout
https://learn.adafruit.com/working-with ... 2c-devices

Otherwise, you'll need to go through an initiation for each sensor using XSHUT. This is for a different model, but it's the same idea:
i2c_addr_xshut.jpg
i2c_addr_xshut.jpg (65.84 KiB) Viewed 292 times
The equivalent to VL53LX_SetDeviceAddress() is set_address() in the CircuitPython library.


EDIT:
oh, yah, forgot, there's actually an example for this in the library repo:
https://github.com/adafruit/Adafruit_Ci ... sensors.py

User avatar
hespHB
 
Posts: 5
Joined: Tue May 10, 2022 8:22 am

Re: VL53L1X ToF Sensor - usage of XSHUT-Pin/change default

Post by hespHB »

Thank you for the quick feedback and the tip with the multiplexer. If I do not get further on the software side, it will be ordered directly.

However, when running the library example, I get the following error message:

Code: Select all

/usr/bin/python3 /home/pi/DroneStock/VL53L1X/adafruit/vl53l1x_set_address_multiple_sensors.py
Traceback (most recent call last):
  File "/home/pi/DroneStock/VL53L1X/adafruit/vl53l1x_set_address_multiple_sensors.py", line 53, in <module>
    sensor_i2c.set_address(pin_number + 0x30)
AttributeError: 'VL53L1X' object has no attribute 'set_address'
I tried to install all necessary/prescribed dependencies from the git repo (https://github.com/adafruit/Adafruit_Ci ... on_VL53L1X). When installing "circup install vl53l1x" I still get the following error:

Code: Select all

circup install vl53l1x
Version 1.0.3 of circup is outdated. Version 1.0.4 was released Friday February 18, 2022.
Could not find a connected CircuitPython device.
If I try to update it afterwards with the "circup update" it stays at the outdated release version and throws the same error message.

Could this be related? Alternatively, I had tried to overwrite the address directly when instantiating the sensor by

Code: Select all

import time
import board
import adafruit_vl53l1x

i2c = board.I2C()
#-----try to initialize another bus address with the hex and dezimal numbers
# hex to dezimal -> 0x29=41
vl53_left = adafruit_vl53l1x.VL53L1X(i2c, address=0x29)
# hex to dezimal -> 0x30=48
vl53_right = adafruit_vl53l1x.VL53L1X(i2c, address=0x30)


vl53_left.start_ranging()
vl53_right.start_ranging()

while True:
    if vl53_left.data_ready :
        print("Left: {} cm".format(vl53_left.distance))
        vl53_left.clear_interrupt()
        print("Right: {} cm".format(vl53_right.distance))
        vl53_right.clear_interrupt()
        time.sleep(1.0)[
/code]

, but also here I got only an error:

[code]
/usr/bin/python3 /home/pi/DroneStock/VL53L1X/adafruit/change_addr.py
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/adafruit_bus_device/i2c_device.py", line 154, in __probe_for_device
    self.i2c.writeto(self.device_address, b"")
  File "/home/pi/.local/lib/python3.7/site-packages/busio.py", line 169, in writeto
    return self._i2c.writeto(address, buffer, stop=stop)
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 52, in writeto
    self._i2c_bus.write_bytes(address, buffer[start:end])
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/smbus.py", line 314, in write_bytes
    self._device.write(buf)
OSError: [Errno 121] Remote I/O error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/adafruit_bus_device/i2c_device.py", line 160, in __probe_for_device
    self.i2c.readfrom_into(self.device_address, result)
  File "/home/pi/.local/lib/python3.7/site-packages/busio.py", line 159, in readfrom_into
    return self._i2c.readfrom_into(address, buffer, stop=stop)
  File "/home/pi/.local/lib/python3.7/site-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 59, in readfrom_into
    readin = self._i2c_bus.read_bytes(address, end - start)
  File "/usr/local/lib/python3.7/dist-packages/Adafruit_PureIO/smbus.py", line 181, in read_bytes
    return self._device.read(number)
OSError: [Errno 121] Remote I/O error

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/pi/DroneStock/VL53L1X/adafruit/change_addr.py", line 10, in <module>
    vl53_right = adafruit_vl53l1x.VL53L1X(i2c, address=0x30)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_vl53l1x.py", line 79, in __init__
    self.i2c_device = i2c_device.I2CDevice(i2c, address)
  File "/usr/local/lib/python3.7/dist-packages/adafruit_bus_device/i2c_device.py", line 50, in __init__
    self.__probe_for_device()
  File "/usr/local/lib/python3.7/dist-packages/adafruit_bus_device/i2c_device.py", line 163, in __probe_for_device
    raise ValueError("No I2C device at address: 0x%x" % self.device_address)
ValueError: No I2C device at address: 0x30
And again, thanks for any help with my problem!

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: VL53L1X ToF Sensor - usage of XSHUT-Pin/change default

Post by adafruit_support_carter »

You may have an older version of the VL53L1X library. The address setting function was added fairly recently:
https://github.com/adafruit/Adafruit_Ci ... /tag/1.1.0

So it may be related to the circup issue? You can try manually downloading the latest library bundle and copying the library from there.
https://learn.adafruit.com/welcome-to-c ... -libraries

User avatar
hespHB
 
Posts: 5
Joined: Tue May 10, 2022 8:22 am

Re: VL53L1X ToF Sensor - usage of XSHUT-Pin/change default

Post by hespHB »

So this has partially worked for me. But I´m still facing to the same error as before. The libary is updated and knows the methods (you can see ist in the picture), but if I want to run the code this error apperas:

Code: Select all

Traceback (most recent call last):
  File "/home/pi/DroneStock/VL53L1X/adafruit/vl53l1x_set_address_multiple_sensors.py", line 57, in <module>
    sensor_i2c.set_address(pin_number + 0x30)
AttributeError: 'VL53L1X' object has no attribute 'set_address'
I checked manually the libary and everything is up to date. Do you have any idea where the last error/problem could be? I checked my wiring and everything seems okay. Meanwhile I ordered a multiplexer so maybe this could solve my problem. Another option would be to define a new i2c bus on my raspberry pi and try to to connect and read out the second sensor. (https://www.instructables.com/Raspberry ... c-Devices/)


Thanks for any help and sorry for the late reply!
Attachments
vl53_setAddress.png
vl53_setAddress.png (34.68 KiB) Viewed 266 times

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: VL53L1X ToF Sensor - usage of XSHUT-Pin/change default

Post by adafruit_support_carter »

Sorry, ignore my previous suggestion about using the Library Bundle. Forgot you were using a Pi.

For a Pi, libraries are installed via pip.
https://learn.adafruit.com/adafruit-vl5 ... 3105069-10

Library updating would also be done with pip.

How did you update the library? What specific command did you use?

User avatar
hespHB
 
Posts: 5
Joined: Tue May 10, 2022 8:22 am

Re: VL53L1X ToF Sensor - usage of XSHUT-Pin/change default

Post by hespHB »

No problem :D

By now, I think I tried every possible commad. I´ve tried it with:

1. pip install adafruit-circuitpython-vl53l1x

Output:

Code: Select all

Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: adafruit-circuitpython-vl53l1x in /usr/local/lib/python3.7/dist-packages (1.0.2)
Requirement already satisfied: adafruit-circuitpython-busdevice in /usr/local/lib/python3.7/dist-packages (from adafruit-circuitpython-vl53l1x) (5.1.2)
Requirement already satisfied: Adafruit-Blinka in ./.local/lib/python3.7/site-packages (from adafruit-circuitpython-vl53l1x) (7.3.0)
Requirement already satisfied: pyftdi>=0.40.0 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (0.53.1)
Requirement already satisfied: Adafruit-PlatformDetect>=3.13.0 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (3.14.0)
Requirement already satisfied: rpi-ws281x>=4.0.0 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (4.3.0)
Requirement already satisfied: RPi.GPIO in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (0.7.1)
Requirement already satisfied: Adafruit-PureIO>=1.1.7 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (1.1.9)
Requirement already satisfied: sysv-ipc>=1.1.0 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (1.1.0)
Requirement already satisfied: adafruit-circuitpython-typing in ./.local/lib/python3.7/site-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (1.6.0)
Requirement already satisfied: pyusb>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from pyftdi>=0.40.0->Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (1.1.1)
Requirement already satisfied: pyserial>=3.0 in /usr/lib/python3/dist-packages (from pyftdi>=0.40.0->Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (3.4)
Requirement already satisfied: typing-extensions; python_version <= "3.7" in ./.local/lib/python3.7/site-packages (from adafruit-circuitpython-typing->Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (3.10.0.0)
pi@knxpi:~ $ 
2. sudo pip install adafruit-circuitpython-vl53l1x ...to guarantee a system-wide installation

Output:

Code: Select all

Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: adafruit-circuitpython-vl53l1x in /usr/local/lib/python3.7/dist-packages (1.0.2)
Requirement already satisfied: Adafruit-Blinka in /usr/local/lib/python3.7/dist-packages (from adafruit-circuitpython-vl53l1x) (7.3.0)
Requirement already satisfied: adafruit-circuitpython-busdevice in /usr/local/lib/python3.7/dist-packages (from adafruit-circuitpython-vl53l1x) (5.1.2)
Requirement already satisfied: sysv-ipc>=1.1.0 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (1.1.0)
Requirement already satisfied: Adafruit-PlatformDetect>=3.13.0 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (3.14.0)
Requirement already satisfied: RPi.GPIO in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (0.7.1)
Requirement already satisfied: adafruit-circuitpython-typing in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (1.6.0)
Requirement already satisfied: rpi-ws281x>=4.0.0 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (4.3.0)
Requirement already satisfied: Adafruit-PureIO>=1.1.7 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (1.1.9)
Requirement already satisfied: pyftdi>=0.40.0 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (0.53.1)
Requirement already satisfied: typing-extensions; python_version <= "3.7" in /usr/local/lib/python3.7/dist-packages (from adafruit-circuitpython-typing->Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (4.2.0)
Requirement already satisfied: pyserial>=3.0 in /usr/lib/python3/dist-packages (from pyftdi>=0.40.0->Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (3.4)
Requirement already satisfied: pyusb>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from pyftdi>=0.40.0->Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (1.1.1)
3. And for the last time I tried it with "pip3 install adafruit-circuitpython-vl53l1x"

Output:

Code: Select all

Looking in indexes: https://pypi.org/simple, https://www.piwheels.org/simple
Requirement already satisfied: adafruit-circuitpython-vl53l1x in /usr/local/lib/python3.7/dist-packages (1.0.2)
Requirement already satisfied: adafruit-circuitpython-busdevice in /usr/local/lib/python3.7/dist-packages (from adafruit-circuitpython-vl53l1x) (5.1.2)
Requirement already satisfied: Adafruit-Blinka in /home/pi/.local/lib/python3.7/site-packages (from adafruit-circuitpython-vl53l1x) (7.3.0)
Requirement already satisfied: Adafruit-PlatformDetect>=3.13.0 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (3.14.0)
Requirement already satisfied: pyftdi>=0.40.0 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (0.53.1)
Requirement already satisfied: rpi-ws281x>=4.0.0 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (4.3.0)
Requirement already satisfied: sysv-ipc>=1.1.0 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (1.1.0)
Requirement already satisfied: Adafruit-PureIO>=1.1.7 in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (1.1.9)
Requirement already satisfied: adafruit-circuitpython-typing in /home/pi/.local/lib/python3.7/site-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (1.6.0)
Requirement already satisfied: RPi.GPIO in /usr/local/lib/python3.7/dist-packages (from Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (0.7.1)
Requirement already satisfied: pyusb>=1.0.0 in /usr/local/lib/python3.7/dist-packages (from pyftdi>=0.40.0->Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (1.1.1)
Requirement already satisfied: pyserial>=3.0 in /usr/lib/python3/dist-packages (from pyftdi>=0.40.0->Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (3.4)
Requirement already satisfied: typing-extensions; python_version <= "3.7" in /home/pi/.local/lib/python3.7/site-packages (from adafruit-circuitpython-typing->Adafruit-Blinka->adafruit-circuitpython-vl53l1x) (3.10.0.0)
This should be a similiar output for everything with different commands. After running the commands once more, the error ( from my last reply) still apperas if I want to run the script.

User avatar
adafruit_support_carter
 
Posts: 29056
Joined: Tue Nov 29, 2016 2:45 pm

Re: VL53L1X ToF Sensor - usage of XSHUT-Pin/change default

Post by adafruit_support_carter »

Looks like you have the 1.0.2 release installed:

Code: Select all

    Requirement already satisfied: adafruit-circuitpython-vl53l1x in /usr/local/lib/python3.7/dist-packages (1.0.2)
And it's seeing that as "already installed, nothing to do".

Try forcing an update so it'll bring in the latest release:

Code: Select all

pip install --upgrade adafruit-circuitpython-vl53l1x

User avatar
hespHB
 
Posts: 5
Joined: Tue May 10, 2022 8:22 am

Re: VL53L1X ToF Sensor - usage of XSHUT-Pin/change default

Post by hespHB »

This has solved my problem, the sensors are running and they are detected with diffrent addresses if I use "sudo i2cdetect -y 1".

Code: Select all

     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- 29 -- -- -- -- -- -- 
30: 30 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --   
Thanks for your support and help!

Locked
Please be positive and constructive with your questions and comments.

Return to “General Project help”