Showing results for 
Search instead for 
Did you mean: 

Unable to get measures from VL53L4CX

Associate III


I'm in doing a project to measure the distance of my car from the wall. I'm in the software development phase using an ESP32 and a VL53L4CX to measure the distance.

I'm using ESPHome to develop the entire thing and due to the reason there is no specific driver for this distance sensor I started to develop one.

I copied source code from github stm32duino VL53L4CX repository and perform a few modifications to deal with GPIOPin instead of pin numbers and I2CDevice instead of Wire. It took me a couple of days to finish all required changes but I'm able to send and receive data using the I2C.

Following the code structure in the examples, I'm able to successfully perform the calls to InitSensor() and StartMeasurement(). I've log files where I check the possible error codes on every call, and the initialisation phase is executed without any errors. This includes calls to:



But later, inside the update() method, every call to

VL53L4CX_GetMeasurementDataReady() returns 00 and no distance measure is ever available.

Also If I debug I2C communication, reading register 0x0031 returns 0x03, what means there is no data available.

I've never been able to test if the device works properly as I never received a valid measure.

What could be wrong?

Thank you for your help

Note: the links have 'colon', 'slash' and 'dot' because new users cannot add links.

Associate III

After some additional tests.

If I force a call to VL53L4CX_ClearInterruptAndStartMeasurement before calling VL53L4CX_StartMeasurement in the setup function, then VL53L4CX_GetMeasurementDataReady() returns NewDataReady != 0 but VL53L4CX_GetMultiRangingData() returns all 0s.

The observation of all 0s is obtained activating VERY_VERBOSE logs at the I2C level.

At this time I don't know how to continue. Shall I calibrate the device? Is it not calibrated already?

Thank you for your help

Associate III

It took me long time to find the root of the problem. I just wanted to document here the root of all my problems just to help others with similar issues.

I'm using an ESP32 and the distance sensor driver was slightly modified to run in the esphome development environment. The problem was related with memory management of ESP32 in that particular scenario.

The driver instance class was created during the setup method. And I removed MyDevice from the source code and used only Dev (that was a pointer to MyDevice) of course, I was doing a malloc and free in order to reserve the required memory for the driver. But, that memory was reserved in the heap of the processor running the setup method. But, the loop method was running in the other processor with different heap so that caused plenty of issues when configuring the device.

I finally decided to allocate MyDevice as a static variable so it is allocated in a shared memory that can be used by either processor of the ESP32.


i am experiencing the exact same issues that you did through this process. Would you be able to help me out and save some headaches by providing the code you used? I'm running micropython on my ESP32-S2. i will buy you a coffee for helping me out!

Associate III

Hello there!

It is long time since I'm not using that code anymore. My response is based on what I remember the issue was.

General response: The issue was related with wrong memory allocation of the VL53L4CX. It has to be created on heap memory in order to the allocation to survive. The problems I was experimenting happened because the allocation of the VL53L4CX object was created on the stack during the creation of the wrapper class and that object instance was not available later when it was needed.

How I fixed it. I turned my variable of type VL53L4CX into a pointer to VL53L4CX class and allocated it using this->driver = new VL53L4CX() (remember to do a delete this->driver in the wrapper class destructor.

Then, I changed all calls to be this->driver->VL53L4CX_StartMeasurement() etc.

Hope this helps!