MPU9250-获取数据

685 阅读1分钟

所使用的设备

本文使用Arduino Uno开发板连接MPU9250传感器,并获取传感器加速度和陀螺仪数据。

  • Arduino Uno 21630841103_.pic.jpg
  • MPU9250 31630841104_.pic.jpg
  • 连线方式
MPU9250Arduino
3.3VVCC
GNDGND
A4SDA
A5SCL

前期准备

  • 下载MPU9250相关库 image.png image.png

编写代码--获取姿态角(Roll、Yaw、Pitch)

#include "MPU9250.h"

MPU9250 mpu;

void setup() {
    Serial.begin(115200);
    Wire.begin();
    delay(2000);

    if (!mpu.setup(0x68)) {  // change to your own address
        while (1) {
            Serial.println("MPU connection failed. Please check your connection with `connection_check` example.");
            delay(5000);
        }
    }
}

void loop() {
    if (mpu.update()) {
        static uint32_t prev_ms = millis();
        if (millis() > prev_ms + 25) {
            print_roll_pitch_yaw();
            prev_ms = millis();
        }
    }
}

void print_roll_pitch_yaw() {
    Serial.print("Yaw, Pitch, Roll: ");
    Serial.print(mpu.getYaw(), 2);
    Serial.print(", ");
    Serial.print(mpu.getPitch(), 2);
    Serial.print(", ");
    Serial.println(mpu.getRoll(), 2);
}

串口输出

image.png