The VL53L8CX sensor

The VL53L8CX sensor has a built-in firmware function for detecting motion in a scene. This motion indicator is calculated based on consecutive frames. This option is available using the plugin `vl53l8cx_plugin_motion_indicator`.The motion indicator is initialized using the `vl53l8cx_motion_indicator_init()` function. If the user wants to change the sensor’s resolution, they must update the motion indicator’s resolution using the dedicated function `vl53l8cx_motion_indicator_set_resolution()` .Users can also change the minimum and maximum distances for motion detection. The difference between the minimum and maximum distances cannot exceed 1500 mm. By default, the distance is initialized between 400 mm and 1500 mm.The results are stored in the `motion_indicator` field. In this field, the `motion` array gives the motion intensity value for each region. High values ​​indicate high motion variation between frames. Typical motion will give a value between 100 and 500. This sensitivity depends on the integration time, target distance, and target reflectivity.For low-power applications, the ideal combination is to use a motion indicator with an autonomous ranging mode, and to program a detection threshold on the motion. This allows for the detection of motion changes within the field of view (FoV) while maintaining minimal power consumption.

Sample application

The VL53L8CX sensor

Source code download

Hardware preparation

First, you’ll need a development board. I’ve provided a custom-designed one; you can request one if you need one.
The main controller is an STM32G431CB, and the TOF is a VL53L8CX.
Insert image description here

Generate STM32CUBEMX

Select MCU

The MCU used in the test version is STM32G431CB.
Insert image description here

Serial port configuration

According to the schematic diagram, PA9 and PA10 are set as the serial ports of the development board.
Insert image description here

Configure the serial port.
Insert image description here

IIC Configuration

In this application, the VL53L8CX module communicates with the host controller via the I2C (IIC) interface. Specifically, the I2C pins of the VL53L8CX module are connected to the PA8 and PB5 I/O ports of the host controller.

Insert image description here
Insert image description here

Configure IIC to fast mode with a speed of 400k.

Insert image description here

LPn settings

If IIC communication is to be performed, LPn should be set to a high level.

Insert image description here

The corresponding pin here is PA12.

Insert image description here

Configured as PA12.

Insert image description here

INT settings

In autonomous mode, the availability of data can be determined by obtaining the INT pin.

Insert image description here
Configure PA11 as input mode.
Insert image description here

X-CUBE-TOF1

This section describes how to add the X-CUBE-TOF1 package to a project using STM32CubeMX when the sample application is not needed. With this setup, only the driver layer is configured.
Insert image description here

Serial port redirection

Open the magic wand and check MicroLIB.
Insert image description here

In main.c, add the header file; otherwise, the error “identifier “FILE” is undefined” will occur.





  1. /* USER CODE BEGIN Includes */
  2. #include"stdio.h"
  3. /* USER CODE END Includes */

Function declaration and serial port redirection:





  1. /* USER CODE BEGIN PFP */
  2. int fputc(int ch,FILE*f){
  3. HAL_UART_Transmit(&huart1 ,(uint8_t*)&ch,1,0xFFFF);
  4. return ch;
  5. }
  6. /* USER CODE END PFP */

Code configuration

In the custom_ranging_sensor.c code, there is code that resets the VL53L8CX via an I/O port driver. Since the corresponding I/O is not configured, it needs to be commented out.

Insert image description here

Testing process

  1. Initialization Phase (User Code Begins 2):
    ○ Declare and initialize several variables, including status, isReady, i (loop variable), and structures related to VL53L8CX motion detection.
    ○ Obtain a reference to the VL53L8CX device.
  2. Configure the motion indicator:
    ○ Initialize the motion indicator using a 4×4 resolution. If this fails, print an error message and return a status.
    ○ Optionally, change the minimum and maximum distances used to detect motion. These distances have limitations; exceeding these limits will return an error.
    ○ The sensor resolution can be changed if needed by the user. The motion indicator resolution also needs to be updated.
    ○ Set the ranging frequency to 2Hz for the example. If setting fails, print an error message.
  3. Start distance measurement:
    ○ Start the distance measurement function of the VL53L8CX device.
  4. Ranging Loop (Infinite Loop):
    ○ Check if new data is available from the VL53L8CX device.
    ○ If new data is available:
    ■ Acquire ranging data.
    ■ Print data stream count (possibly a consecutive measurement number).
    ■ Print motion intensity for each area (16 areas at 4×4 resolution).
    This program is primarily used to configure the motion indicator of the VL53L5CX sensor, then continuously acquires ranging data from the sensor and prints the motion intensity for each area.

