MPU9250 Arduino Simple Code: Difference between revisions
From wikiluntti
(→Code 2) |
(→Code 3) |
||
Line 167: | Line 167: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
== Code | == Code 3d == | ||
== Code MPU6500 The smaller == | |||
<syntaxhighlight lang="C"> | |||
/*************************************************************************** | |||
* Example sketch for the MPU6500_WE library | |||
* For further information visit: | |||
* https://wolles-elektronikkiste.de/en/mpu9250-9-axis-sensor-module-part-1 (English) | |||
***************************************************************************/ | |||
#include <MPU6500_WE.h> | |||
#include <Wire.h> | |||
#define MPU6500_ADDR 0x68 | |||
MPU6500_WE myMPU6500 = MPU6500_WE(MPU6500_ADDR); | |||
void setup() { | |||
Serial.begin(115200); | |||
Wire.begin(); | |||
if(!myMPU6500.init()){ | |||
Serial.println("MPU6500 does not respond"); | |||
} | |||
else{ | |||
Serial.println("MPU6500 is connected"); | |||
} | |||
Serial.println("Position you MPU6500 flat and don't move it - calibrating..."); | |||
delay(1000); | |||
myMPU6500.autoOffsets(); | |||
Serial.println("Done!"); | |||
myMPU6500.enableGyrDLPF(); | |||
//myMPU6500.disableGyrDLPF(MPU6500_BW_WO_DLPF_8800); // bandwdith without DLPF | |||
/* Sample rate divider divides the output rate of the gyroscope and accelerometer. | |||
* Sample rate = Internal sample rate / (1 + divider) | |||
* It can only be applied if the corresponding DLPF is enabled and 0<DLPF<7! | |||
* Divider is a number 0...255 | |||
*/ | |||
myMPU6500.setSampleRateDivider(5); | |||
myMPU6500.setGyrRange(MPU6500_GYRO_RANGE_250); | |||
myMPU6500.setAccRange(MPU6500_ACC_RANGE_2G); | |||
myMPU6500.enableAccDLPF(true); | |||
delay(200); | |||
} | |||
void loop() { | |||
xyzFloat gValue = myMPU6500.getGValues(); | |||
float temp = myMPU6500.getTemperature(); | |||
Serial.println("Acceleration in g (x,y,z):"); | |||
Serial.print(gValue.x); | |||
Serial.print(" "); | |||
Serial.print(gValue.y); | |||
Serial.print(" "); | |||
Serial.println(gValue.z); | |||
Serial.print("Resultant g: "); | |||
Serial.println(resultantG); | |||
Serial.print("Temperature in °C: "); | |||
Serial.println(temp); | |||
Serial.println("********************************************"); | |||
delay(1000); | |||
} | |||
</syntaxhighlight> |
Revision as of 16:25, 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
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 3d
Code MPU6500 The smaller
/***************************************************************************
* Example sketch for the MPU6500_WE library
* For further information visit:
* https://wolles-elektronikkiste.de/en/mpu9250-9-axis-sensor-module-part-1 (English)
***************************************************************************/
#include <MPU6500_WE.h>
#include <Wire.h>
#define MPU6500_ADDR 0x68
MPU6500_WE myMPU6500 = MPU6500_WE(MPU6500_ADDR);
void setup() {
Serial.begin(115200);
Wire.begin();
if(!myMPU6500.init()){
Serial.println("MPU6500 does not respond");
}
else{
Serial.println("MPU6500 is connected");
}
Serial.println("Position you MPU6500 flat and don't move it - calibrating...");
delay(1000);
myMPU6500.autoOffsets();
Serial.println("Done!");
myMPU6500.enableGyrDLPF();
//myMPU6500.disableGyrDLPF(MPU6500_BW_WO_DLPF_8800); // bandwdith without DLPF
/* Sample rate divider divides the output rate of the gyroscope and accelerometer.
* Sample rate = Internal sample rate / (1 + divider)
* It can only be applied if the corresponding DLPF is enabled and 0<DLPF<7!
* Divider is a number 0...255
*/
myMPU6500.setSampleRateDivider(5);
myMPU6500.setGyrRange(MPU6500_GYRO_RANGE_250);
myMPU6500.setAccRange(MPU6500_ACC_RANGE_2G);
myMPU6500.enableAccDLPF(true);
delay(200);
}
void loop() {
xyzFloat gValue = myMPU6500.getGValues();
float temp = myMPU6500.getTemperature();
Serial.println("Acceleration in g (x,y,z):");
Serial.print(gValue.x);
Serial.print(" ");
Serial.print(gValue.y);
Serial.print(" ");
Serial.println(gValue.z);
Serial.print("Resultant g: ");
Serial.println(resultantG);
Serial.print("Temperature in °C: ");
Serial.println(temp);
Serial.println("********************************************");
delay(1000);
}