VL53L0X Time-of-Flight Ranging Sensor


@RWB, I finally got my VL53L0X board connected and running with the Pololu library, but the Adafruit lib doesn’t give me good readings (constant 21021) - although I don’t see any SOS with the sample code.

I’ll have to investigate.

BTW, do you also get the TIMEOUT message every other reading with the Pololu CONTINOUS sample?


@ScruffR I’m using your library you have here with zero problems : https://build.particle.io/build/584f095f31c7319f1d001284

I never saw a timeout message serial print.

Using your Library I’m using the exact code below with great results inside the house. Background Christmas lights do cause random readings, but pointed at a wall it’s solid.

I also took it outside today while a good amount of snow was falling and it would measure or get thrown off as it measured snowflakes that fell in the laser beams path. So you would have to be careful when placing one of these outside to avoid readings being triggered by snow or rain particles.

Here is the exact code I’m running on a Photon with 0.6.0 firmware using your library.

/* This example shows how to use continuous mode to take
range measurements with the VL53L0X. It is based on
vl53l0x_ContinuousRanging_Example.c from the VL53L0X API.

The range readings are in units of mm. */
#if defined(_PARTICLE_BUILD_IDE_)
#  include "VL53L0X/VL53L0X.h"
#  include "VL53L0X.h"

#if !defined(PARTICLE)
#  include <Wire.h>

VL53L0X sensor;

#define LONG_RANGE

// Uncomment ONE of these two lines to get
// - higher speed at the cost of lower accuracy OR
// - higher accuracy at the cost of lower speed

//#define HIGH_SPEED

void setup()


 #if defined LONG_RANGE
  // lower the return signal rate limit (default is 0.25 MCPS)
  // increase laser pulse periods (defaults are 14 and 10 PCLKs)
  sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, 18);
  sensor.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, 14);

#if defined HIGH_SPEED
  // reduce timing budget to 20 ms (default is about 33 ms)
#elif defined HIGH_ACCURACY
  // increase timing budget to 200 ms
  sensor.setMeasurementTimingBudget(100000);  //This default setting was 200000 before I lowered it to 100000 FYI. The lower the number the more samples per second.  


void loop()

int distancemm = sensor.readRangeSingleMillimeters();

if (distancemm >= 9000)  //Change 9000 to the maximum distance you want to get readings from. Only readings below this number will serial print. 
  // action A

The sensor I have would max out at 8198 or something in the 8000+ range. If the laser is pointing at something further away then 6.5 feet the reading would return a steady 8195 - 8194. Not sure why yours is showing 20,000+?

I think we have access to the Interrupt threshold setting in your working library but I’m not sure how to activate it. I did see the interrupt registers in the .h file code as shown in the screen shot below:


The 21021 came from the Adafruit_VL53L0X lib that caused the SOS error.
The reason for that might be the different endianness between AVR and STM chips
I definetly want to get the Adafruit lib to work as it’s much closer to the STM original API set


The sensor is awesome and I also agree it deserves a proper library to get the most out of it. Good luck


So how does that explain the Pololu library working? Unless there is code in there that accounts for this?


@scruffr Any luck with that Adafruit Library or getting the interrupt working?


I gotta get me one of these sensors. Ordering from Canada, shipping is more expensive then the sensor itself!


I’ve activated the debug output on the lib and try to locate the issue.

Are you still seeing the SOS - I don’t get SOS.
SOS+5 means usage fault and is most often caused by a DIV/0 exception.


@ScruffR FWIW I also get SOS with the adafruit library on both local and Build compile, against both 6.0 and 5.3 firmware on the photon (using the 5.3 2016q1 compiler)


@bpr, what sensor board are you using?
What exaxt SOS code do you see?

I’ll soon upload the lib with activated log output.


@ScruffR I have tried both the Pololu and Adafruit boards. Seven flashing red , pause, Five flashing red, reboot. (I can’t figure out how to upload a video :frowning:)
Here’s the video:


You can’t upload a video direct, but can post a link (e.g. youtube, vimeo, dropbox, …)

Yup, that looks like the same issue that @RWB is seeing and it’s most likely a DIV/0 cause by the way both your boards responds, while mine responds slightly different.


@peekay123 I ordered one of these sensors up from Adafruit and it’s shipping via UPS Expedited International so you should have it in a few days. I’m sure you and @ScruffR will help us get all the features of the chip up and running :smile:

I’m most interested in letting the Photon & Electron Sleep while this low power sensor keeps taking measurements until the interrupt threshold is triggered.


@RWB & @bpr, I’ve just pushed 0.0.2 which has got Serial1 logging enabled.
If you could post the log output up to the SOS I might be able to locate the offending line(s)

@bpr, since you’ve got the sources too, you may be able to add some extra log statements to further home in on the culprit :wink:


@ScruffR This txt file contains the Debug output: https://www.dropbox.com/s/j807nc22mmfoz6w/Debug.txt?dl=0


Here’s my debug output, compiled with CLI, no extra messages coded yet, but note I had to disconnect the SDA line otherwise my photon “freezes” non-breathing cyan and gives no output.


So you are not getting the SOS anymore but only the freezing cyan?
How if you comment these lines in the vl53l0x.ino?

#define VL53L0X_LOG_ENABLE 0
uint32_t _trace_level = TRACE_LEVEL_ALL;

Without the SDA line attached the debug output doesn’t help a lot.

With @RWB’s logs I found several divisions that might cause a DIV/0 in VL53L0X_calc_sigma_estimate() & VL53L0X_calc_macro_period_ps() inside vl53l0x_api_core.cpp.
If you could take precautions against DIV/0 you might be able to get around the SOS - I hope :wink:


@ScruffR Commenting those lines out causes compile error:
(and only freezing cyan, no SOS before and with that change)

 Compile failed. Exiting.
../../../build/target/user/platform-6/libuser.a(vl53l0x_api.o): In function `VL53L0X_GetDeviceInfo':
vl53l0x_api.cpp:114: undefined reference to `_trace_level'
../../../build/target/user/platform-6/libuser.a(vl53l0x_api.o): In function `VL53L0X_GetRangeStatusString':
vl53l0x_api.cpp:158: undefined reference to `_trace_level'
../../../build/target/user/platform-6/libuser.a(vl53l0x_api.o): In function `VL53L0X_GetOffsetCalibrationDataMicroMeter':
vl53l0x_api.cpp:277: undefined reference to `_trace_level'
../../../build/target/user/platform-6/libuser.a(vl53l0x_api.o): In function `VL53L0X_SetDeviceMode':
vl53l0x_api.cpp:928: undefined reference to `_trace_level'
../../../build/target/user/platform-6/libuser.a(vl53l0x_api.o): In function `VL53L0X_GetDeviceMode':
vl53l0x_api.cpp:940: undefined reference to `_trace_level'
../../../build/target/user/platform-6/libuser.a(vl53l0x_api.o):vl53l0x_api.cpp:969: more undefined references to `_trace_level' follow
collect2: error: ld returned 1 exit status
make: *** [bed9be248d29d05c51d65cd807b1e49021862ccffcf953225083e7098e15.elf] Error 1


OK, maybe not comment this one :blush:

uint32_t _trace_level = TRACE_LEVEL_ALL;

or maybe change it to

uint32_t _trace_level = TRACE_LEVEL_NONE;


@ScruffR just got me back to solid “frozen” cyan