A library to read value of MPU9250.
You can install to Arduino IDE with using library manager.
- Select [Sketch -> Include Library -> Manage Libraries] to open library manager.
- Search MPU9250 in library manager.
- Select and install this project.
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.
You can see all function on example project.
#include <MPU9250_asukiaaa.h>
MPU9250 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
}
#include <MPU9250_asukiaaa.h>
MPU9250 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
}
#include <MPU9250_asukiaaa.h>
MPU9250 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 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.
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.
For ESP8266, ESP32 and so on.
void setup() {
Wire.begin(26, 25); //sda, scl
mySensor.setWire(&Wire);
}
MIT
- 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のデータを取得(加速度、ジャイロ編)