cancel
Showing results for 
Search instead for 
Did you mean: 

How to use VL53L0X with I2C?

zahra_y10
Associate

Hello!

I recently started working with STM32 MCUs and I want to set up a Time Of Flight sensor using STM32F072C8. I have set up other sensors with my board but not with I2C. I'm familiar with the I2C protocol but after reading the datasheet I don't know where to begin.

Thanks in advance.

1 ACCEPTED SOLUTION

Accepted Solutions
I can only help so far, if you have a scope you may be able to see a timing issue on the IIC bus
Anyhow, please check your pin speed is set to very fast.
This is one main problem for intermittent IIC with STM32

View solution in original post

3 REPLIES 3
T J
Lead

Just firstly check the two resistors are in place, 10K pullups on SDA and SCL.

then make sure the IO pin is set to maximum speed.

set the IIC to 100KHz to start, once its working you can try the 400KHz mode.

Do you know the address of the chip ?

dont forget the address byte contails only 7 address bits and the 8th bit is for Read or Write mode.

did you check the examples ?

zahra_y10
Associate

Thank you for your answer :D

Yes the pullups are in place and the I2C frequency is set to 100KHz. Also based on the datasheet the chip address is 0x52.

After looking at the notes for VL53L0X API and its application notes I was able to write a program for this sensor but I'm currently stuck at sensor initialization (VL53L0X_DataInit function in the API). By using the debugger I found out that this function returns an error at the very end of its execution. Do you have any idea what might have cause this error?

UPDATE!!!: So I fixed the error by changing the I2C frequency from 100KHz to 400KHz. Weird! I still don't know why it wasn't working properly with 100 KHz frequency.

You can see how I initialized my sensor in the code below:

VL53L0X_Dev_t device;

 device.I2cHandle = &hi2c1;

 device.I2cDevAddr = 0x52;

 device.Present = 0;

 device.Id = 0;

VL53L0X_Error Status = VL53L0X_ERROR_NONE;

 uint32_t refSpadCount   = 0;

 uint8_t isApertureSpads = 0;

 uint8_t VhvSettings   = 0;

 uint8_t PhaseCal     = 0;

 if (Status == VL53L0X_ERROR_NONE)

 {

    Status = VL53L0X_DataInit(&device); // Here's where I get my error

  }

  if (Status == VL53L0X_ERROR_NONE)

  {

    Status=VL53L0X_StaticInit(&device);

  }

  if (Status == VL53L0X_ERROR_NONE)

  {

    Status = VL53L0X_PerformRefSpadManagement(&device, &refSpadCount, &isApertureSpads);

  }

  if (Status == VL53L0X_ERROR_NONE)

  {

    Status = VL53L0X_PerformRefCalibration(&device, &VhvSettings, &PhaseCal);

  }

  if (Status == VL53L0X_ERROR_NONE)

  {

    Status = VL53L0X_SetReferenceSpads(&device, refSpadCount, isApertureSpads);

  }

  if (Status == VL53L0X_ERROR_NONE)

  {

    Status = VL53L0X_SetRefCalibration(&device, VhvSettings, PhaseCal);

  }

  if (Status == VL53L0X_ERROR_NONE)

  {

    Status = VL53L0X_SetDeviceMode(&device, VL53L0X_DEVICEMODE_CONTINUOUS_RANGING);

  }

  if (Status == VL53L0X_ERROR_NONE)

  {

    Status = VL53L0X_SetLimitCheckValue(&device, VL53L0X_CHECKENABLE_SIGNAL_RATE_FINAL_RANGE, (FixPoint1616_t)(0.25*65536));

  }

  if (Status == VL53L0X_ERROR_NONE)

  {

  // High Speed

    Status = VL53L0X_SetLimitCheckValue(&device, VL53L0X_CHECKENABLE_SIGMA_FINAL_RANGE, (FixPoint1616_t)(32*65536));

  }

  if (Status == VL53L0X_ERROR_NONE)

  {

    Status = VL53L0X_SetMeasurementTimingBudgetMicroSeconds(&device, 20000);

  }

  if (Status == VL53L0X_ERROR_NONE)

  {

    Status = VL53L0X_SetVcselPulsePeriod(&device, VL53L0X_VCSEL_PERIOD_PRE_RANGE, 14);

  }

  if (Status == VL53L0X_ERROR_NONE)

  {

    Status = VL53L0X_SetVcselPulsePeriod(&device, VL53L0X_VCSEL_PERIOD_FINAL_RANGE, 10);

  }

  if (Status == VL53L0X_ERROR_NONE)

  {

    Status = VL53L0X_StartMeasurement(&device);

  }

I can only help so far, if you have a scope you may be able to see a timing issue on the IIC bus
Anyhow, please check your pin speed is set to very fast.
This is one main problem for intermittent IIC with STM32