Python interface to ST VL53L0X Library

Recently I obtained several VL53L0X ToF sensors and after being impressed with their capabilities, I wanted to add these to a project I am working on that is written entirely in Python.I found that there are a few Python interfaces that perform the basics but none that provide the full capability and configuration afforded by the ST Library. Since porting the entire ST Library to Python would be time consuming and prone to error, I instead created the necessary Python code to interface the ST Library into Python. In order to maintain compatibility with other Python code that accesses the I2C bus, the I2C interface is performed via callbacks to the Python smbus interface in the ST Library platform layer. The Python code/Interface library includes a simplified interface to the VL53L0X which allows for configuring the sensor into 4 different accuracy modes with a simple call. Currently this Python code only provides access to a single sensor but I am working on adding simplified access to multiple sensors. The code can be found at:

vl53L0x_rasp_python

Comments and suggestions are appreciated.

Please note that I am a novice at Python but have been an embedded software engineer for 25 years. If Python experts know of a better/more efficient way to do any of this, please let me know.

3 Likes

Hello.

Thanks for sharing it here.

-Nathan

Updated to include multiple sensor support and better error checking.

1 Like

Impressive piece of work!
I was impressed by this`post and it was the trigger for me to obtain a RaspberryPi and start working on it.
Yes, my first hello word steps in python etc, but I have engineering background and did do a lot on embedded and assembler, C quite some time ago. I guessed python would be like my old eighties basic.

After soldering a single VL53L0X to the Pi-connector, I followed the instructions, but are now facing a general error:

root@raspberrypi:/home/pi/git/VL53L0X_rasp_python/python# sudo python2.7 /home/pi/git/VL53L0X_rasp_python/python/VL53L0X_example.py > text.txt Traceback (most recent call last):
File “/home/pi/git/VL53L0X_rasp_python/python/VL53L0X_example.py”, line 26, in
import VL53L0X
File “/home/pi/git/VL53L0X_rasp_python/python/VL53L0X.py”, line 35, in
i2cbus = smbus.SMBus(1)
IOError: [Errno 2] No such file or directory root@raspberrypi:/home/pi/git/VL53L0X_rasp_python/python#

I’m a newbie on Linux&Pi and presume I need to set a path or something…

I somebody able to give me an indication what to do?
Thanks a lot!
Robert

Ref: https://github.com/johnbryanmoore/VL53L0X_rasp_python

There are a few things to check:

  1. Have you enabled I2C on the Pi? From the Pi command line run “sudo raspi-config” or select raspi-config from the GUI. Under Advanced Options is where you can enable I2C. Reboot once it is enabed.
  2. Once I2C is enabled, you need to make sure that your VL53L0X module is being seen by the Pi I2C. First make sure that the I2C tools are installed using:

sudo apt-get install -y i2c-tools

Once installed issue “sudo i2cdetect -y 1” and make sure that the VL53L0X is detected as 0x29.
3) Once i2cdetect shows the module, make sure that smbus is installed in python using:

sudo apt-get install -y python-smbus

and then try running the example again.

I really need to update the README in the github project to include more instruction like this for Pi Novices.

John

2 Likes

Thanks a lot! The suggestions were very helpful!

After I followed the hints to enable I2C, I got it running!
Some commands differed a bit (e.g. “Interface options” instead of "Advanced options), but the rest was perfect.

I saw 0x29 appearing in a table and I wondered a bit if the VL53L03X was really detected.
After powering down, removing the HAT-board with the VL53L0X and running again the 0x029 was gone.

Finally running the example by opening the python 2.7.9 shell, loading and running the file.
Great work John, thanks a lot for sharing these examples.
If feel the need to make things and write growing.

Hello World, Hello Raspberry Pi!

Rbert

Thank you John!!

Saved us a lot of time.

Hi John,

in my case i’ve done bitbanging on pi board due to lack of i2c pins. now i could detect VL53L0X in 3. (sudo i2cdetect -y 3). and it’s detected as 0x29 only but in 3rd bus not in 1st bus.

now the problem is vl53l0x_rasp_python code written for only 1not for 3. do u know where can we modify code to support for 3.?

thanks,
Sunil.