Accessing functions from the PiicoDev VL53L1X MicroPython Library

Hello All,

I am having problems getting correct readings from the PiicoDev VL53L1X Distance Sensor. I would like to ignore the values that the sensor returns when it is out of range.

I am using it with the Raspberry Pi Pico and MicroPython.

I have looked at the PiicoDev libary for the sensor and this document and it seems as though the PiicoDev library supports thresholds and detection modes. As far as I can work out, using these functions would allow me to only get values when it is within accurate range (4m).

Any help as to how I would enable the thresholds and detection modes would be greatly appreciated.

Thanks,
Jaden

2 Likes

Hi @Jaden130809 - the PiicoDev VL53L1X module only supports basic ranging functions (for now :wink: ). The linked document is for the VL53L1X C/C++ API which has a lot more functionality that we haven’t ported to MicroPython.
If you’re up for a bit of reverse engineering, I believe this is the official page where you can download the C/C++ libraries for the VL53L1X. And here’s Pololu’s version for Arduino. If you do end up porting any functionality to MicroPython, consider creating a pull request on the repo!

For now, if you’re just after a quick fix, does it make sense to just ignore values that fall outside a given range?
eg.

d = sensor.read()
if d < 15 or d > 3000:
    d = None

2 Likes

Hello @Michael,

Thanks for your response. I will have a look at those links.

The problem with the code you suggested is once the sensor is out of range it starts returning values that are likely to be within the limits (less than 15 or greater than 3000). Hope this makes sense.

I actually tried to modify the VL53L1X module by uncommenting the if, elseif, and else statements for the range status and added in return status into each statement.

The results were great. When it was in range it returned OK and when it was out of range it returned SignalFail.

Will keep experimenting and try to modify my code. Can’t wait till its completely ported to MicroPython. :slightly_smiling_face:

Thanks again for your support.

Jaden

3 Likes

You’re right @Jaden130809, the functionality is in there. I must have had a brainfart. Glad you dug into the module :slight_smile:

Maybe it makes sense to set the status as a property of the class, and access that with a ‘getter’ function. So code to check validation might look like:

d = sensor.read()
if sensor.rangeIsValid() == True:
    # do something with the range
else:
    print('Data is out of range')

In any case, I’ll open an issue on our repo and earmark it to work on - this functionality is too useful to ignore. Thanks :smiley:

4 Likes

Hello @Michael

Just wondered if the rangeIsValid() functionality was ever added to the module?

Thanks for all the great products
Steve

Hey @Stephen202603, I’ve just pushed an (experimental) updated that exposes the .status attribute.
Available by downloading PiicoDev_VL53L1X.py (right-click, save-as).
It appears to work well for ranges beyond the max-range. Minimum range still gets reported as OK through. You can include the status in the example script as follows.

from PiicoDev_VL53L1X import PiicoDev_VL53L1X
from time import sleep

distSensor = PiicoDev_VL53L1X()

while True:
    dist = distSensor.read() # read the distance in millimetres
    print(str(dist) + " mm    " + distSensor.status) # convert the number to a string and print
    sleep(0.1)

This update hasn’t been made available for Raspberry Pi through the Thonny package manager yet. That’s ok though you can still download the source file into your working directory if you’re working on a Raspberry Pi. It’ll get pushed out as usual on the next package update ie. when we release a new PiicoDev device or make an important patch.

1 Like

My order should be here today and I will try this as soon as I can.

Thanks, brilliant customer service as always!
Steve