Slow initial connection to i2c sensor from Python

I’m experiencing quite slow initial connection time (above 10 sec) with a VL6180X TOF sensor on the Nvidia Jetson Dev Kits (both Nano and Xavier NX).

I’m using the Adafruit VL6180X Circuit Python Library

Once the connection is established everything seems to work fine.
However, sensor = adafruit_vl6180x.VL6180X(i2c) takes above 10 seconds to execute.

What could be causing this such slow sensor init time?
Is there any alternative library I could use to communicate with the sensor?

Try to set the i2c clock to verify.

hey @ShaneCCC,
On my system I got several subfolders under
/sys/bus/i2c/devices/
from i2x-0 all the way up to i2c-9
Which one should I update?

Also, when booting the Jetson in command line mode ( sudo systemctl set-default multi-user.target), I can see this output every time I’m running my python app:


The app works fine in the end, and I’m getting data from the sensor. But I suspect the long connection time to the sensor has something to do with the above errors/timeouts.

Again, those are not visible when running my app from a terminal in GUI mode, and are not part of my program output, they seem to be printed by the system in the background. so I was unable to copy/paste them here or store in a log file, only take a photo.

i2c@31e00000 is i2c8, if the i2c error message show in the access time it could be device HW problem.

I’m getting similar errors on both, Jetson Xavier NX and the Jetson Nano Dev kits.
Each kit has its own VL6180X sensor connected to it.
Could both kits and both sensors have the same hardware problem?

Here are the screenshots again for comparison:

Nano Dev Kit:

Xavier NX Dev Kit:

p.s.
I’m also having problems (on both kits) with some sensors not being detected at all such as the mlx90614 - IR Temperature sensor

The sensors work well on other systems such as an Arduino and a RaspberryPi.

The easy way to check I2C port is to probe the output I2C signal to confirm if the waveform is correct. In general, the I2C port on Jetson should be no problem since it is validated before ship out. So please check the external device’s setup to make sure it is power up correctly, its IO level match with Jetson’s, it is initiated correctly etc…

Hey @Trumany,

I tested 4 different VL6180X sensors with 3 different Jetson Boards - 1 Jetson Nano Dev Kit and 2 different Xavier NX Dev Kits. The case was the same across all tests:
The initial connection to the sensor takes about 10 seconds, once established the data flow is stable and quick.

I made the same test on a Raspberry Pi, with the same sensors and the initial connection to the sensor takes 0.02 seconds. So I think the sensors work fine.

Here is my test code:

import time
import board
import busio
import adafruit_vl6180x # TOF Sensor

class SensorsFeed:

    def __init__(self):
        self.i2c = busio.I2C(board.SCL, board.SDA, frequency=100000)       
        print("connecting to range sensor...")
        self.rng_sensor = adafruit_vl6180x.VL6180X(self.i2c)

        print("Sensor online!")

    def get_range(self):
        sensorVal = self.rng_sensor.range
        return sensorVal

if __name__ == "__main__":
   
    start_time = time.time()
    sensors = SensorsFeed()
    print(f"Took: {time.time() - start_time} sec.")

    start_time = time.time()
    print("getting measurements")
    for i in range(10):
        print(sensors.get_range())
    print(f"Took: {time.time() - start_time} sec.")

Here is the output on the Jetson (Initial connection takes 10s):

    connecting to the range sensor...
    Sensor online!
    Took: 10.185436964035034 sec.
    getting measurements
    72
    72
    73
    73
    73
    70
    72
    73
    69
    73
    Took: 0.16100001335144043 sec.

Here is the output of the same code on the Raspberry Pi (initial connection takes 0.02s):

    connecting to range sensor...
    Sensor online!
    Took: 0.025896072387695312 sec.
    getting measurements
    51
    51
    52
    53
    54
    54
    53
    54
    54
    53
    Took: 0.11930608749389648 sec.

What the SensorsFeed() do?

@ShaneCCC
“SensorsFeed” is a class (declared at the beginning of the code) which holds the sensor-related functions.
So sensors = SensorsFeed() creates a new instance of this class and stores it in the “sensors” variable. When an instance of a class is created it automatically calls the __init__() function within that class.

In this function, I establish the connection with the sensors, and that’s the part which takes quite long ~10 seconds on the Jetson.

To be more specific the line that takes 10 sec is:

self.rng_sensor = adafruit_vl6180x.VL6180X(self.i2c)

Can break down this function or know what’s this function doing?

This command: adafruit_vl6180x.VL6180X(self.i2c) opens the connection with the sensor.
It calls this function from the the vl6180x Adafruit library:

Which in turn loads those settings:

then writes one byte of data:

Can the i2c utils like i2cget can access this device before running this script?

Yes, running an i2c scan on the Jetson sudo i2cdetect -y -r 8 on the Xavier NX or sudo i2cdetect -y -r 1 on the Nano takes ~1 second to perform the scan and it finds the sensor on address 0x29 with no problems or delays.

How about i2cget to access any of REG?

i2get seems to work with no delays.

Could it be the below loop cause the problem?

while not self._read_8(_VL6180X_REG_RESULT_RANGE_STATUS) & 0x01:

I don’t think this loop is being accessed in the connection init phase as it seems to be part of the range reading property which works fine with no delays.

OK, may need to break down further like maybe access which REG cause the problem.

Checking with Adafuit, they should have a better understanding of their library:
https://forums.adafruit.com/viewtopic.php?f=60&t=172811&p=845239#p845239