Mpu6050
MPU6050 full-featured driver library for general-purpose MCU and Linux.
Install / Use
/learn @libdriver/Mpu6050README
English | 简体中文 | 繁體中文 | 日本語 | Deutsch | 한국어
<div align=center> <img src="/doc/image/logo.svg" width="400" height="150"/> </div>LibDriver MPU6050
The MPU6050 is the world’s first integrated 6-axis MotionTracking device that combines a 3-axis gyroscope, 3-axis accelerometer, and a Digital Motion Processor™ (DMP) all in a small 4x4x0.9mm package. With its dedicated I2C sensor bus, it directly accepts inputs from an external 3-axis compass to provide a complete 9-axis MotionFusion™ output. The MPU6050 MotionTracking device, with its 6-axis integration, on-board MotionFusion™, and run-time calibration firmware, enables manufacturers to eliminate the costly and complex selection, qualification, and system level integration of discrete devices, guaranteeing optimal motion performance for consumers. The MPU6050 is also designed to interface with multiple non-inertial digital sensors, such as pressure sensors, on its auxiliary I2C port. The MPU6050 is footprint compatible with the MPU30X0 family.The MPU6050 features three 16-bit analog-to-digital converters (ADCs) for digitizing the gyroscope outputs and three 16-bit ADCs for digitizing the accelerometer outputs. For precision tracking of both fast and slow motions, the parts feature a user-programmable gyroscope full-scale range of ±250, ±500, ±1000, and ±2000°/sec (dps) and a user-programmable accelerometer full-scale range of ±2g, ±4g, ±8g, and ±16g.
LibDriver MPU6050 is a full-featured driver for MPU6050, launched by LibDriver. It provides acceleration reading, angular velocity reading, attitude angle reading, dmp reading, tap detection and additional features. LibDriver is MISRA compliant.
Table of Contents
Instruction
/src includes LibDriver MPU6050 source files.
/interface includes LibDriver MPU6050 IIC platform independent template.
/test includes LibDriver MPU6050 driver test code and this code can test the chip necessary function simply.
/example includes LibDriver MPU6050 sample code.
/doc includes LibDriver MPU6050 offline document.
/datasheet includes MPU6050 datasheet.
/project includes the common Linux and MCU development board sample code. All projects use the shell script to debug the driver and the detail instruction can be found in each project's README.md.
/misra includes the LibDriver MISRA code scanning results.
Install
Reference /interface IIC platform independent template and finish your platform IIC driver.
Add the /src directory, the interface driver for your platform, and your own drivers to your project, if you want to use the default example drivers, add the /example directory to your project.
Usage
You can refer to the examples in the /example directory to complete your own driver. If you want to use the default programming examples, here's how to use them.
example basic
#include "driver_mpu6050_basic.h"
uint8_t res;
uint32_t i;
uint32_t times;
float g[3];
float dps[3];
float degrees;
mpu6050_address_t addr;
/* init */
addr = MPU6050_ADDRESS_AD0_LOW;
res = mpu6050_basic_init(addr);
if (res != 0)
{
return 1;
}
...
/* read all */
times = 3;
for (i = 0; i < times; i++)
{
/* read */
if (mpu6050_basic_read(g, dps) != 0)
{
(void)mpu6050_basic_deinit();
return 1;
}
...
if (mpu6050_basic_read_temperature(°rees) != 0)
{
(void)mpu6050_basic_deinit();
return 1;
}
...
/* output */
mpu6050_interface_debug_print("mpu6050: %d/%d.\n", i + 1, times);
mpu6050_interface_debug_print("mpu6050: acc x is %0.2fg.\n", g[0]);
mpu6050_interface_debug_print("mpu6050: acc y is %0.2fg.\n", g[1]);
mpu6050_interface_debug_print("mpu6050: acc z is %0.2fg.\n", g[2]);
mpu6050_interface_debug_print("mpu6050: gyro x is %0.2fdps.\n", dps[0]);
mpu6050_interface_debug_print("mpu6050: gyro y is %0.2fdps.\n", dps[1]);
mpu6050_interface_debug_print("mpu6050: gyro z is %0.2fdps.\n", dps[2]);
mpu6050_interface_debug_print("mpu6050: temperature %0.2fC.\n", degrees);
...
/* delay 1000 ms */
mpu6050_interface_delay_ms(1000);
...
}
...
/* deinit */
(void)mpu6050_basic_deinit();
return 0;
example fifo
#include "driver_mpu6050_fifo.h"
uint32_t i;
uint32_t times;
uint16_t len;
uint8_t (*g_gpio_irq)(void) = NULL;
static int16_t gs_accel_raw[128][3];
static float gs_accel_g[128][3];
static int16_t gs_gyro_raw[128][3];
static float gs_gyro_dps[128][3];
mpu6050_address_t addr;
/* gpio init */
if (gpio_interrupt_init() != 0)
{
return 1;
}
g_gpio_irq = mpu6050_fifo_irq_handler;
/* init */
addr = MPU6050_ADDRESS_AD0_LOW;
if (mpu6050_fifo_init(addr) != 0)
{
g_gpio_irq = NULL;
(void)gpio_interrupt_deinit();
return 1;
}
/* delay 100 ms */
mpu6050_interface_delay_ms(100);
...
times = 3;
for (i = 0; i < times; i++)
{
len = 128;
/* read */
if (mpu6050_fifo_read(gs_accel_raw, gs_accel_g,
gs_gyro_raw, gs_gyro_dps, &len) != 0)
{
(void)mpu6050_fifo_deinit();
g_gpio_irq = NULL;
(void)gpio_interrupt_deinit();
return 1;
}
...
/* output */
mpu6050_interface_debug_print("mpu6050: %d/%d.\n", i + 1, times);
mpu6050_interface_debug_print("mpu6050: fifo %d.\n", len);
mpu6050_interface_debug_print("mpu6050: acc x[0] is %0.2fg.\n", gs_accel_g[0][0]);
mpu6050_interface_debug_print("mpu6050: acc y[0] is %0.2fg.\n", gs_accel_g[0][1]);
mpu6050_interface_debug_print("mpu6050: acc z[0] is %0.2fg.\n", gs_accel_g[0][2]);
mpu6050_interface_debug_print("mpu6050: gyro x[0] is %0.2fdps.\n", gs_gyro_dps[0][0]);
mpu6050_interface_debug_print("mpu6050: gyro y[0] is %0.2fdps.\n", gs_gyro_dps[0][1]);
mpu6050_interface_debug_print("mpu6050: gyro z[0] is %0.2fdps.\n", gs_gyro_dps[0][2]);
...
/* delay 100 ms */
mpu6050_interface_delay_ms(100);
...
}
...
/* deinit */
(void)mpu6050_fifo_deinit();
g_gpio_irq = NULL;
(void)gpio_interrupt_deinit();
return 0;
example dmp
#include "driver_mpu6050_dmp.h"
uint32_t i;
uint32_t times;
uint32_t cnt;
uint16_t len;
uint8_t (*g_gpio_irq)(void) = NULL;
static int16_t gs_accel_raw[128][3];
static float gs_accel_g[128][3];
static int16_t gs_gyro_raw[128][3];
static float gs_gyro_dps[128][3];
static int32_t gs_quat[128][4];
static float gs_pitch[128];
static float gs_roll[128];
static float gs_yaw[128];
mpu6050_address_t addr;
static void a_receive_callback(uint8_t type)
{
switch (type)
{
case MPU6050_INTERRUPT_MOTION :
{
mpu6050_interface_debug_print("mpu6050: irq motion.\n");
break;
}
case MPU6050_INTERRUPT_FIFO_OVERFLOW :
{
mpu6050_interface_debug_print("mpu6050: irq fifo overflow.\n");
break;
}
case MPU6050_INTERRUPT_I2C_MAST :
{
mpu6050_interface_debug_print("mpu6050: irq i2c master.\n");
break;
}
case MPU6050_INTERRUPT_DMP :
{
mpu6050_interface_debug_print("mpu6050: irq dmp\n");
break;
}
case MPU6050_INTERRUPT_DATA_READY :
{
mpu6050_interface_debug_print("mpu6050: irq data ready\n");
break;
}
default :
{
mpu6050_interface_debug_print("mpu6050: irq unknown code.\n");
break;
}
}
}
static void a_dmp_tap_callback(uint8_t count, uint8_t direction)
{
switch (direction)
{
case MPU6050_DMP_TAP_X_UP :
{
mpu6050_interface_debug_print("mpu6050: tap irq x up with %d.\n", count);
break;
}
case MPU6050_DMP_TAP_X_DOWN :
{
mpu6050_interface_debug_print("mpu6050: tap irq x down with %d.\n", count);
break;
}
case MPU6050_DMP_TAP_Y_UP :
{
mpu6050_interface_debug_print("mpu6050: tap irq y up with %d.\n", count);
break;
}
case MPU6050_DMP_TAP_Y_DOWN :
{
mpu6050_interface_debug_print("mpu6050: tap irq y down with %d.\n", count);
break;
}
case MPU6050_DMP_TAP_Z_UP :
{
mpu6050_interface_debug_print("mpu6050: tap irq z up with %d.\n", count);
break;
}
case MPU6050_DMP_TAP_Z_DOWN :
{
mpu6050_interface_debug_print("mpu6050: tap irq z down with %d.\n", count);
break;
}
default :
{
mpu6050_interface_debug_print("mpu6050: tap irq unknown code.\n");
break;
}
}
}
static void a_dmp_orient_callback(uint8_t orientation)
{
switch (orientation)
{
case MPU6050_DMP_ORIENT_PORTRAIT :
{
mpu6050_interface_debug_print("mpu6050: orient irq portrait.\n");
break;
}
case MPU6050_DMP_ORIENT_LANDSCAPE :
{
mpu6050_interface_debug_print("mpu6050:
Related Skills
node-connect
351.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
110.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
qqbot-media
351.8kQQBot 富媒体收发能力。使用 <qqmedia> 标签,系统根据文件扩展名自动识别类型(图片/语音/视频/文件)。
