cancel
Showing results for 
Search instead for 
Did you mean: 

MPU 6050 I2C unable to read data

dav davi
Associate
Posted on February 23, 2018 at 09:49

Hello,

I am trying to use the MPU6050 with a STM32F108. I configured the I2C and UART using cubeMx. I also have put pull up resistors on I2C sda scl. I use Keil.

As far as I understood, I have several options to read the measures from MPU6050.

I have defined

#define NB_TAB 6

#define I2C_TIMEOUT 10

#define REG_ADDRESS 0x59

#define MPU_ADDRESS (0x68<<1)

and the main code is

  /* USER CODE BEGIN 2 */

    uint8_t reponse[NB_TAB];

    uint8_t reg = REG_ADDRESS;

    HAL_UART_Transmit(&huart1, (uint8_t*)'Start\r\n', 7, 10);

    

    HAL_StatusTypeDef status;

  /* USER CODE END 2 */

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {            

        status = HAL_I2C_IsDeviceReady(&hi2c1, MPU_ADDRESS, 10, I2C_TIMEOUT);

        

        if(status != HAL_OK){

            HAL_UART_Transmit(&huart1, (uint8_t*)'ready_error\r\n', 13, 100);

        }

      

        status = HAL_I2C_Master_Transmit(&hi2c1, MPU_ADDRESS, &reg, 2, I2C_TIMEOUT);

        

        if(status != HAL_OK){

            HAL_UART_Transmit(&huart1, (uint8_t*)'trans_error\r\n', 13, 100);

        }

        

        status = HAL_I2C_Master_Receive(&hi2c1, MPU_ADDRESS, reponse, NB_TAB, I2C_TIMEOUT);

        

        if(status != HAL_OK){

            HAL_UART_Transmit(&huart1, (uint8_t*)'recei_error\r\n', 13, 100);

        }

        

        //while(HAL_I2C_Mem_Read(&hi2c1, MPU_ADDRESS, REG_ADDRESS, 1, &val, 2, I2C_TIMEOUT) != HAL_OK);

        

        HAL_UART_Transmit(&huart1, reponse, NB_TAB, 100);

    

        /* USER CODE END WHILE */

The I2C is working well with arduino to transmit data but I can't read it.

The result printed on Termite 3.3 is [00][00] ...

I think that the issue comes from the 'response ' array because of of wrong type cast.

I tried many things but still the same result.

If anyone can help me

Thanks in advance.

1 REPLY 1
dav davi
Associate
Posted on February 23, 2018 at 09:58

I forget some explanation

/* USER CODE BEGIN WHILE */

  while (1)

  {            

      //check if the device is working

        status = HAL_I2C_IsDeviceReady(&hi2c1, MPU_ADDRESS, 10, I2C_TIMEOUT);

        

        if(status != HAL_OK){

            HAL_UART_Transmit(&huart1, (uint8_t*)'ready_error\r\n', 13, 100);

        }

      

      //transmit to the MPU address the address of the register containing the data '&reg'.

        status = HAL_I2C_Master_Transmit(&hi2c1, MPU_ADDRESS, &reg, 2, I2C_TIMEOUT);

        

        if(status != HAL_OK){

            HAL_UART_Transmit(&huart1, (uint8_t*)'trans_error\r\n', 13, 100);

        }

        

      //write the value of the register in reponse.

        status = HAL_I2C_Master_Receive(&hi2c1, MPU_ADDRESS, reponse, NB_TAB, I2C_TIMEOUT);

        

        if(status != HAL_OK){

            HAL_UART_Transmit(&huart1, (uint8_t*)'recei_error\r\n', 13, 100);

        }

        

        //while(HAL_I2C_Mem_Read(&hi2c1, MPU_ADDRESS, REG_ADDRESS, 1, &val, 2, I2C_TIMEOUT) != HAL_OK);

        

      //transmit data with UART

        HAL_UART_Transmit(&huart1, reponse, NB_TAB, 100);

    

        /* USER CODE END WHILE */