Drive TOF module VL53l0x(3) based on STM32CUBEMX----drive multiple VL53L0X

Drive TOF module VL53l0x based on STM32CUBEMX----3. Drive multiple VL53L0X

Overview

In this chapter, we will explore how to drive multiple VL53L0X sensors simultaneously for distance measurement. We'll cover how to effectively manage communication and control between multiple sensors to ensure they can make accurate distance measurements simultaneously. After studying this chapter, you will be able to understand how to take advantage of multiple VL53L0X sensors to provide applications with more comprehensive environmental awareness capabilities.
I am currently taking ST courses. If you need samples, you can join the group and apply: 615061293.
Insert image description here

Video teaching

https://www.bilibili.com/video/BV1wN4y1X7aw/

Drive TOF module VL53l0x(3) based on STM32CUBEMX----drive multiple VL53L0X

sample application

https://www.wjx.top/vm/OhcKxJk.aspx#

Source code download

https://download.csdn.net/download/qq_24312945/88332775

Modify device address

The VL53L0X uses the I²C bus for communication. By default, the 7-bit address of the VL53L0X is 0x29. However, to avoid address conflicts and allow multiple sensors to share the same I2C bus, the user can modify the sensor's address by writing a custom 7-bit address to the register I2C_SLAVE_DEVICE_ADDRESS (register address 0x8A).
Insert image description here

The modified version is as follows.

// Public Methods //

void VL53L0X_setAddress(uint8_t add,uint8_t new_addr)
{
    
    
  VL53L0X_WriteByte(add,I2C_SLAVE_DEVICE_ADDRESS, new_addr & 0x7F);

}

In the VL53L0X.h file, this file contains the relevant definitions and configuration of the VL53L0X sensor.
In the VL53L0X.h file, you can define the address constants of two devices, representing different sensors respectively. For example, you can define an address constant using:

#define VL53L0X_DEFAULT_I2C_ADDR1 0x29  ///< The fixed I2C addres
#define VL53L0X_DEFAULT_I2C_ADDR2 0x30  ///< The fixed I2C addres

The above example shows the definition of two device address constants, representing different sensors. You can increase or decrease the definition of device address constants based on the number of sensors you actually use and your configuration needs.
In code, you can use these device address constants to specify the addresses of different sensors. For example, if you want to use the address of the first sensor, you can use VL53L0X_DEFAULT_I2C_ADDR1 to represent that address. Note that these address constants need to be mapped to the physical connection and configuration of the sensor. Be sure to assign the correct address constants to the appropriate sensors to ensure proper communication and operation.
When defining address constants, it is recommended to refer to the data sheet and related documentation of the VL53L0X sensor to learn more details about the sensor address and configuration.
The address value here is selected based on actual needs and hardware connection conditions. It is very important to ensure that each device has a unique address. Multiple VL53L0X sensors can be easily managed and operated by using defined device address constants. By assigning unique device address constants to each sensor, you can use these constants in your code to specify the address that corresponds to each sensor. This way, different sensors can be easily distinguished and the appropriate commands and configurations sent to communicate and operate each sensor. By using defined device address constants, multiple sensors can be easily managed and operated without the need to manually track and set the address of each sensor. This provides convenience and flexibility, especially for applications that require the use of multiple VL53L0X sensors simultaneously.

Configure VL53L0X

In the corresponding demo board, the pin diagram is as follows.
Insert image description here

In the code below, the process of initializing and configuring multiple VL53L0X sensors is shown. Each sensor is connected to the motherboard through different pins, and the corresponding sensor is selected for operation by controlling the level of the GPIO pin.
Here's an expanded description of the code:

  1. First, by controlling the level of the GPIO pin, set the corresponding pin to the RESET state to prepare to initialize the corresponding sensor. Specifically, for each sensor, they are set to the RESET state by controlling two GPIO pins, the chip select.
  2. Then, by controlling the level of the GPIO pin, the corresponding pin is set to the SET state to select the corresponding sensor for operation. For each sensor, set them to the SET state by controlling two GPIO pins, the chip select.
  3. A delay is added after each sensor switch to ensure a stable pin state switch. The length of this delay can be adjusted according to actual needs.
    Perform the following operations for each sensor:
    a. Initialize the sensor, using the VL53L0X_Init function, passing the sensor's address as a parameter.
    b. Modify the sensor address through the VL53L0X_setAddress function and use VL53L0X_DEFAULT_I2C_ADDR2 as the source address.

