VL53L1x Address Change Error

Hi everyone,

I’m trying to connect 2 PiicoDev Laser Distance Sensor VL53L1X to a raspberry pi 4, as pictured in the image below.

I am using the following code to read from the sensors:

from PiicoDev_VL53L1X import PiicoDev_VL53L1X
from PiicoDev_Unified import sleep_ms
from time import sleep
from gpiozero import OutputDevice

# Change these pin numbers to the XSHUT pins you are using
shutdownA = OutputDevice(17)
shutdownB = OutputDevice(22)

# Shutdown all connected sensors
shutdownA.off()
shutdownB.off()

# Enable A for initialisation
shutdownA.on()
sensorA = PiicoDev_VL53L1X() # initialise sensor A with default address
sensorA.change_addr(0x2B)
# sensorA now has a new address

# Enable B for initialisation
shutdownB.on())
sensorB = PiicoDev_VL53L1X() # initialise sensor B with default address


# There are now two sensors initialised with separate addresses
# sensorA is at 0x2B
# sensorB is at 0x29 (default)

while True:
    distA = sensorA.read()
    distB = sensorB.read()
    print(f"{distA:4.0f}A, {distB:4.0f}B") # print both distances on same line
    sleep(0.1)

However, when I run the code I get the error:

Traceback (most recent call last):
  File "/home/drone/Desktop/test/test.py", line 47, in <module>
    sensorB = PiicoDev_VL53L1X()  # Initialize sensor B with default address
  File "/home/drone/.local/lib/python3.9/site-packages/PiicoDev_VL53L1X.py", line 111, in __init__
    self.reset()
  File "/home/drone/.local/lib/python3.9/site-packages/PiicoDev_VL53L1X.py", line 135, in reset
    self.writeReg(0x0000, 0x00)
  File "/home/drone/.local/lib/python3.9/site-packages/PiicoDev_VL53L1X.py", line 124, in writeReg
    return self.i2c.writeto_mem(self.addr, reg, bytes([value]), addrsize=16)
  File "/home/drone/.local/lib/python3.9/site-packages/PiicoDev_Unified.py", line 119, in writeto_mem
    self.smbus_i2c_write(addr, memaddr, buf, len(buf), addrsize=addrsize)
  File "/home/drone/.local/lib/python3.9/site-packages/PiicoDev_Unified.py", line 132, in smbus_i2c_write
    self.i2c.i2c_rdwr(msg_w)
  File "/home/drone/.local/lib/python3.9/site-packages/smbus2/smbus2.py", line 658, in i2c_rdwr
    ioctl(self.fd, I2C_RDWR, ioctl_data)
OSError: [Errno 121] Remote I/O error

When I’m running only 1 sensor, I can change the address and initialize the sensor. But the moment I try to initialize the second sensor I run into this error.

Any help or advice would be greatly appreciated.

2 Likes

Hey @JooKai278662,

What a weird error, It could be that Python is trying to initialize the two sensors without waiting for the address change to fully complete. I would try adding a pause to the code to slow things down and see if that makes a difference.

# Enable A for initialisation
shutdownA.on()
sensorA = PiicoDev_VL53L1X() # initialise sensor A with default address
sensorA.change_addr(0x2B)
# sensorA now has a new address
sleep(0.5) # wait for 0.5 seconds

# Enable B for initialisation
shutdownB.on()
sleep(0.5) # wait for 0.5 seconds
sensorB = PiicoDev_VL53L1X() # initialise sensor B with default address

The line shutdownB.on()) also had an extra close bracket that may have been causing some problems in this code.

Let us know if you still see the same behaviour with this new code slowing down the process.

Hope this helps!

Hi Samuel,

Thanks for the response! I tried adding the pauses and it now works properly, I can initialize and read from both sensors.

Thanks for the help :smiley:

2 Likes