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.
- Features
- Prerequisites
- Installation
- Configuration
- Usage
- Error Handling
- Contributing
- License
- Acknowledgments
- 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
- 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
- Clone this repository into your project directory:
git clone https://github.com/SnoopyNomad/MPU6500_STM32_Library.git
- Copy the following files into your project:
mpu6500.c
→ Your project's source foldermpu6500.h
→ Your project's include folder
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
- 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
}
- Include the Library: Add the library header to your project.
#include "mpu6500.h"
- 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();
}
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;
-
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
-
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
-
Temperature Data
- Raw data range: -32768 to +32767
- Conversion formula: T(°C) = (TEMP_OUT / 340) + 36.53
- Configure interrupts:
// Enable data ready interrupts
status = MPU6500_EnableDataReadyInterrupts();
if(status != HAL_OK){
Error_Handler();
}
- 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
// ...
}
}
All functions return HAL_StatusTypeDef
:
HAL_OK
: Operation successfulHAL_ERROR
: Operation failedHAL_BUSY
: Device is busyHAL_TIMEOUT
: Operation timed out
Common issues:
- I²C communication failure
- Invalid device address
- Sensor unresponsive
- Misconfiguration
Contributions are welcome! Please follow these steps:
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
MIT – see LICENSE.
- InvenSense TDK (MPU6500)
- STMicroelectronics (HAL drivers)
- Community contributors