MPU9250 Arduino Simple Code: Difference between revisions

From wikiluntti
 
(21 intermediate revisions by the same user not shown)
Line 1: Line 1:
== Introduction ==
== Introduction ==


MPU9250 Simple code  
MPU9250 Simple code
 
NOTE: MPU9250 is 3.3V device, so it might break if connected wrong. Double check that Arduino's +5 V is on VCC. Doublecheck that SCL and SCA are on the Analog inputs, and do not push current into those pins. See more at https://electronics.stackexchange.com/questions/492792/does-the-mpu9250-require-a-5-to-3-3-v-level-shifter-when-used-with-arduino-uno
 
NOTE: delay() do not work with MPU9250.h used below!
 
Some MPU9250 libaries for Arduino
* MPU9250 by hideakitai. Discontinued. Not more supported. https://github.com/hideakitai/MPU9250
* Bolder Flight Systems MPU9250 by Brian Taylor
* FaBo 202 9Axis MPU9250  by FaBo
* FastIMU by LiquidCGS. https://github.com/LiquidCGS/FastIMU
* I2C-Sensor-Lib iLib by Ingmar Splitt
* ICM20948_WE by Wolfgang
* MPU9250_asukiaaa by Asuki Kono
 
Use i2c_scanner (from Arduino IDE) to find out the address.
 
 
The IMUIndentiefier by FastIMU describes:
<pre>
=========== IMU Identifier ===========
IMU Found: MPU6500 On address: 0x68
This IMU is capable of the following axis: 3A,3G
This IMU is supported by the FastIMU library.
======================================
</pre>
thought the board was supposed to be 9250.


== The simple code ==  
== The simple code ==  


This is very similar to that given in https://github.com/hideakitai/MPU9250
This is very similar to that given in https://github.com/hideakitai/MPU9250
Connect
* MPU0250 SDA -> A4
* MPU0250 SCL -> A5
Because of the 5V and 3.3V mismatch. The analog input works, though.


<syntaxhighlight lang="C">
<syntaxhighlight lang="C">
Line 45: Line 76:
</syntaxhighlight>
</syntaxhighlight>


== Code 2 ==


