Problem reading MPU-6050 sensor values with STM32F103RB via I2C
I'm trying to read data from my MPU-6050 (a 3-axis accelerometer and gyroscope from DFRobot) using an STM32F103RB microcontroller, but I'm running into an issue.
The sensor communicates via I2C. It's powered by 3.3V from the STM32, and I've used once 1,2kΩ and other time 10kΩ pull-up resistors on the SDA and SCL lines to 3.3V.
During program debugging, the sensor is left completely stationary. However, in the "Live Expressions" view, the raw values from the sensor are inconsistent. The values for the corresponding accelerometer and gyroscope axes are always the same, which seems strange.
Here are the raw readings from the first few reads:
First Read:
Accel_X_RAW & Gyro_X_RAW: -4017
Accel_Y_RAW & Gyro_Y_RAW: 32
Accel_Z_RAW & Gyro_Z_RAW: 30476
Second Read:
Accel_X_RAW & Gyro_X_RAW: 0
Accel_Y_RAW & Gyro_Y_RAW: 0
Accel_Z_RAW & Gyro_Z_RAW: -1280
Third Read:
Accel_X_RAW & Gyro_X_RAW: -1280
Accel_Y_RAW & Gyro_Y_RAW: 0
Accel_Z_RAW & Gyro_Z_RAW: -1280
Fourth Read:
Accel_X_RAW & Gyro_X_RAW: -2559
Accel_Y_RAW & Gyro_Y_RAW: 0
Accel_Z_RAW & Gyro_Z_RAW: -1280
In my program, I have a flag variable that counts the number of times the loop has executed.
On subsequent reads, only the Accel/Gyro_X_RAW value changes, decrementing by roughly 1279. When it reaches around -3200, it wraps around to approximately +3200. The Y and Z axes remain unchanged at 0 and -1280, respectively.
This pattern continues until the flag variable reaches a value of 264. At that point, the Accel/Gyro_Y_RAW value changes from 0 to 256, while the X-axis continues its previous pattern of change and Z-axis stays still at value -1280.
Has anyone encountered a similar issue or can spot what I might be doing wrong? I'd appreciate any help or suggestions. Thank you!
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "i2c.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
int16_t Accel_X_RAW;
int16_t Accel_Y_RAW;
int16_t Accel_Z_RAW;
int16_t Ax;
int16_t Ay;
int16_t Az;
int16_t Gyro_X_RAW;
int16_t Gyro_Y_RAW;
int16_t Gyro_Z_RAW;
int16_t Gx;
int16_t Gy;
int16_t Gz;
int flag = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
#define MPU6050_ADDR 0xD0
#define SMPLRT_DIV_REG 0x19
#define GYRO_CONFIG_REG 0x1B
#define ACCEL_CONFIG_REG 0x1C
#define ACCEL_XOUT_H_REG 0x3B
#define TEMP_OUT_H_REG 0x41
#define GYRO_XOUT_H_REG 0x43
#define WHO_AM_I_REG 0x75
void MPU6050_Init(void) {
uint8_t check;
uint8_t Data;
HAL_I2C_Mem_Read(&hi2c1, MPU6050_ADDR, 0x75, 1, &check, 1, 1000); // read WHO_AM_I
if (check == 0x68) // 0x68 will be returned by the sensor if everything goes well
{
// power management register 0X6B we should write all 0's to wake the sensor up
Data = 0;
HAL_I2C_Mem_Write(&hi2c1, MPU6050_ADDR, 0x6B, 1, &Data, 1, 1000);
// Set DATA RATE of 1KHz by writing SMPLRT_DIV register
Data = 0x07;
HAL_I2C_Mem_Write(&hi2c1, MPU6050_ADDR, 0x19, 1, &Data, 1, 1000);
// Set accelerometer configuration in ACCEL_CONFIG Register
Data = 0x00; // XA_ST=0,YA_ST=0,ZA_ST=0, FS_SEL=0 -> <strong>±</strong> 2g
HAL_I2C_Mem_Write(&hi2c1, MPU6050_ADDR, ACCEL_CONFIG_REG, 1, &Data, 1,
1000);
Data = 0x00; // XG_ST=0,YG_ST=0,ZG_ST=0, FS_SEL=0 -> <strong>±</strong> 250 ̐/s
HAL_I2C_Mem_Write(&hi2c1, MPU6050_ADDR, GYRO_CONFIG_REG, 1, &Data, 1,
1000);
}
}
void MPU6050_Read_Accel(void) {
uint8_t Rec_Data[6];
// Read 6 BYTES of data starting from ACCEL_XOUT_H (0x3B) register
HAL_I2C_Mem_Read(&hi2c1, MPU6050_ADDR, 0x3B, 1, Rec_Data, 6, 1000);
Accel_X_RAW = (int16_t) (Rec_Data[0] << 8 | Rec_Data[1]);
Accel_Y_RAW = (int16_t) (Rec_Data[2] << 8 | Rec_Data[3]);
Accel_Z_RAW = (int16_t) (Rec_Data[4] << 8 | Rec_Data[5]);
Ax = (float) Accel_X_RAW / 16384.0;
Ay = (float) Accel_Y_RAW / 16384.0;
Az = (float) Accel_Z_RAW / 16384.0;
}
void MPU6050_Read_Gyro(void) {
uint8_t Rec_Data[6];
// Read 6 BYTES of data starting from GYRO_XOUT_H register
HAL_I2C_Mem_Read(&hi2c1, MPU6050_ADDR, 0x43, 1, Rec_Data, 6, 1000);
Gyro_X_RAW = (int16_t) (Rec_Data[0] << 8 | Rec_Data[1]);
Gyro_Y_RAW = (int16_t) (Rec_Data[2] << 8 | Rec_Data[3]);
Gyro_Z_RAW = (int16_t) (Rec_Data[4] << 8 | Rec_Data[5]);
Gx = (float) Gyro_X_RAW / 131.0;
Gy = (float) Gyro_Y_RAW / 131.0;
Gz = (float) Gyro_Z_RAW / 131.0;
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_I2C1_Init();
/* USER CODE BEGIN 2 */
MPU6050_Init();
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1) {
MPU6050_Read_Accel();
MPU6050_Read_Gyro();
flag++;
HAL_Delay(250);
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1) {
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @PAram file: pointer to the source file name
* @PAram line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
