百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
M

MPU9250_asukiaaa

> 编程语言
开源

一个Arduino的库来读取MPU9250的值.

100 stars0 点赞0 次浏览
访问官网GitHub

工具介绍

一个Arduino的库来读取MPU9250的值.

MPU9250_asukiaaa

A library to read values from MPU9250.

Installation

You can install to Arduino IDE with using library manager.

  1. Select [Sketch -> Include Library -> Manage Libraries] to open library manager.
  2. Search MPU9250 in library manager.
  3. Select and install this project.

Connection

For uno, nano and so on.

Arduino MPU9250
3.3 VCC
A4(SDA) SDA
A5(SCL) SCL
GND GND

For other boards, please check i2c pin assign.

Usage

You can see all function on example project.

Accelerometer

c
#include 
MPU9250_asukiaaa mySensor;
float aX, aY, aZ, aSqrt;

void setup() {
  Wire.begin();
  mySensor.setWire(&Wire);
  mySensor.beginAccel();
}

void loop() {
  mySensor.accelUpdate();
  aX = mySensor.accelX();
  aY = mySensor.accelY();
  aZ = mySensor.accelZ();
  aSqrt = mySensor.accelSqrt();
  // Do what you want
}

Gyrometer

c
#include 
MPU9250_asukiaaa mySensor;
float gX, gY, gZ;

void setup() {
  Wire.begin();
  mySensor.setWire(&Wire);
  mySensor.beginGyro();
}

void loop() {
  mySensor.gyroUpdate();
  gX = mySensor.gyroX();
  gY = mySensor.gyroY();
  gZ = mySensor.gyroZ();
  // Do what you want
}

Magnetometer

c
#include 
MPU9250_asukiaaa mySensor;
float mDirection;
uint16_t mX, mY, mZ;

void setup() {
  Wire.begin();
  mySensor.setWire(&Wire);
  mySensor.beginMag();
}

void loop() {
  Serial.begin(115200);
  mySensor.magUpdate();
  mX = mySensor.magX();
  mY = mySensor.magY();
  mZ = mySensor.magZ();
  mDirection = mySensor.magHorizDirection();
  // Do what you want
}

If you cannot get values by calling magUpdate, try calling beginMag and magUpdate again.

c
void loop() {
  auto result = mySensor.magUpdate();
  if (result != 0) {
    mySensor.beginMag();
    result = mySensor.magUpdate();
  }
}

If you get values of sensor like this..

Name Max Min
magX 70 -30
maxY 110 10

You can calcurate offset values like this.

maxXOffset = - (magXMax + magXMin) / 2
           = - (70 - 30) / 2
           = - 40 / 2
           = -20
magYOffset = - (magYMax + magYMin) / 2
           = - (110 + 10) / 2
           = - 120 / 2
           = -60

Then set the offset values like this.

c
void setup() {
  mySensor.magXOffset = -20;
  mySensor.magYOffset = -60;
}

Then you can get like this.

Name Max Min
magX 50 -50
maxY 50 -50

After setting offset value, you can get magHorizDirection as you expected.

Warning: Offset value changes by temperature or some reason. If you want to get high accuracy value, you should recheck the offset value.

Example about auto calibration (calculating offset values) is here.

With customizable Wire

For ESP32 and so on.

c
#define SDA_PIN 21
#define SCL_PIN 22

void setup() {
  Wire.begin(SDA_PIN, SCL_PIN);
  mySensor.setWire(&Wire);
}

License

MIT

References

  • ArduinoでMPU9250(加速度センサ、磁気センサ)を使う方法
  • MPU-9250 | TDK
  • MPU-9250Product SpecificationRevision 1.1
  • MPU-9250Register Map and DescriptionsRevision 1.6
  • HaLakeKit-Library
  • Aliexpress MPU9250
  • AK8963 datasheet
  • jrowberg/i2cdevlib/Arduino/AK8963
  • SparkFun_MPU-9250
  • 「MPU-9250 9軸センサモジュール (メーカー品番:MPU-9250)」を使う
  • 【PSoC】MPU-9250のデータを取得(加速度、ジャイロ編)

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

C++accelerometerarduinogyrometermagnetometer

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言