Mpu 6050 gyro values in graph

Hi ,
Is there any code that would help me to get gyro value as well as in it graphical form as mentioned in this video ?

It would be helpful for my research on a project

Use the Arduino Serial Plotter.

1 Like

Thank You for the reply . I am using esp32 along with mpu6050. is this the code that need to e uploaded into esp32 to be shown on arduino serial monitor ?

#include <Wire.h>

//Declaring some global variables
int gyro_x, gyro_y, gyro_z;
long acc_x, acc_y, acc_z, acc_total_vector;
int temperature;

void setup() {
  Wire.begin();                                                        //Start I2C as master
  Wire.setClock(400000);
  Serial.begin(115200);                                               //Use only for debugging
  
  setup_mpu_6050_registers();                                          //Setup the registers of the MPU-6050 (500dfs / +/-8g) and start the gyro
}

void loop(){

  read_mpu_6050_data();                                                //Read the raw acc and gyro data from the MPU-6050

 
  Serial.print("Pitch:");
  Serial.print(gyro_x);
  Serial.print(",");
  Serial.print("Roll:");
  Serial.println(gyro_y);
  delay(10);                                                          //Delay 3us to simulate the 250Hz program loop
  
}


void read_mpu_6050_data(){    
      Wire.beginTransmission(0x68); 
      Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) 
      Wire.endTransmission();                                              //End the transmission
      Wire.requestFrom(0x68,14);                                           //Request 14 bytes from the MPU-6050
      acc_x = Wire.read()<<8|Wire.read();// 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
      acc_y = Wire.read()<<8|Wire.read();//0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
      acc_z = Wire.read()<<8|Wire.read();// 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
      temperature = Wire.read()<<8|Wire.read();// 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
      gyro_x = Wire.read()<<8|Wire.read();// 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
      gyro_y = Wire.read()<<8|Wire.read();// 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
      gyro_z = Wire.read()<<8|Wire.read();// 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
      
}
     

void setup_mpu_6050_registers(){
  //Activate the MPU-6050
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x6B);                                                    //Send the requested starting register
  Wire.write(0x00);                                                    //Set the requested starting register
  Wire.endTransmission();                                              //End the transmission
  //Configure the accelerometer (+/-8g)
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x1C);                                                    //Send the requested starting register
  Wire.write(0x10);                                                    //Set the requested starting register
  Wire.endTransmission();                                              //End the transmission
  //Configure the gyro (500dps full scale)
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x1B);                                                    //Send the requested starting register
  Wire.write(0x08);                                                    //0x00 for ±250dps/ 0x08 for ±500dps/ 0x10 for ±1000dps 0x18 for ±2000dps
  Wire.endTransmission();                                              //End the transmission
}

Did you try uploading and running the program? That is one of many possibilities.

To get the pitch, roll and yaw, you need a mathematical calculation. That is called a "filter".
A filter that combines multiple sensors (such as the accelerometer and the gyro) is called "sensor fusion".
Here is an example: https://github.com/jremington/MPU-6050-Fusion.

Don't forget to get the offsets and calibration values from the sensor. That is needed for a good result.

The raw data from the gyro is not like a real gyro. The angle from the gyro is not the orientation of the sensor.

I need to get gyro values only specifically Roll (Gyro Y axis). So is filter require for this as real life roll values would have noise value . I need to take a range of this to ensure a click function (using Mouse.h) .
Secondly , using filter might use extra space in my flash memory which is not convenient for me

A simulation with the 6050 on a plotter:

The raw gyro values are rates of rotation, not angles. Which do you want?

ok but my purpose is to get a range of gyro Y values when our index finger moves in y direction as clicking a button (if you are taking the analogy ) .So are the tates of rotation or angles prefereed in such a scenario

Sorry, I have no idea what you mean.

The sensor axis labels are arbitrary, and it can be oriented any way you like. If the sensor is connected somehow to your finger and the finger moves, then usually, all three values will change.

You will probably have to experiment with the sensor to see if the measurements make sense.

If you do not need an angle, and only need movement, maybe a piezo "tap" sensor will work? If you need to use the 6050, check DroneBotWorkshop: Build a Digital Level with MPU-6050 and Arduino | DroneBot Workshop

Hi , i tested my code but i dint get the graph based on serial plotter . I will attach the graph as well as the code too

#include <Wire.h>