TOF code configuration

Add the corresponding header file to main.c.





  1. /* USER CODE BEGIN Includes */
  2. #include"stdio.h"
  3. #include"custom_ranging_sensor.h"
  4. #include"vl53l8cx_plugin_motion_indicator.h"
  5. /* USER CODE END Includes */

Function and variable definitions:





  1. /* USER CODE BEGIN PFP */
  2. int fputc(int ch,FILE*f){
  3. HAL_UART_Transmit(&huart1 ,(uint8_t*)&ch,1,0xFFFF);
  4. return ch;
  5. }
  6. /* USER CODE END PFP */

Add TOF initialization.
This is mainly for configuring the motion indicator.





  1. /* USER CODE BEGIN 2 */
  2. uint8_t status, isReady, i;
  3. VL53L8CX_Motion_Configuration motion_config;/* 运动配置 */
  4. VL53L8CX_Object_t*pL8obj = CUSTOM_RANGING_CompObj[CUSTOM_VL53L8CX];
  5. VL53L8CX_ResultsData Results;/* 来自VL53L5CX的结果数据 */
  6. /*********************************/
  7. /* 编程运动指示器 */
  8. /*********************************/
  9. /* 使用4x4分辨率创建运动指示器 */
  10. status = vl53l8cx_motion_indicator_init(&pL8obj->Dev,&motion_config, VL53L8CX_RESOLUTION_4X4);
  11. if(status)
  12. {
  13. printf("运动指示器初始化失败,状态 : %u\n", status);
  14. return status;
  15. }
  16. /* (可选) 更改用于检测运动的最小和最大距离。最小和最大之间的差异
  17. * 不得超过1500mm,最小值不能小于400mm,否则下面的函数返回错误127 */
  18. status = vl53l8cx_motion_indicator_set_distance_motion(&pL8obj->Dev,&motion_config,1000,2000);
  19. if(status)
  20. {
  21. printf("设置运动指示器距离失败,状态 : %u\n", status);
  22. return status;
  23. }
  24. /* 如果用户想要更改分辨率,他还需要更新运动指示器的分辨率 */
  25. //status = vl53l5cx_set_resolution(&Dev, VL53L5CX_RESOLUTION_4X4);
  26. //status = vl53l5cx_motion_indicator_set_resolution(&Dev, &motion_config, VL53L5CX_RESOLUTION_4X4);
  27. /* 为了示例,增加测距频率 */
  28. status = vl53l8cx_set_ranging_frequency_hz(&pL8obj->Dev,2);
  29. if(status)
  30. {
  31. printf("设置vl53l8cx测距频率失败,状态 : %u\r\n", status);
  32. return status;
  33. }
  34. /*********************************/
  35. /* 测距循环 */
  36. /*********************************/
  37. status = vl53l8cx_start_ranging(&pL8obj->Dev);
  38. /* USER CODE END 2 */

Main program

The main program can obtain the corresponding isReady bit status to determine whether the data is ready or to check the INT IO status.





  1. /* Infinite loop */
  2. /* USER CODE BEGIN WHILE */
  3. while(1)
  4. {
  5. if(HAL_GPIO_ReadPin ( GPIOA, GPIO_PIN_11)==0)
  6. {
  7. status = vl53l8cx_check_data_ready(&pL8obj->Dev,&isReady);
  8. if(isReady)
  9. {
  10. vl53l8cx_get_ranging_data(&pL8obj->Dev,&Results);
  11. /* 由于传感器默认设置为4x4模式,我们总共有
  12. * 16个区域要打印。在此示例中,只打印第一个区域的数据 */
  13. printf("打印数据编号 : %3u\n", pL8obj->Dev.streamcount);
  14. for(i =0; i <16; i++)
  15. {
  16. printf("区域 : %3d, 运动强度 : %3lu\n",
  17. i,
  18. Results.motion_indicator.motion[motion_config.map_id[i]]);
  19. }
  20. printf("\n");
  21. }
  22. }
  23. /* USER CODE END WHILE */
  24. /* USER CODE BEGIN 3 */
  25. }
  26. /* USER CODE END 3 */

Demonstration results

The intensity of motion in a stationary state is shown below.

Insert image description here

The intensity of exercise during the exercise was measured as shown below.

Insert image description here

1

This topic was approved by the girl on the other side on 2026-7-10 at 11:04.

error: Content is protected !!
Scroll to Top