General Arduino

SEN0378 VL53L3CX ToF on ESP32 DevKit V1 Demo Code [Working Demo]

userHead Jona.Gladines 2026-03-16 05:36:15 17 Views0 Replies

Hello,

 

I was playing with a SEN0378 VL53L3CX ToF Sensor on an ESP32 Devkit V1 and found that the provided example code (https://wiki.dfrobot.com/sen0378/docs/20725) for the Nucleo board doesn't directly work for the ESP32 Devkit V1.

 

After some puzzling with info from various other sources, I managed to get a working version for the ESP32 Devkit V1. So I thought I'd better share this

to make life easier for others.

 

Additionally, the original tutorial linked above describes to alter Wire.h to enlarge the I2C buffer to 256. With the standard Wire.h for the Espressif ESP32 board this can be done at runtime (so without altering the Wire.h) using Wire.setBufferSize(256);

 

Cheers,

 

Jona

 

 Here is the code:

 

/**

 ******************************************************************************

 * @file    VL53L3CX_Sat_HelloWorld_ESP32.ino

 * @author  SRA

 * @version V1.0.1

 * @date    15 March 2026

 * @brief   Arduino test application for the STMicrolectronics VL53L3CX

 *          proximity sensor satellite based on FlightSense.

 *          This application makes use of C++ classes obtained from the C

 *          components' drivers.

 ******************************************************************************

 * @attention

 *

 * <h2><center>&copy; COPYRIGHT(c) 2020 STMicroelectronics</center></h2>

 *

 * Redistribution and use in source and binary forms, with or without modification,

 * are permitted provided that the following conditions are met:

 *   1. Redistributions of source code must retain the above copyright notice,

 *      this list of conditions and the following disclaimer.

 *   2. Redistributions in binary form must reproduce the above copyright notice,

 *      this list of conditions and the following disclaimer in the documentation

 *      and/or other materials provided with the distribution.

 *   3. Neither the name of STMicroelectronics nor the names of its contributors

 *      may be used to endorse or promote products derived from this software

 *      without specific prior written permission.

 *

 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"

 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE

 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE

 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE

 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL

 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR

 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER

 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,

 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE

 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

 *

 ******************************************************************************

 */

/*

 * To use this sketch you need to connect the VL53L3CX satellite sensor directly to the Nucleo board with wires in this way:

 * pin 1 (Interrupt) of the VL53L3CX satellite connected to pin D4 of the ESP32 Devkit V1 Board

 * pin 2 (SCL_I) of the VL53L3CX satellite connected to pin D22 (SCL) of the ESP32 Devkit V1 Board.

 * pin 3 (XSDN_I) of the VL53L3CX satellite connected to pin 13 of the ESP32 Devkit V1 Board

 * pin 4 (SDA_I) of the VL53L3CX satellite connected to pin D21 (SDA) of the ESP32 Devkit V1 Board

 * pin 5 (VDD) of the VL53L3CX satellite connected to 3V3 pin of the ESP32 Devkit V1 Board

 * pin 6 (GND) of the VL53L3CX satellite connected to GND of the ESP32 Devkit V1 Board

 */

/* Includes ------------------------------------------------------------------*/

#include <Arduino.h>

#include <Wire.h>

#include <vl53lx_class.h>


 

#define SerialPort Serial


 

// Components.

#define  DEVICE_I2C_ADDR  0x12

#define  XSHUT_PIN  13

#define  INT_PIN    4  //GPIO0

VL53LX sensor_vl53lx_sat(&Wire, XSHUT_PIN);



 

/* Setup ---------------------------------------------------------------------*/


 

void setup()

{


 

   // Initialize serial for output.

   SerialPort.begin(115200);

   while (!Serial);

   delay(50);

   SerialPort.println("Starting...");


 

   // Initialize I2C bus.

   Wire.begin();

   Wire.setClock(400000);

   Wire.setBufferSize(256);


 

   // Configure VL53LX satellite component.

   sensor_vl53lx_sat.begin();


 

   // Switch off VL53LX satellite component.

   sensor_vl53lx_sat.VL53LX_Off();


 

   //Initialize VL53LX satellite component.

   sensor_vl53lx_sat.InitSensor(DEVICE_I2C_ADDR);


 

   // Start Measurements

   sensor_vl53lx_sat.VL53LX_StartMeasurement();


 

   pinMode(LED_BUILTIN, OUTPUT);

}


 

void loop()

{

   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);


 

   //Led on

   digitalWrite(LED_BUILTIN, HIGH);


 

   if((!status)&&(NewDataReady!=0))

   {

      status = sensor_vl53lx_sat.VL53LX_GetMultiRangingData(pMultiRangingData);

      no_of_object_found=pMultiRangingData->NumberOfObjectsFound;

      snprintf(report, sizeof(report), "VL53LX Satellite: Count=%d, #Objs=%1d ", pMultiRangingData->StreamCount, no_of_object_found);

      SerialPort.print(report);

      for(j=0;j<no_of_object_found;j++)

      {

         if(j!=0)SerialPort.print("\r\n                               ");

         SerialPort.print("status=");

         SerialPort.print(pMultiRangingData->RangeData[j].RangeStatus);

         SerialPort.print(", D=");

         SerialPort.print(pMultiRangingData->RangeData[j].RangeMilliMeter);

         SerialPort.print("mm");

         SerialPort.print(", Signal=");

         SerialPort.print((float)pMultiRangingData->RangeData[j].SignalRateRtnMegaCps/65536.0);

         SerialPort.print(" Mcps, Ambient=");

         SerialPort.print((float)pMultiRangingData->RangeData[j].AmbientRateRtnMegaCps/65536.0);

         SerialPort.print(" Mcps");

      }

      SerialPort.println("");

      if (status==0)

      {

         status = sensor_vl53lx_sat.VL53LX_ClearInterruptAndStartMeasurement();

      }

   }


 

   digitalWrite(LED_BUILTIN, LOW);

}