//Declaring some global variables
int gyro_x, gyro_y, gyro_z;
long acc_x, acc_y, acc_z, acc_total_vector;
int temperature;

void setup() {
  Wire.begin();                                                        //Start I2C as master
  Wire.setClock(400000);
  Serial.begin(115200);                                               //Use only for debugging
  
  setup_mpu_6050_registers();                                          //Setup the registers of the MPU-6050 (500dfs / +/-8g) and start the gyro
}

void loop(){

  read_mpu_6050_data();                                                //Read the raw acc and gyro data from the MPU-6050

 
  Serial.print("Pitch:");
  Serial.print(gyro_x);
  Serial.print(",");
  Serial.print("Roll:");
  Serial.println(gyro_y);
  delay(10);                                                          //Delay 3us to simulate the 250Hz program loop
  
}


void read_mpu_6050_data(){    
      Wire.beginTransmission(0x68); 
      Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) 
      Wire.endTransmission();                                              //End the transmission
      Wire.requestFrom(0x68,14);                                           //Request 14 bytes from the MPU-6050
      acc_x = Wire.read()<<8|Wire.read();// 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
      acc_y = Wire.read()<<8|Wire.read();//0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
      acc_z = Wire.read()<<8|Wire.read();// 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
      temperature = Wire.read()<<8|Wire.read();// 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
      gyro_x = Wire.read()<<8|Wire.read();// 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
      gyro_y = Wire.read()<<8|Wire.read();// 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
      gyro_z = Wire.read()<<8|Wire.read();// 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
      
}
     

void setup_mpu_6050_registers(){
  //Activate the MPU-6050
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x6B);                                                    //Send the requested starting register
  Wire.write(0x00);                                                    //Set the requested starting register
  Wire.endTransmission();                                              //End the transmission
  //Configure the accelerometer (+/-8g)
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x1C);                                                    //Send the requested starting register
  Wire.write(0x10);                                                    //Set the requested starting register
  Wire.endTransmission();                                              //End the transmission
  //Configure the gyro (500dps full scale)
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x1B);                                                    //Send the requested starting register
  Wire.write(0x08);                                                    //0x00 for ±250dps/ 0x08 for ±500dps/ 0x10 for ±1000dps 0x18 for ±2000dps
  Wire.endTransmission();                                              //End the transmission
}

Hi ,
Thanx for the response . My idea is simple . I need to get the values of gyro X, Y and Z in a graphical way . I will attach my code and the graph that i obtained .
Unfortunately , I dint get the spikes of gyro X, Gyro Y and Gyro Z .
How will i solve this ? I will attach the screenshot too

#include <Wire.h>

//Declaring some global variables
int gyro_x, gyro_y, gyro_z;
long acc_x, acc_y, acc_z, acc_total_vector;
int temperature;

void setup() {
  Wire.begin();                                                        //Start I2C as master
  Wire.setClock(400000);
  Serial.begin(115200);                                               //Use only for debugging
  
  setup_mpu_6050_registers();                                          //Setup the registers of the MPU-6050 (500dfs / +/-8g) and start the gyro
}

void loop(){

  read_mpu_6050_data();                                                //Read the raw acc and gyro data from the MPU-6050

 
  Serial.print("Pitch:");
  Serial.print(gyro_x);
  Serial.print(",");
  Serial.print("Roll:");
  Serial.println(gyro_y);
  delay(10);                                                          //Delay 3us to simulate the 250Hz program loop
  
}


void read_mpu_6050_data(){    
      Wire.beginTransmission(0x68); 
      Wire.write(0x3B); // starting with register 0x3B (ACCEL_XOUT_H) 
      Wire.endTransmission();                                              //End the transmission
      Wire.requestFrom(0x68,14);                                           //Request 14 bytes from the MPU-6050
      acc_x = Wire.read()<<8|Wire.read();// 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)
      acc_y = Wire.read()<<8|Wire.read();//0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
      acc_z = Wire.read()<<8|Wire.read();// 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
      temperature = Wire.read()<<8|Wire.read();// 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
      gyro_x = Wire.read()<<8|Wire.read();// 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
      gyro_y = Wire.read()<<8|Wire.read();// 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
      gyro_z = Wire.read()<<8|Wire.read();// 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
      
}
     