Through code, multiple VL53L0X sensors can be initialized and configured, and different sensors can be selected for operation as needed. Please note that the above code examples are for reference only and you need to modify and adjust accordingly according to your own hardware connections and needs. Be sure to refer to the VL53L0X sensor's documentation and datasheet for more details on initializing, configuring, and operating the sensor.

  /* USER CODE BEGIN 2 */
	// 启动第一个VL53L0X传感器并关闭第二个VL53L0X传感器
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_SET);
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_RESET);

	// 等待200ms以确保第一个VL53L0X传感器启动完成
	HAL_Delay(200);

	// 使用默认地址初始化第一个VL53L0X传感器
	if (!VL53L0X_Init(VL53L0X_DEFAULT_I2C_ADDR1, true))
	{
    
    
			printf("Failed to detect and initialize sensor!");
			while (1) {
    
    }  // 如果初始化失败,则无限循环
	}

	// 修改第一个VL53L0X的I2C地址,以便我们可以与第二个VL53L0X传感器通信
	VL53L0X_setAddress(VL53L0X_DEFAULT_I2C_ADDR1, VL53L0X_DEFAULT_I2C_ADDR2);

	// 启动第二个VL53L0X传感器
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_4, GPIO_PIN_SET);
	HAL_Delay(200);  // 等待200ms以确保第二个VL53L0X传感器启动完成

	// 使用之前的默认地址初始化第二个VL53L0X传感器
	if (!VL53L0X_Init(VL53L0X_DEFAULT_I2C_ADDR1, true))
	{
    
    
			printf("Failed to detect and initialize sensor!");
			while (1) {
    
    }  // 如果初始化失败,则无限循环
	}
	
  /* USER CODE END 2 */

main program

In the code, it is shown how to read the data of 2 VL53L0X sensors in a loop in the main program. Here is an expanded description of the code:

  1. In the while loop of the main program, first use the VL53L0X_readRangeSingleMillimeters function to read distance data from VL53L0X sensor 2 (device address is VL53L0X_DEFAULT_I2C_ADDR2). Then, use the printf function to print out the distance value.
  2. Then wait 0.5 seconds.
  3. Next, use the VL53L0X_readRangeSingleMillimeters function to read distance data from VL53L0X sensor 1 (device address is VL53L0X_DEFAULT_I2C_ADDR1). Then, use the printf function to print out the distance value.
  4. Then wait 0.5 seconds.
    Please note that the code examples are for reference only and you need to modify and adjust accordingly according to your actual hardware configuration, VL53L0X library and application requirements. Be sure to refer to the VL53L0X sensor's documentation and datasheet for more details on data reading and interpretation.
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    
    // 从第二个VL53L0X传感器读取距离值(单位:毫米)
    L = VL53L0X_readRangeSingleMillimeters(VL53L0X_DEFAULT_I2C_ADDR2);
    printf("L=%d", L);  // 打印从第二个VL53L0X传感器读取的距离值
    HAL_Delay(500);  // 延迟500ms(0.5秒)

    // 从第一个VL53L0X传感器读取距离值(单位:毫米)
    R = VL53L0X_readRangeSingleMillimeters(VL53L0X_DEFAULT_I2C_ADDR1);
    printf("R=%d", R);  // 打印从第一个VL53L0X传感器读取的距离值
    HAL_Delay(500);  // 延迟500ms(0.5秒)
		
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */

Test Results

The test distance results are shown below.

Insert image description here

Guess you like

Origin blog.csdn.net/qq_24312945/article/details/132843853