Add temperature
Error messages
* ''MPU connection failed. Please check your connection with `connection_check` example'' from the Library. Add there following lines (see https://forum.arduino.cc/t/problem-to-connect-to-my-mpu9250/700295/4)
** If that gives 0x70, the IMU is MPU6500 instead of MPU9250.
<syntaxhighlight lang="C">
        byte ca = readByte(addrs[i], WHO_AM_I_MPU9250);
 
        Serial.print( "The WHO_AM_I byte is: 0x");  // <- added
        Serial.println( (int) ca, HEX);        // <- added
 
        if (ca == MPU9250_WHOAMI_DEFAULT_VALUE) {
</syntaxhighlight>
 
== Code 2 ==
 
Simple, given by Fabo
 
<syntaxhighlight lang="C">
/**
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;
 
  fabo_9axis.readAccelXYZ(&ax,&ay,&az);
 
  Serial.print("ax: ");
  Serial.print(ax);
  Serial.print(" ay: ");
  Serial.print(ay);
  Serial.print(" az: ");
  Serial.println(az);
 
  delay(1000);
}
</syntaxhighlight>
 
 
== Code 2b ==
 
Add temperature and other data.


<syntaxhighlight lang="C">
<syntaxhighlight lang="C">
Line 117: Line 211:
</syntaxhighlight>
</syntaxhighlight>


== Code 3 ==
== Code 3: Implement delay() ==
 
<syntaxhighlight lang="C">
#include "MPU9250.h"
 
MPU9250 mpu;
 
const int buzzer = 9; //buzzer to arduino pin 9
 
void setup(){
  pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
  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);
      }
  }
}
 
int fi = 100;
 
void loop(){
 
    if (mpu.update()) {
        static uint32_t prev_ms = millis();
        if (millis() > prev_ms + 25) {
            print_roll_pitch_yaw();
            prev_ms = millis();
        }
    }
 
    tone(buzzer, fi); // Send sound signal...
    static uint32_t prev_ms_sound = millis();
    if (millis() > prev_ms_sound + 100) {
      noTone(buzzer);
      fi += 100;   
      prev_ms_sound = millis();
      if (fi > 10000){
        fi = 100;
      }
    }
 
}
 
</syntaxhighlight>
 
== Code 4a ==
 
== Code MPU6050 The smaller ==
 
Code from https://www.arduinolearning.com/code/arduino-mpu6500-6-axis-motion-tracking-device.php
 
<syntaxhighlight lang="C">
#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"
 
MPU6050 accelgyro;
 
int16_t ax, ay, az;
int16_t gx, gy, gz;
 
#define LED_PIN 13
bool blinkState = false;
 
void setup() {
  Wire.begin();
  Serial.begin(38400);
 
  Serial.println("Initializing I2C devices...");
  accelgyro.initialize();
 
  Serial.println("Testing device connections...");
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
 
  pinMode(LED_PIN, OUTPUT);
}
 
void loop() {
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
 
  Serial.print(ax); Serial.print("\t");
  Serial.print(ay); Serial.print("\t");
  Serial.println(az); Serial.print("\t");
 
  // blink LED to indicate activity
  blinkState = !blinkState;
  digitalWrite(LED_PIN, blinkState);
}
</syntaxhighlight>

Latest revision as of 16:21, 17 January 2025

Introduction

MPU9250 Simple code

NOTE: MPU9250 is 3.3V device, so it might break if connected wrong. Double check that Arduino's +5 V is on VCC. Doublecheck that SCL and SCA are on the Analog inputs, and do not push current into those pins. See more at https://electronics.stackexchange.com/questions/492792/does-the-mpu9250-require-a-5-to-3-3-v-level-shifter-when-used-with-arduino-uno

NOTE: delay() do not work with MPU9250.h used below!

Some MPU9250 libaries for Arduino

Use i2c_scanner (from Arduino IDE) to find out the address.


The IMUIndentiefier by FastIMU describes:

=========== IMU Identifier ===========
IMU Found: MPU6500 On address: 0x68
This IMU is capable of the following axis: 3A,3G
This IMU is supported by the FastIMU library.
======================================

thought the board was supposed to be 9250.

The simple code

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

Connect

  • MPU0250 SDA -> A4
  • MPU0250 SCL -> A5

Because of the 5V and 3.3V mismatch. The analog input works, though.

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


Error messages

        byte ca = readByte(addrs[i], WHO_AM_I_MPU9250);

        Serial.print( "The WHO_AM_I byte is: 0x");   // <- added
        Serial.println( (int) ca, HEX);         // <- added

        if (ca == MPU9250_WHOAMI_DEFAULT_VALUE) {

Code 2

Simple, given by Fabo

/**
 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;

  fabo_9axis.readAccelXYZ(&ax,&ay,&az);

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

  delay(1000);
}


Code 2b

Add temperature and other data.

/**
 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: Implement delay()

#include "MPU9250.h"

MPU9250 mpu;

const int buzzer = 9; //buzzer to arduino pin 9

void setup(){
  pinMode(buzzer, OUTPUT); // Set buzzer - pin 9 as an output
  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);
      }
  } 
}

int fi = 100;

void loop(){

    if (mpu.update()) {
        static uint32_t prev_ms = millis();
        if (millis() > prev_ms + 25) {
            print_roll_pitch_yaw();
            prev_ms = millis();
        }
    }
  
    tone(buzzer, fi); // Send sound signal...
    static uint32_t prev_ms_sound = millis();
    if (millis() > prev_ms_sound + 100) {
      noTone(buzzer);
      fi += 100;    
      prev_ms_sound = millis();
      if (fi > 10000){
        fi = 100;
      }
    }

}

Code 4a

Code MPU6050 The smaller

Code from https://www.arduinolearning.com/code/arduino-mpu6500-6-axis-motion-tracking-device.php

#include "Wire.h"
#include "I2Cdev.h"
#include "MPU6050.h"

MPU6050 accelgyro;

int16_t ax, ay, az;
int16_t gx, gy, gz;

#define LED_PIN 13
bool blinkState = false;

void setup() {
  Wire.begin();
  Serial.begin(38400);

  Serial.println("Initializing I2C devices...");
  accelgyro.initialize();

  Serial.println("Testing device connections...");
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");

  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

  Serial.print(ax); Serial.print("\t");
  Serial.print(ay); Serial.print("\t");
  Serial.println(az); Serial.print("\t");

  // blink LED to indicate activity
  blinkState = !blinkState;
  digitalWrite(LED_PIN, blinkState);
}