cancel
Showing results for 
Search instead for 
Did you mean: 

Issues with I2C - Gives HAL_ERROR for the VL6180X distance sensor

DMårt
Senior II

Hi!

I'm was writing C-code for the VL6180X because I find ST's own C-code library quite messy and I could not use it because I did not understand it. So I re-wrote a C++ library to STM32 C-code from https://github.com/pololu/vl6180x-arduino

Here is the code. (It's working now!)

https://github.com/DanielMartensson/STM32-Libraries/tree/master/VL6180X

Anyway! When I first tried to connect the I2C with the distance sensor.

#define VL6180X_ADDRESS_DEFAULT  0x29
HAL_StatusTypeDef status = HAL_I2C_IsDeviceReady(&hi2c1, VL6180X_ADDRESS_DEFAULT, 10, 100);

I get status = HAL_ERROR

That's weard because I can use the VL6180X sensor from ST's own .hex file and it works great. But when I trying to compile own C-code, then it won't work.

It's nothing wrong at the Nucleo F401RE board nor the sensor. I have tried to connect a SSD1306 I2C LCD to the I2C as well, but then i get status = HAL_BUSY

This Nucleo Board came with the VL6180X Expension Board X-NUCLEO-6180XA1

Can it be some issues with the firmare?

static void MX_I2C1_Init(void)
{
 
  /* USER CODE BEGIN I2C1_Init 0 */
 
  /* USER CODE END I2C1_Init 0 */
 
  /* USER CODE BEGIN I2C1_Init 1 */
 
  /* USER CODE END I2C1_Init 1 */
  hi2c1.Instance = I2C1;
  hi2c1.Init.ClockSpeed = 100000;
  hi2c1.Init.DutyCycle = I2C_DUTYCYCLE_2;
  hi2c1.Init.OwnAddress1 = 0;
  hi2c1.Init.AddressingMode = I2C_ADDRESSINGMODE_7BIT;
  hi2c1.Init.DualAddressMode = I2C_DUALADDRESS_DISABLE;
  hi2c1.Init.OwnAddress2 = 0;
  hi2c1.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE;
  hi2c1.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE;
  if (HAL_I2C_Init(&hi2c1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN I2C1_Init 2 */
 
  /* USER CODE END I2C1_Init 2 */
 
}

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

HAL_I2C_IsDeviceReady returns HAL_ERROR if the device does not respond.

Slave addresses should be left-aligned. So use 0x52 instead (0x29 << 1).

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

2 REPLIES 2
TDK
Guru

HAL_I2C_IsDeviceReady returns HAL_ERROR if the device does not respond.

Slave addresses should be left-aligned. So use 0x52 instead (0x29 << 1).

If you feel a post has answered your question, please click "Accept as Solution".

Thanks. It's connected now.