void setup_mpu_6050_registers(){
  //Activate the MPU-6050
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x6B);                                                    //Send the requested starting register
  Wire.write(0x00);                                                    //Set the requested starting register
  Wire.endTransmission();                                              //End the transmission
  //Configure the accelerometer (+/-8g)
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x1C);                                                    //Send the requested starting register
  Wire.write(0x10);                                                    //Set the requested starting register
  Wire.endTransmission();                                              //End the transmission
  //Configure the gyro (500dps full scale)
  Wire.beginTransmission(0x68);                                        //Start communicating with the MPU-6050
  Wire.write(0x1B);                                                    //Send the requested starting register
  Wire.write(0x08);                                                    //0x00 for ±250dps/ 0x08 for ±500dps/ 0x10 for ±1000dps 0x18 for ±2000dps
  Wire.endTransmission();                                              //End the transmission
}

But you did get a graph. The problem is that the graph does not look as you expected.

Load the serial monitor instead and see what values for pitch and roll are printed out.

1 Like

Hi i checked the serial print and it shows the same value . I will attach the screenshot .
.Still its the same .
Is there any reference code that can solve this isuue ?

The graph is correct. Your program is not communicating with the sensor.

Run the Arduino I2C address scanner program to help find and fix the basic communication problem.

1 Like

i done it but still the same

You could have a defective sensor or a wiring problem.

Please post a close up, focused photo of your setup, along with the revised wiring diagram. Are all the connections cleanly soldered?

Hi got the gyro value and all . But the main problem still lis on the serial plotter. How can i solve it ?
I will attach the code and my setup . I am using esp32c3 mini1 .

I saw this video too for the help :https://www.youtube.com/watch?v=7VW_XVbtu9k

Can you tell me if the code has the mistake or not ?

#include <Wire.h>
float RteRoll , RtePitch , RteYaw;
float AccX ,AccY,AccZ;
float AngleRoll, AnglePitch ;
float LoopTimer;
int SDA_pin= 6;
int Scl_pin= 7;

void gyro_signals() {
  Wire.beginTransmission(0x68);// put your setup code here, to run once:
 Wire.write(0x1A); 
    Wire.write(0x05); 
    Wire.endTransmission();
     Wire.beginTransmission(0x68);// put your setup code here, to run once:
 Wire.write(0x1C); 
    Wire.write(0x10); 
    Wire.endTransmission();
     Wire.beginTransmission(0x68);// put your setup code here, to run once:
 Wire.write(0x3B); 
   Wire.endTransmission();
   Wire.requestFrom(0x68,6);
   int16_t AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)     
  int16_t AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  int16_t AcZ=Wire.read()<<8|Wire.read();
  Wire.beginTransmission(0x68);// put your setup code here, to run once:
 Wire.write(0x1B); 
    Wire.write(0x8); 
    Wire.endTransmission();
    Wire.beginTransmission(0x68);// put your setup code here, to run once:
 Wire.write(0x43); 
    Wire.endTransmission();
     Wire.requestFrom(0x68,6);
     int16_t GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  int16_t GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  int16_t GyZ=Wire.read()<<8|Wire.read();
   RteRoll = (float)GyX/65.5 ;
    RtePitch = (float)GyY/65.5 ;
     RteYaw = (float)GyZ/65.5;
     AccX = (float)AcX/4096;
    AccY = (float)AcY/4096;
    AccZ = (float)AcZ/4096;
    AngleRoll = atan(AccY/sqrt(AccX*AccX + AccZ*AccZ))*1/(3.142/180);
     AnglePitch = - atan(AccX/sqrt(AccY*AccY + AccZ*AccZ))*1/(3.142/180);

}

void setup() {
  Serial.begin(115200);
  Wire.setPins(SDA_pin ,Scl_pin);

    pinMode(SDA_pin,INPUT_PULLUP );
    pinMode(Scl_pin,INPUT_PULLUP );
    Wire.setClock(160000);
      Wire.begin();
      delay(250);
      Wire.beginTransmission(0x68);// put your setup code here, to run once:
 Wire.write(0x6B); 
    Wire.write(0x00); 
    Wire.endTransmission();

  // put your setup code here, to run once:

}

void loop() {
gyro_signals();
Serial.print("acc x=");
Serial.print(AngleRoll);
Serial.print("acc y=");
Serial.print(AnglePitch);

delay(50);
  // put your main code here, to run repeatedly:

}

Describe the problem.

Please don't post pictures of code or text. Copy and paste instead.

1 Like