MisterBotBreak
Published

How to Use an IMU (MPU6050)

This project will show you how to use a IMU.

BeginnerFull instructions provided1 hour757
How to Use an IMU (MPU6050)

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
Inertial Measurement Unit (IMU) (6 deg of freedom)
Inertial Measurement Unit (IMU) (6 deg of freedom)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Custom parts and enclosures

MPU6050 Library

i2c Dev Library

Schematics

mpu_zkdLdUOMXX.fzz

Code

MPU-6050 code

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

MPU6050 accelgyro;
 
int16_t ax, ay, az;  
int16_t gx, gy, gz;
double angleA1, angleA2; 
double angleG1, angleG2; 
double angleC1, angleC2; 

void setup() {
  Wire.begin();  
  Serial.begin(9600); 
  accelgyro.initialize();
  
}
 
void naturalValues(){

  Serial.print("Ax :");
  Serial.println(ax); 
  Serial.print("\t");
  Serial.print("Ay :");
  Serial.println(ay); 
  Serial.print("\t");
  Serial.print("Az :");
  Serial.println(az); 
  Serial.print("\t");


  Serial.print("Gx :");
  Serial.println(gx);
  Serial.print("\t"); 
  Serial.print("Gy :");
  Serial.println(gy); 
  Serial.print("\t");
  Serial.print("Gz :");
  Serial.println(gz); 
  Serial.println("\t");
}

void angleMeasurementAcceleration(){
  angleA1= atan2((double)ax,(double)az )*180/PI;
  angleA2= atan2((double)ay,(double)az )*180/PI; 

  Serial.print("Anglex :");
  Serial.println(angleA2); 
  Serial.print("\t");
  Serial.print("Angley :");
  Serial.println(angleA1); 
  Serial.print("\t");

}

void angleMeasurementGyroscope(){
  angleG1 = angleG1+float(gy)*0.01/131;
  angleG2 = angleG2+float(gx)*0.01/131;

  Serial.print("Anglex :");
  Serial.println(angleG2); 
  Serial.print("\t");
  Serial.print("Angley :");
  Serial.println(angleG1); 
  Serial.print("\t");
}

void complementaryFilter(){
  angleC1 = 0.9 * (angleC1 + float(gy)*0.01/131) + 0.1 * atan2((double)ax,(double)az)*180/PI;
  angleC2 = 0.9 * (angleC2 + float(gx)*0.01/131) + 0.1 * atan2((double)ay,(double)az)*180/PI;

  Serial.print("Filtered anglex :");
  Serial.println(angleC2); 
  Serial.print("\t");
  Serial.print("Filtered angley :");
  Serial.println(angleC1); 
  Serial.print("\t");
}


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

//naturalValues                                      //
complementaryFilter();                             //  CHOOSE WHICH MEASUREMENT MODE YOU WANT 
//angleMeasurementAcceleration();                    //  DEFAULT : COMPLEMENTARY FILTER
//angleMeasurementGyroscope();                       //


  delay(10);  
}

Credits

MisterBotBreak

MisterBotBreak

48 projects • 149 followers
I love electronics and cats :D !

Comments