# Getting Started with the VL53L5CX ToF Sensor

**URL:** <https://forum.digikey.com/t/getting-started-with-the-vl53l5cx-tof-sensor/20900>\
**Category:** Sensors Transducers\
**Tags:** stmicroelectronics, evaluation-boards, associated-products, optical-sensors\
**Created:** [March 16, 2022, 9:25pm UTC](https://forum.digikey.com/t/getting-started-with-the-vl53l5cx-tof-sensor/20900 "2022-03-16T21:25:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matt\_Mielke](https://sea2.discourse-cdn.com/digikey/user_avatar/forum.digikey.com/matt_mielke/32/71373_2.png) [@Matt\_Mielke](https://forum.digikey.com/u/Matt_Mielke)\
**Post date:** [March 16, 2022, 9:25pm UTC](https://forum.digikey.com/t/getting-started-with-the-vl53l5cx-tof-sensor/20900/1 "2022-03-16T21:25:22Z")

</div>

The [VL53L5CX](https://www.digikey.com/en/products/detail/stmicroelectronics/VL53L5CXV0GC-1/14552424) is a multizone ranging Time-of-Flight (ToF) sensor capable of producing ranging data in either 4 x 4 or 8 x 8 separate zones. Each zone can detect up to four targets and provide distance measurements up to four meters. The maximum ranging frequency is 60 Hz when configured with 4 x 4 resolution and 15 Hz when configured with 8 x 8 resolution. The driver supports many additional features such as crosstalk calibration when coverglass is used, object threshold detection for low-power operation, and motion detection.

![demo_animation](https://global.discourse-cdn.com/digikey/original/3X/a/8/a840445fa44362ccb9001db8070d0d17da384f3a.gif)

The driver for the VL53L5CX comes in two flavors. The [ULD API](https://www.st.com/content/st_com/en/products/embedded-software/imaging-software/stsw-img023.html) is a bare-bones, optimized collection of C functions with only three files required for basic functionality. On the other hand, the [X-CUBE-TOF1 expansion software package](https://www.st.com/content/st_com/en/products/ecosystems/stm32-open-development-environment/stm32cube-expansion-software/stm32-ode-sense-sw/x-cube-tof1.html) includes several additional layers of abstraction and example applications, making it ideal for prototyping with the [X-NUCLEO-53L5A1 expansion board](https://www.digikey.com/en/products/detail/stmicroelectronics/X-NUCLEO-53L5A1/14552449) and/or the [VL53L5CS-SATEL satellite board(s)](https://www.digikey.com/en/products/detail/stmicroelectronics/VL53L5CX-SATEL/14552430). The X-CUBE-TOF1 package is well-integrated into the STM32CubeMX code configurator software and thorough getting started instructions are included in [UM2853](https://www.st.com/content/ccc/resource/technical/document/user_manual/group2/8b/ba/ee/3b/67/70/40/28/DM00779659/files/DM00779659.pdf/jcr:content/translations/en.DM00779659.pdf). Therefore, this article will focus on the ULD API as it requires a bit more manual work to add the driver to the project. The last section also provides resources to recreate the example project shown in the animation above.

# Requirements

Before getting started, the following firmware, software, and hardware items should be acquired.

- **STSW-IMG023** (Version 1.3.6 used in this tutorial) 
  - This package contains the platform independent VL53L5CX ULD driver files (written in C). It can be found [here](https://www.st.com/en/embedded-software/stsw-img023.html).

- **A Nucleo board**
  - Though the [NUCLEO-F401RE](https://www.digikey.com/en/products/detail/stmicroelectronics/NUCLEO-F401RE/4695525) board was used in this tutorial, any [Nucleo board](https://www.digikey.com/en/products/filter/evaluation-boards-embedded-mcu-dsp/786?s=N4IgTCBcDaIHYFcDGAbApgexAXQL5A) will work (though the instructions may have to be altered slightly). Note that a USB cable is _not_ included.

- **X-NUCLEO-53L1A1**
  - An expansion board for evaluating the VL53L5CX module. It can be purchased [here](https://www.digikey.com/en/products/detail/stmicroelectronics/X-NUCLEO-53L5A1/14552449).

- **STM32CubeIDE** (Version 1.11.2 used in this tutorial)
  - Feel free to use another IDE if you prefer, e.g. Keil, IAR, etc. I wholeheartedly recommend using STM32CubeIDE, though. Especially if you are new to STM32 development. It may be downloaded from [here](https://www.st.com/en/development-tools/stm32cubeide.html)

# Configure the STM32 Device

1. If not done already, create a new STM32CubeIDE project ( **File** \> **New** \> **STM32 Project** ). Select your target device using either the _MCU/MPU Selector_ or _Board Selector_ tabs. For this tutorial, I chose the [NUCLEO-F401RE](https://www.digikey.com/en/products/detail/stmicroelectronics/NUCLEO-F401RE/4695525) development board because it’s what’s included with the [P-NUCLEO-53L5A1 evaluation kit](https://www.digikey.com/en/products/detail/stmicroelectronics/P-NUCLEO-53L5A1/14552445) . Give your project a name and click **Finish**.

2. Using the Device Configurator Tool (double click the `.ioc` file in the Project Explorer to launch it if it didn’t open automatically), configure the device to interface with the VL53L5CX sensor. Start by configuring the device’s GPIO pins for the I2C instance, external interrupt signal, and enable/reset lines in the Pinout view. For this example, I used the center module on the [X-NUCLEO-53L5A1 expansion board](https://www.digikey.com/en/products/detail/stmicroelectronics/X-NUCLEO-53L5A1/14552449), and therefore configured my device based on the schematic diagram provided in Figure 2 of [UM2889](https://www.st.com/content/ccc/resource/technical/document/user_manual/group2/ea/c7/f0/e3/d6/c8/41/2d/DM00800667/files/DM00800667.pdf/jcr:content/translations/en.DM00800667.pdf) (see Table 1 below). My final pinout configuration is shown in Figure 1.

**Table 1:** GPIO configuration for the center module of the [X-NUCLEO-53L5A1 expansion board](https://www.digikey.com/en/products/detail/stmicroelectronics/X-NUCLEO-53L5A1/14552449)

| X-NUCLEO-53L5CX Signal Name | Arduino Pin | NUCLEO-F401RE Pin | Pin Configuration |
| --- | --- | --- | --- |
| I2C\_RST\_C | D3 | PB3 | GPIO\_Output |
| LPn\_C | D5 | PB4 | GPIO\_Output |
| PWR\_EN\_C | A3 | PB0 | GPIO\_Output |
| INT\_C | A2 | PA4 | GPIO\_EXTI_x_ |
| SCL | D15 | PB8 | I2C_x_\_SCL |
| SDA | D14 | PB9 | I2C_x_\_SDA |

 ![ide_prelimPinout](https://global.discourse-cdn.com/digikey/original/3X/9/7/9782bf2a7885b68927b5dd56f84aaeaee892939c.png)  
**Figure 1:** Pinout configuration changes corresponding to the signals in Table 1

1. From the Components List on the left, choose the I2C peripheral corresponding to the SDA and SCL pins configured in the previous step. For my device, I chose **I2C1**. In the _Mode_ panel, select “I2C” from the drop-down list for the I2C setting. Note that after doing this, the SDA and SCL pins in the Pinout View should change from yellow to green. In the _Configuration_ panel, select “Fast Mode” from the drop-down list for the I2C Speed Mode setting. The final I2C configuration should resemble that shown in Figure 2.

 ![ide_i2cConfig](https://global.discourse-cdn.com/digikey/original/3X/6/1/618f04c641ce7d1fde07d125b07f14edc86adca4.png)  
**Figure 2:** I2C configuration for the VL53L5CX sensor

1. Select the GPIO peripheral from the Components List and change the GPIO Output Level to “High” for the PWR\_EN\_C and LPn\_C pins. For the INT\_C pin, change the GPIO mode to “External Interrupt Mode with Falling edge trigger detection”. Note that the default “No pull-up and no pull-down” setting for the INT\_C pin is left unchanged because there is a 47 kΩ pull-up resistor mounted on the X-NUCLEO-53L5A1 board (R5). Optionally, add the corresponding User Labels to the VL53L5CX interface pins for clarity. Each of these changes to the GPIO settings is highlighted in Figure 3 below.

 ![ide_gpioConfig](https://global.discourse-cdn.com/digikey/original/3X/1/7/17f3435c2981775c577f7ff0db9c6f5340e2bd42.png)  
**Figure 3:** GPIO configuration for the VL53L5CX sensor

1. Under the _NVIC_ tab, check the box next to the EXTI Line corresponding to the INT\_C pin as shown in Figure 4.

 ![ide_nvicConfig](https://global.discourse-cdn.com/digikey/original/3X/f/3/f3a84b7d29ff0025e20d20b56974ec0c38436da1.png)  
**Figure 4:** Enable the external interrupt corresponding to the VL53L5CX INT signal.

1. Save the `.ioc` file to generate code for the project.

# Add the Driver

Again, this tutorial explains how to add and utilize the VL53L5CX ULD API. If you would rather use the X-CUBE-TOF1 package, please follow the instructions provided in [UM2853](https://www.st.com/content/ccc/resource/technical/document/user_manual/group2/8b/ba/ee/3b/67/70/40/28/DM00779659/files/DM00779659.pdf/jcr:content/translations/en.DM00779659.pdf).

1. Download the [ULD API](https://www.st.com/content/st_com/en/products/embedded-software/imaging-software/stsw-img023.html) and extract the file to your desired location. At the time of writing, version 1.3.6 is the most recent release.

2. In STM32CubeIDE, right-click on the Drivers folder in the Project Explorer and select **Import**. Using the Import Wizard, choose **File System** from the General category. Click **Next**.

3. Populate the “From Directory:” field with the path to the ULD driver: `<extraction_location>/VL53L5CX_ULD_driver_1.3.0`. As shown in Figure 5, select all resources in the VL53L5CX\_ULD\_API directory. Click **Finish**.

 ![ide_importDriverFiles](https://global.discourse-cdn.com/digikey/original/3X/6/9/6942e90bdfc45bf33d37d1a14b67bba7e4f3fa17.png)  
**Figure 5:** Importing the VL53L5CX ULD driver

1. Right-click on `Drivers/VL53L5CX_ULD_API/inc` directory in the Project Explorer. Select **Add/remove include path…** and click **OK**.

2. Copy and paste the example platform files into the project.

- Copy `<extraction_location>/VL53L5CX_ULD_driver_1.3.6/CubeIDE_F401RE_Example/Core/Inc/platform.h` and paste it into `Core/Inc`.

- Copy `<extraction_location>/VL53L5CX_ULD_driver_1.3.6/CubeIDE_F401RE_Example/Core/Src/platform.c` and paste it into `Core/Src`.

The changes made to the directory structure are shown below in Figure 6. To begin using the driver, add the line `#include "vl53l5cx_api.h"` to the Private includes section of the `main.c` file. The project should build successfully.

 ![ide_projStruct](https://global.discourse-cdn.com/digikey/original/3X/7/5/75309c6dc1b8b64e1d1009e87cedb68e0bc0c485.png)  
**Figure 6:** Platform files

# Example Application

The animation shown at the top of this article shows a simple example application using an [8 x 8 NeoPixel matrix](https://www.digikey.com/en/products/detail/adafruit-industries-llc/2872/5885905). A still image of the hardware setup is shown below in Figure 7. An external 5V (2A) power supply is connected to E5V on the Nucleo Board and the +5V pin supplies power to the NeoPixel matrix. Pin PB10 (D6 on the Arduino headers) provides the NeoPixel control signal and, thus, is connected to DIN on the matrix. For detailed information on generating the NeoPixel control signal, see [Controlling NeoPixels with STM32](https://forum.digikey.com/t/controlling-neopixels-with-stm32/20527).

 ![20220316_090915_crop](https://global.discourse-cdn.com/digikey/original/3X/c/9/c9d32772f25f45d853b18e141590935e8e32ae90.jpeg)  
**Figure 7:** Application photo

To power up the Nucleo board, **the proper startup sequence must be followed!**

1. Ensure JP5 is in the E5V position (between pins 2 and 3)

2. Ensure JP1 is removed

3. Connect and power on the external 5V power source

4. Check that LD3 is on

5. Connect the PC to the ST-LINK USB connector (CN1)

> I highly recommend reading section 6.3 of [UM1724](https://www.st.com/content/ccc/resource/technical/document/user_manual/98/2e/fa/4b/e0/82/43/b7/DM00105823.pdf/files/DM00105823.pdf/jcr:content/translations/en.DM00105823.pdf) before using an external power source to avoid damaging your board!

The `main.c` file for this application is available on the [project’s GitHub Repository](https://github.com/eewiki/vl53l5cx_neopixel_demo). Note that the printf statements are enabled using the method described in [Easily Use printf on STM32](https://forum.digikey.com/t/easily-use-printf-on-stm32/20157).

---

<div class="post-metadata">

**Author:** ![Vector2915](https://avatars.discourse-cdn.com/v4/letter/v/e495f1/32.png) [@Vector2915](https://forum.digikey.com/u/Vector2915)\
**Post date:** [September 9, 2024, 12:28pm UTC](https://forum.digikey.com/t/getting-started-with-the-vl53l5cx-tof-sensor/20900/2 "2024-09-09T12:28:25Z")

</div>

Hi @Matt_Mielke , I am new to the world of ToF sensors, and I have started a project using the SATEL-VL53L7CX breakout board and the STM32F303K8 board, where I have set up the pins INT, I2C\_RST, SDA, SCL, LPn, PWREN, AVDD, and GND as shown in the picture. I configured the I2C as you can see in the next image. In my STM project, I also implemented the ULD for my sensor STSW-IMG036, where I modified _platform.c_ and _platform.h_ according to your code, and I further adjusted _main.c_ to my needs.

However, I encountered a problem when the function `status = vl53l7cx_is_alive(&Dev, &isAlive);` is executed. It always returns 2, which indicates that the I2C status is BUSY. This is likely an issue with the I2C configuration, but I have no idea what could be wrong, and I’m feeling frustrated. Could you help me?

 ![Snímka obrazovky z 2024-09-09 14-25-49](https://global.discourse-cdn.com/digikey/original/3X/b/3/b33a955c192ec7eba205bf3613678d198364df36.png)  
 ![Snímka obrazovky z 2024-09-09 14-25-56](https://global.discourse-cdn.com/digikey/original/3X/b/5/b5ec10a3d3bc38397e3f771c603622d5da559817.png)  
 ![Snímka obrazovky z 2024-09-09 14-26-13](https://global.discourse-cdn.com/digikey/original/3X/8/5/8550bcf4ee11a42a3745b35fd68cbb38614705d2.png)

main.c  
[main.c](https://forum.digikey.com/uploads/short-url/51PB1GzE1xtTcXKS1gS5Yagu3ox.c) (7,3 KB)

---

<div class="post-metadata">

**Author:** ![Matt\_Mielke](https://sea2.discourse-cdn.com/digikey/user_avatar/forum.digikey.com/matt_mielke/32/71373_2.png) [@Matt\_Mielke](https://forum.digikey.com/u/Matt_Mielke)\
**Post date:** [September 10, 2024, 5:27pm UTC](https://forum.digikey.com/t/getting-started-with-the-vl53l5cx-tof-sensor/20900/3 "2024-09-10T17:27:00Z")

</div>

Hi @Vector2915,

The config you posted looks OK to me… Can you verify that solder bridges SB5 and SB7 are populated on your SATEL-VL53L7CX board?

 ![image](https://global.discourse-cdn.com/digikey/original/3X/b/a/ba118c4812b524c0d43ce4f3ccabd5e1bcd6159d.png)

At this point, I would verify that the MCU is behaving as expected. Any chance you can probe the bus with a logic analyzer or oscilloscope? You should also use an instrument to verify that I2C\_RST is low and LPn and PWREN are high.

---

<div class="post-metadata">

**Author:** ![Vector2915](https://avatars.discourse-cdn.com/v4/letter/v/e495f1/32.png) [@Vector2915](https://forum.digikey.com/u/Vector2915)\
**Post date:** [September 10, 2024, 6:25pm UTC](https://forum.digikey.com/t/getting-started-with-the-vl53l5cx-tof-sensor/20900/4 "2024-09-10T18:25:57Z")

</div>

Thank you for the quick response, @Matt_Mielke. I checked with a digital multimeter to see if SB5 is connected to SCL and SB7 to SCL, and they are, whether it’s on the pads on the sides of the breakout board or the pins on the flying leads + photo. I currently don’t have a logic analyzer or oscilloscope, but I will have a logic analyzer by tomorrow, as it is currently at work.

 ![20240910_200510](https://global.discourse-cdn.com/digikey/original/3X/0/0/00362edc278e77677a0160e12a7baef6fa786009.jpeg)

Using the digital multimeter, I checked the pins while uploading the code to the STM, and I found that I2C\_RST is at 0V, and both LPn and PWREN are at 3.3V, which seems correct. However, what concerns me is that both SDA and SCL are only at 1.12V.
