MPU6500 and MPU6050 Arduino Simple Code: Difference between revisions

From wikiluntti
Line 15: Line 15:
<syntaxhighlight lang="C">
<syntaxhighlight lang="C">
/***************************************************************************
/***************************************************************************
* Example sketch for the MPU9250_WE library (MPU6050)
* https://wolles-elektronikkiste.de/en/mpu9250-9-axis-sensor-module-part-1  (English)
* https://wolles-elektronikkiste.de/en/mpu9250-9-axis-sensor-module-part-1  (English)
*
***************************************************************************/
***************************************************************************/
#include <MPU6050_WE.h>
#include <MPU6050_WE.h>
Line 23: Line 21:
#define MPU6050_ADDR 0x68
#define MPU6050_ADDR 0x68


/* There are several ways to create your MPU6050 object:
* MPU6050_WE myMPU6050 = MPU6050_WE()              -> uses Wire / I2C Address = 0x68
* MPU6050_WE myMPU6050 = MPU6050_WE(MPU6050_ADDR)  -> uses Wire / MPU6050_ADDR
* MPU6050_WE myMPU6050 = MPU6050_WE(&wire2)        -> uses the TwoWire object wire2 / MPU6050_ADDR
* MPU6050_WE myMPU6050 = MPU6050_WE(&wire2, MPU6050_ADDR) -> all together
* Successfully tested with two I2C busses on an ESP32
*/
MPU6050_WE myMPU6050 = MPU6050_WE(MPU6050_ADDR);
MPU6050_WE myMPU6050 = MPU6050_WE(MPU6050_ADDR);


Line 57: Line 48:
void loop() {
void loop() {
   xyzFloat gValue = myMPU6050.getGValues();
   xyzFloat gValue = myMPU6050.getGValues();
  float temp = myMPU6050.getTemperature();
   float resultantG = myMPU6050.getResultantG(gValue);
   float resultantG = myMPU6050.getResultantG(gValue);



Revision as of 18:13, 17 January 2025

Introduction

Libraries

  • FastIMU
  • MPU6050_WE
    • Wire / I2C addr = 0x68
    • Wire / MPU6050 addr
    • TwoWire obj wire2

Theory

MPU6050_WE.h

/***************************************************************************
* https://wolles-elektronikkiste.de/en/mpu9250-9-axis-sensor-module-part-1  (English)
***************************************************************************/
#include <MPU6050_WE.h>
#include <Wire.h>
#define MPU6050_ADDR 0x68

MPU6050_WE myMPU6050 = MPU6050_WE(MPU6050_ADDR);

void setup() {
  Serial.begin(115200);
  Wire.begin();
  if(!myMPU6050.init()){
    Serial.println("MPU6050 does not respond");
  }
  else{
    Serial.println("MPU6050 is connected");
  }
  
  Serial.println("Position you MPU6050 flat and don't move it - calibrating...");
  delay(1000);
  myMPU6050.autoOffsets();
  Serial.println("Done!");
  
  myMPU6050.setGyrRange(MPU6050_GYRO_RANGE_250);
  myMPU6050.setAccRange(MPU6050_ACC_RANGE_2G);
  
  Serial.println( "ax, ay, az, a" );

  delay(200);
}

void loop() {
  xyzFloat gValue = myMPU6050.getGValues();
  float resultantG = myMPU6050.getResultantG(gValue);

  Serial.print(gValue.x);
  Serial.print(", ");
  Serial.print(gValue.y);
  Serial.print(", ");
  Serial.print(gValue.z);
  Serial.print(", ");
  Serial.println(resultantG);

  delay(100);
}