I m working on a "self-balancing robot" with STM32F303RE.I m using STCubeMX to generate the MCU configuration code and then I added the main() to get the values from MPU6050 from the following code :

#include "main.h"
#include "stm32f3xx_hal.h"

/* Private variables-----*/
I2C_HandleTypeDef hi2c1;
uint8_t i;
uint8_t i2cBuff[8];
uint16_t ax,ay,az;
float Xaccel,Yaccel,Zaccel;
#define mpu6050Address 0xD0

int main(void)
   /* Configure the system clock */
   /* USER CODE BEGIN 2 */
   for(uint8_t i=0 ; i <255;i++)
       if(HAL_I2C_IsDeviceReady(&hi2c1,i,1 ,10) == HAL_OK )
          HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);

    /* USER CODE END 2 */

    /* Infinite loop */
    while (1)
       i2cBuff[0]= 0x3B;
       HAL_I2C_Master_Transmit(&hi2c1, mpu6050Address, i2cBuff, 1, 10);
       i2cBuff[1] = 0x00;
       HAL_I2C_Master_Receive(&hi2c1, mpu6050Address, &i2cBuff[1], 6, 10);

       ax = -(i2cBuff[1]<<8 | i2cBuff[2]);
       ay = -(i2cBuff[3]<<8 | i2cBuff[4]);
       az = -(i2cBuff[5]<<8 | i2cBuff[6]);

       Xaccel = ax/8192.0;
       Yaccel = ay/8192.0;
       Zaccel = az/8192.0;

      /* USER CODE BEGIN 3 */

      /* USER CODE END 3 */


But, when I add ax,ay,az or Xaccel,Yaccel,Zaccel variables to watch it is always showing 0.What can be the problem?

  • \$\begingroup\$ Possible duplicate of MPU6050 Interfacing with STM32 \$\endgroup\$ Apr 28, 2018 at 18:40
  • \$\begingroup\$ You already asked a question about this, do not re-post, instead edit your code into the original question. As for the problem check the library reference to figure out how to tell if the I2C transfer failed. Also explain how you determined the received values. \$\endgroup\$ Apr 28, 2018 at 18:40
  • \$\begingroup\$ Sorry, but I deleted my previous question after your pointed out that it was too broad and made it bit specific with the code, you want to know how I m reading the data from the MPU or the method that I m using to calculate the data that the MCU is receiving \$\endgroup\$ Apr 28, 2018 at 18:44
  • \$\begingroup\$ Don't make false claims, the fact is you deleted your post only just now, after I placed the duplicate close vote and pointed out that you needed to add this to it - which is to say you did the wrong thing after what you needed to do was explained, not before. Deleting posts is extremely discouraged - don't do that again. \$\endgroup\$ Apr 28, 2018 at 18:49
  • \$\begingroup\$ Alright man, look I have a problem here, please help me if you can \$\endgroup\$ Apr 28, 2018 at 18:52

1 Answer 1


I'm not sure whether this is your problem, but the HAL functions you're using are not the best for reading a register (sub-address) from a device. HAL_I2C_Master_Transmit() writes the specified data to the device and terminates the transmission with an I2C Stop bit. And then HAL_I2C_Master_Receive() starts a new exchange to read data from the device.

But the recommended I2C read sequence on page 36 of the datasheet is different from what your code does. The recommendation is to write the register address value and then send a Repeat Start bit (not a Stop) before reading the data bytes. You can't write, repeat-start, and read with HAL_I2C_Master_Transmit() or HAL_I2C_Master_Receive(). But you can do this with HAL_I2C_Mem_Read().

Try replacing your calls to HAL_I2C_Master_Transmit() and HAL_I2C_Master_Receive() with this single call:

HAL_I2C_Mem_Read(&hi2c1, mpu6050Address, 0x3B, I2C_MEMADD_SIZE_8BIT, &i2cBuff[1], 6, 10);

I'm not sure whether this will fix your problem because the way you're doing it may work for many I2C devices that remember the previous register sub-address from one exchange to the next. If you can read the device's WHO_AM_I register successfully then there may be another problem. For example, perhaps you need to enable or configure the accelerometer before you can read the accelerometer values.

  • \$\begingroup\$ It did work with HAL_I2C_Master_Transmit() and receive, the problem was with the memory address. \$\endgroup\$ May 11, 2018 at 18:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.