Skip to content

SnoopyNomad/MPU6500_STM32_Library

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

18 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MPU6500 STM32 Library

A lightweight and easy-to-integrate C library for interfacing the InvenSense MPU6500 6-axis motion sensor with STM32 microcontrollers using the STM32Cube HAL. This library allows you to initialize the sensor, read accelerometer, gyroscope, and temperature sensor data, and configure sensor settings for real-time motion tracking applications.

Table of Contents

Features

  • Initialize and configure MPU6500 via I²C
  • Read 3-axis accelerometer and gyroscope data
  • Configure full-scale ranges:
    • Accelerometer: ±2/4/8/16 g
    • Gyroscope: ±250/500/1000/2000 °/s
  • Data-ready interrupt generation
  • Temperature sensor reading
  • Compatible with STM32 HAL drivers
  • Low power consumption modes

Prerequisites

  • STM32 microcontroller
  • STM32Cube HAL drivers installed
  • I²C peripheral configured in your project
  • Power supply and wiring to MPU6500 per datasheet
  • Basic understanding of STM32 development

Installation

  1. Clone this repository into your project directory:
git clone https://github.com/SnoopyNomad/MPU6500_STM32_Library.git
  1. Copy the following files into your project:
    • mpu6500.c → Your project's source folder
    • mpu6500.h → Your project's include folder

Configuration

The library is configured with the following default settings:

  • Accelerometer: ±16g full-scale range
  • Gyroscope: ±2000°/s full-scale range
  • Sample Rate: 1kHz
  • Bandwidth: 20Hz
  • Clock Source: Internal oscillator
  • Sleep Mode: Disabled
  • Interrupts: Disabled

Before using the library, configure any pin assignments and settings in your project headers:

#define MPU6500_INT_Pin        GPIO_PIN_0
#define MPU6500_INT_GPIO_Port  GPIOA

Usage

Initialization

  1. Configure I²C in your STM32 project:
// In your main.c or system initialization
I2C_HandleTypeDef hi2c1;

void SystemClock_Config(void);
static void MX_I2C1_Init(void);

int main(void)
{
    HAL_Init();
    SystemClock_Config();
    MX_I2C1_Init();
    // ... rest of your initialization code
}
  1. Include the Library: Add the library header to your project.
#include "mpu6500.h"
  1. Initialize the MPU6500:
HAL_StatusTypeDef status;

// Basic initialization
status = MPU6500_Init();
if(status != HAL_OK){
    Error_Handler();
}

// Optional: Read WHO_AM_I register to verify communication
uint8_t whoami;
status = MPU6500_ReadWhoAmI(&whoami);
if(status != HAL_OK || whoami != 0x70){
    Error_Handler();
}

Reading Sensor Data

int16_t accel_x, accel_y, accel_z;
int16_t gyro_x, gyro_y, gyro_z;
int16_t temperature;
float accel_g[3];    // Acceleration in g
float gyro_dps[3];   // Angular velocity in degrees per second
float temp_c;        // Temperature in Celsius

// Read raw sensor data
status = MPU6500_ReadAccel(&accel_x, &accel_y, &accel_z);
if(status != HAL_OK){
    Error_Handler();
}

status = MPU6500_ReadGyro(&gyro_x, &gyro_y, &gyro_z);
if(status != HAL_OK){
    Error_Handler();
}

status = MPU6500_ReadTemp(&temperature);
if(status != HAL_OK){
    Error_Handler();
}

// Convert raw data to physical units
// For ±16g range: 1g = 2048 LSB
accel_g[0] = (float)accel_x / 2048.0f;
accel_g[1] = (float)accel_y / 2048.0f;
accel_g[2] = (float)accel_z / 2048.0f;

// For ±2000°/s range: 1°/s = 16.4 LSB
gyro_dps[0] = (float)gyro_x / 16.4f;
gyro_dps[1] = (float)gyro_y / 16.4f;
gyro_dps[2] = (float)gyro_z / 16.4f;

// Temperature conversion: T(°C) = (TEMP_OUT / 340) + 36.53
temp_c = ((float)temperature / 340.0f) + 36.53f;

Data Formats

  1. Accelerometer Data

    • Raw data range: -32768 to +32767
    • Scale factors for different ranges:
      • ±2g: 16384 LSB/g
      • ±4g: 8192 LSB/g
      • ±8g: 4096 LSB/g
      • ±16g: 2048 LSB/g
  2. Gyroscope Data

    • Raw data range: -32768 to +32767
    • Scale factors for different ranges:
      • ±250°/s: 131 LSB/°/s
      • ±500°/s: 65.5 LSB/°/s
      • ±1000°/s: 32.8 LSB/°/s
      • ±2000°/s: 16.4 LSB/°/s
  3. Temperature Data

    • Raw data range: -32768 to +32767
    • Conversion formula: T(°C) = (TEMP_OUT / 340) + 36.53

Interrupt Handling

  1. Configure interrupts:
// Enable data ready interrupts
status = MPU6500_EnableDataReadyInterrupts();
if(status != HAL_OK){
    Error_Handler();
}
  1. Set up interrupt handler:
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    if(GPIO_Pin == MPU6500_INT_Pin)
    {
        int16_t accel_x, accel_y, accel_z;
        int16_t gyro_x, gyro_y, gyro_z;
        
        // Read sensor data
        MPU6500_ReadAccel(&accel_x, &accel_y, &accel_z);
        MPU6500_ReadGyro(&gyro_x, &gyro_y, &gyro_z);
        
        // Process data here
        // ...
    }
}

Error Handling

All functions return HAL_StatusTypeDef:

  • HAL_OK: Operation successful
  • HAL_ERROR: Operation failed
  • HAL_BUSY: Device is busy
  • HAL_TIMEOUT: Operation timed out

Common issues:

  • I²C communication failure
  • Invalid device address
  • Sensor unresponsive
  • Misconfiguration

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/AmazingFeature)
  3. Commit your changes (git commit -m 'Add some AmazingFeature')
  4. Push to the branch (git push origin feature/AmazingFeature)
  5. Open a Pull Request

License

MIT – see LICENSE.

Acknowledgments

  • InvenSense TDK (MPU6500)
  • STMicroelectronics (HAL drivers)
  • Community contributors

About

MPU6500 STM32 Library

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages