sparkfun / SparkFun_VL53L1X_Arduino_Library

A library for the laser based VL53L1X Time Of Flight distance sensor capable of detecting a target 4m away!
MIT License
88 stars 50 forks source link

Unit fails to init after first POR #37

Closed nseidle closed 4 years ago

nseidle commented 4 years ago

At first power on, the library is unable to begin a sensor. I think it's related to the changes made within VL53L1X_SensorInit()

To replicate,

Sketch will fail to init sensor every time. Hitting reset on Uno (no sensor power cycle), library starts every time.

I think most users aren't seeing this because they are hitting reset or some other power sequence.

Also related (I think) - library freezes when no sensor is connected.

nseidle commented 4 years ago

Ok, the lib freezing when no sensor is connected has been fixed. There was an infinite loop in I2CRead.

nseidle commented 4 years ago

Alrighty! The default inter-measurement period is set to 0xF89 which (using their function) equates to 103ms.

The status polling that is done within VL53L1X_SensorInit() has no delay. So VL53L1X_CheckForDataReady() is checked 50 times very quickly and because it takes 103ms before data will be ready, it exits with VL53L1_ERROR_TIME_OUT. I checked this with a print and a delay and correspondingly saw ~100 to ~110ms timeout before data ready returned successfully, and the sensor worked as expected.

The fix is to wait at least 103ms before starting our poll.

For good measure, I inserted an additional 1ms delay into the poll. This will give us an additional 50ms of wiggle room before failure.

Fix pushed!