VL53L3CX - distance sensor

Hello Community,

I have purchased the VL53L3CX distance sensor

I have a wheeled robot moving towards an object. I would like to stop the rover when it approaches 10cm from the object in front of it.
Sometimes it works, but 20% of the cases it fails to detect the distance. Can you help me understand what could be the problem?

I use aruduino nano iot.

Here is the code:
I use the library “STM32duino VL53L3CX” by STMicroelectronics

#include <vl53lx_class.h>
#define DEV_I2C Wire

VL53LX sensor_vl53lx_sat(&DEV_I2C, A1);


void setup(){
  // init distance sensor
  DEV_I2C.begin();
  sensor_vl53lx_sat.begin();
  sensor_vl53lx_sat.VL53LX_Off();
  sensor_vl53lx_sat.InitSensor(0x12);
}

void loop(){
    // start measuring distance
    sensor_vl53lx_sat.VL53LX_StartMeasurement();

// measure distance until 10 cm
    do {
      VL53LX_MultiRangingData_t MultiRangingData;
      VL53LX_MultiRangingData_t* pMultiRangingData = &MultiRangingData;
      uint8_t NewDataReady = 0;
      // int no_of_object_found = 0, j;
      // char report[64];
      int status;

      do {
        status = sensor_vl53lx_sat.VL53LX_GetMeasurementDataReady(&NewDataReady);
      } while (!NewDataReady);

      if ((!status) && (NewDataReady != 0)) {
        status = sensor_vl53lx_sat.VL53LX_GetMultiRangingData(pMultiRangingData);
        // no_of_object_found = pMultiRangingData->NumberOfObjectsFound;
        char buffer[10];
        itoa(pMultiRangingData->RangeData[0].RangeMilliMeter, buffer, 10);
        log(buffer, true);

        if (status == 0) {
          status = sensor_vl53lx_sat.VL53LX_ClearInterruptAndStartMeasurement();
        }
        if (status == 0 && pMultiRangingData->NumberOfObjectsFound == 1 && pMultiRangingData->RangeData[0].RangeMilliMeter <= 100) {
          log("Distance sensor: object found at 10cm", true);
          break;
        }
      }
    } while (true);
}

Thank you for your help :))))

Hello.

If you post some pictures of your setup that shows the board and all of your connections, then I can check those and let you know if there are any potential issues there. What is the object you are trying to detect, how fast does the robot approach the object, and what type of environment are you operating in?

It might be a good idea for you to log or otherwise output some more information from your program so you can better understand what is going wrong. Specifically, you are looking for the condition if (status == 0 && pMultiRangingData->NumberOfObjectsFound == 1 && pMultiRangingData->RangeData[0].RangeMilliMeter <= 100); in the cases where you expect that condition to be met, but it is not, which part of the condition is not true? (Is the status non-zero, is the number of objects found not 1, or is the range inaccurate?) It looks like you’re already logging buffer; does that give you additional information? Could you post some of the log output here?

- Patrick

Hi Patrick,

Thanks for your reply.
I will send you the schematics in a couple of days, as I’m out of town.

I’ve tried to log more information, but it seems that the times that the sensor doesn’t work, also the wifi of the Arduino Nano IoT stops working.

Generally this happens close to the point the motors start. As said, 80% of the cases works… 20% not…

If other parts of your system are failing at the same time, and it corresponds with the motors turning on, that makes it sound like you might be dealing electrical noise or an inadequate power supply. How are you powering your robot? What motors does it use, and what other components might be drawing significant power? You might consider testing your robot with a different power supply, and you could try monitoring the I2C and power supply lines with an oscilloscope to look for signs of noise or the power dropping out.

- Patrick

hi Patrick,

I have managed to get the schematic

the replies to your questions
How are you powering your robot? with 2x LiFePo 4 batteries in series, they are charged and give 6.4v. The rover goes forwards, picks up the bottle of wine, and return back home where it get charged.

What motors does it use?
150:1 Micro Metal Gearmotor HP 6V

what other components might be drawing significant power? I have those 2 motors, the Arduino Nano IoT (maybe wifi gets a lot) and a big stepper motor, but this gets activated only at the end (it moves the grippers to pick up the bottle). Wifi and Distance sensor start not working before I activate the stepper.

Do you need pictures of the rover?

Hello.

Yes, pictures that show both sides of the board and all of your physical connections would be useful.

Have you tried my other suggestions about outputting more information from your program and monitoring your setup with a scope?

- Patrick

So, I have moved far the entire arduino. Wifi is stable, but the distance sensor has still sometimes this problem.

I have uploaded the sample sketch from STM (https://github.com/stm32duino/VL53L3CX/blob/main/examples/VL53L3CX_Sat_HelloWorld/VL53L3CX_Sat_HelloWorld.ino)

I get this as result from the log:

VL53LX Satellite: Count=213, #Objs=0 
VL53LX Satellite: Count=214, #Objs=4 status=12, D=222mm, Signal=0.70 Mcps, Ambient=1.95 Mcps
                               status=12, D=992mm, Signal=0.70 Mcps, Ambient=1.95 Mcps
                               status=12, D=1762mm, Signal=0.70 Mcps, Ambient=1.95 Mcps
                               status=12, D=2532mm, Signal=0.70 Mcps, Ambient=1.95 Mcps

any idea how to solve it?
when I restart arduino sometimes it works, it detects the right object, sometimes I get a loop of the above log

Hi, frankie.

When that line is logged, do you expect the sensor to be seeing something <10cm away? Are the four reported distances reasonable given what is in front of the sensor?

According to the VL53L3CX user manual, the status of 12 means “Indicate that there is a target, but the signal is too low to report ranging”. This might indicate that you are trying to sense something under marginal conditions where the sensor is unable to give a reliable result. Additionally, Objs=4 suggests there is probably more than one object detected, so you might try changing the conditional in your program to look for 1 or more objects instead of exactly 1 object.

If you don’t think that explains your issues, it would definitely be a good idea to look at things with a scope as Patrick mentioned to see if there are any signs of power instability or excessive noise. Another thing you could try is to temporarily disconnect the other parts of your system (including the motors and Wi-Fi) to see if that improves the behavior of the distance sensor. Also, it would still be helpful for you to show some pictures of your setup.

Kevin

Hello Kevin and Patrick,

Thank you for your support.

I’ve found a simple solution. The sensor fails sometimes to detect objects or distance. I would say once every 40/50 measurements.

Via software I’m filtering such rare cases and ignoring them.

Regards
Frens

2 Likes