MPU9250 Arduino Simple Code: Difference between revisions

From wikiluntti
Line 51: Line 51:
<syntaxhighlight lang="C">
<syntaxhighlight lang="C">
/**
/**
  @file read9axis.ino
  Original: @file read9axis.ino  
  @brief This is an Example for the FaBo 9Axis I2C Brick.
  @brief This is an Example for the FaBo 9Axis I2C Brick.  
 
http://fabo.io/202.html
  http://fabo.io/202.html
Released under APACHE LICENSE, VERSION 2.0
 
  Released under APACHE LICENSE, VERSION 2.0
 
  http://www.apache.org/licenses/
 
  @author FaBo<info@fabo.io>
  @author FaBo<info@fabo.io>
*/
*/
Line 121: Line 116:
}
}
</syntaxhighlight>
</syntaxhighlight>


== Code 3 ==
== Code 3 ==

Revision as of 15:49, 18 November 2024

Introduction

MPU9250 Simple code

The simple code

This is very similar to that given in https://github.com/hideakitai/MPU9250

#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);
}

Code 2

Add temperature

/**
 Original: @file read9axis.ino 
 @brief This is an Example for the FaBo 9Axis I2C Brick. 
 http://fabo.io/202.html
 Released under APACHE LICENSE, VERSION 2.0
 @author FaBo<info@fabo.io>
*/

#include <Wire.h>
#include <FaBo9Axis_MPU9250.h>

FaBo9Axis fabo_9axis;

void setup() {
  Serial.begin(115200);
  Serial.println("RESET");
  Serial.println();

  Serial.println("configuring device.");

  if (fabo_9axis.begin()) {
    Serial.println("configured FaBo 9Axis I2C Brick");
  } else {
    Serial.println("device error");
    while(1);
  }
}

void loop() {
  float ax,ay,az;
  float gx,gy,gz;
  float mx,my,mz;
  float temp;

  fabo_9axis.readAccelXYZ(&ax,&ay,&az);
  fabo_9axis.readGyroXYZ(&gx,&gy,&gz);
  fabo_9axis.readMagnetXYZ(&mx,&my,&mz);
  fabo_9axis.readTemperature(&temp);

  Serial.print("ax: ");
  Serial.print(ax);
  Serial.print(" ay: ");
  Serial.print(ay);
  Serial.print(" az: ");
  Serial.println(az);

  Serial.print("gx: ");
  Serial.print(gx);
  Serial.print(" gy: ");
  Serial.print(gy);
  Serial.print(" gz: ");
  Serial.println(gz);

  Serial.print("mx: ");
  Serial.print(mx);
  Serial.print(" my: ");
  Serial.print(my);
  Serial.print(" mz: ");
  Serial.println(mz);

  Serial.print("temp: ");
  Serial.println(temp);

  delay(1000);
}

Code 3