Custom sensor component in ESPHome for VL53L4CX Time of Flight sensor?

Hi all,
I am attempting to create a custom i2c sensor component for the VL53L4CX and am running into trouble. I’m using a Wemos D1 Mini Esp32 and trying to implement an Adafruit VL53L4CX Time of Flight sensor. I have tested it in arduino IDE directly and it works as expected, but when trying to implement it into esphome it wont work.

I am using VL53L4CX as the library reference.

Following this guide to set up the custom sensor

#include "esphome.h"
#include "vl53l4cx_class.h"

#define DEV_I2C Wire
#define SerialPort Serial
int LED_BUILTIN = 2;

#define LedPin LED_BUILTIN
// Components.

// Components.
VL53L4CX sensor_vl53l4cx_sat(&DEV_I2C, 17);

class MyCustomSensor : public PollingComponent, public Sensor {
  VL53L4CX sensor_vl53l4cx_sat;
  // constructor
  MyCustomSensor() : PollingComponent(1000) {}

  float get_setup_priority() const override { return esphome::setup_priority::BUS; }

  void setup() override {
    pinMode(LedPin, OUTPUT);
    DEV_I2C.setPins(26, 27);
    // Configure VL53L4CX satellite component.
    // Switch off VL53L4CX satellite component.
    // sensor_vl53l4cx_sat.VL53L4CX_Off();
    //Initialize VL53L4CX satellite component.
    // Start Measurements

  void update() override {
    // This will be called every "update_interval" milliseconds.
    VL53L4CX_MultiRangingData_t MultiRangingData;
    VL53L4CX_MultiRangingData_t *pMultiRangingData = &MultiRangingData;
    digitalWrite(LedPin, HIGH);
    digitalWrite(LedPin, LOW);

This is my c++ file. As is this runs fine and outputs 42.0 once per second. If I uncomment the parts to initialize the sensor and start measurement in setup I get:

WARNING Can't connect to ESPHome API for tofsensor.local: Error connecting to ('', 6053): [Errno 111] Connect call failed ('', 6053)
INFO Trying to reconnect to tofsensor.local in the background

because its boot looping. I haven’t even tried to properly publish the distance state yet, was trying to get this working first as if it wont even setup properly then i’m not sure what to do. I’ve tried everything I can think of so I appreciate anyones help here.


Did you find a solution yet? I am currently struggling with the same problem.

Many thanks,


Did you find a solution?
Or maybe @OptimalCode ?

Thanks a lot

Not yet unfortunately. Every few months i’ve gone back to it and still can’t get it right.

Nothing yet.

Does somebody know anybody who could help?