GY-521

I am using a GY-521 a 6 axis gyro and i am trying to calibrate it using this code.

// Arduino sketch that returns calibration offsets for MPU6050 
//   Version 1.1  (31th January 2014)
// Done by Luis Ródenas <luisrodenaslorda@gmail.com>
// Based on the I2Cdev library and previous work by Jeff Rowberg <jeff@rowberg.net>
// Updates (of the library) should (hopefully) always be available at https://github.com/jrowberg/i2cdevlib

// These offsets were meant to calibrate MPU6050's internal DMP, but can be also useful for reading sensors. 
// The effect of temperature has not been taken into account so I can't promise that it will work if you 
// calibrate indoors and then use it outdoors. Best is to calibrate and use at the same room temperature.

/* ==========  LICENSE  ==================================
 I2Cdev device library code is placed under the MIT license
 Copyright (c) 2011 Jeff Rowberg
 
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
 =========================================================
 */

// I2Cdev and MPU6050 must be installed as libraries
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

///////////////////////////////////   CONFIGURATION   /////////////////////////////
//Change this 3 variables if you want to fine tune the skecth to your needs.
int buffersize=1000;     //Amount of readings used to average, make it higher to get more precision but sketch will be slower  (default:1000)
int acel_deadzone=8;     //Acelerometer error allowed, make it lower to get more precision, but sketch may not converge  (default:8)
int giro_deadzone=1;     //Giro error allowed, make it lower to get more precision, but sketch may not converge  (default:1)

// default I2C address is 0x68
// specific I2C addresses may be passed as a parameter here
// AD0 low = 0x68 (default for InvenSense evaluation board)
// AD0 high = 0x69
//MPU6050 accelgyro;
MPU6050 accelgyro(0x68); // <-- use for AD0 high

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

int mean_ax,mean_ay,mean_az,mean_gx,mean_gy,mean_gz,state=0;
int ax_offset,ay_offset,az_offset,gx_offset,gy_offset,gz_offset;

///////////////////////////////////   SETUP   ////////////////////////////////////
void setup() {
  // join I2C bus (I2Cdev library doesn't do this automatically)
  Wire.begin();
  // COMMENT NEXT LINE IF YOU ARE USING ARDUINO DUE
  TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Leonardo measured 250kHz.

  // initialize serial communication
  Serial.begin(115200);

  // initialize device
  accelgyro.initialize();

  // wait for ready
  while (Serial.available() && Serial.read()); // empty buffer
  while (!Serial.available()){
    Serial.println(F("Send any character to start sketch.\n"));
    delay(1500);
  }                
  while (Serial.available() && Serial.read()); // empty buffer again

  // start message
  Serial.println("\nMPU6050 Calibration Sketch");
  delay(2000);
  Serial.println("\nYour MPU6050 should be placed in horizontal position, with package letters facing up. \nDon't touch it until you see a finish message.\n");
  delay(3000);
  // verify connection
  Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
  delay(1000);
  // reset offsets
  accelgyro.setXAccelOffset(0);
  accelgyro.setYAccelOffset(0);
  accelgyro.setZAccelOffset(0);
  accelgyro.setXGyroOffset(0);
  accelgyro.setYGyroOffset(0);
  accelgyro.setZGyroOffset(0);
}

///////////////////////////////////   LOOP   ////////////////////////////////////
void loop() {
  if (state==0){
    Serial.println("\nReading sensors for first time...");
    meansensors();
    state++;
    delay(1000);
  }

  if (state==1) {
    Serial.println("\nCalculating offsets...");
    calibration();
    state++;
    delay(1000);
  }

  if (state==2) {
    meansensors();
    Serial.println("\nFINISHED!");
    Serial.print("\nSensor readings with offsets:\t");
    Serial.print(mean_ax); 
    Serial.print("\t");
    Serial.print(mean_ay); 
    Serial.print("\t");
    Serial.print(mean_az); 
    Serial.print("\t");
    Serial.print(mean_gx); 
    Serial.print("\t");
    Serial.print(mean_gy); 
    Serial.print("\t");
    Serial.println(mean_gz);
    Serial.print("Your offsets:\t");
    Serial.print(ax_offset); 
    Serial.print("\t");
    Serial.print(ay_offset); 
    Serial.print("\t");
    Serial.print(az_offset); 
    Serial.print("\t");
    Serial.print(gx_offset); 
    Serial.print("\t");
    Serial.print(gy_offset); 
    Serial.print("\t");
    Serial.println(gz_offset); 
    Serial.println("\nData is printed as: acelX acelY acelZ giroX giroY giroZ");
    Serial.println("Check that your sensor readings are close to 0 0 16384 0 0 0");
    Serial.println("If calibration was succesful write down your offsets so you can set them in your projects using something similar to mpu.setXAccelOffset(youroffset)");
    while (1);
  }
}

///////////////////////////////////   FUNCTIONS   ////////////////////////////////////
void meansensors(){
  long i=0,buff_ax=0,buff_ay=0,buff_az=0,buff_gx=0,buff_gy=0,buff_gz=0;

  while (i<(buffersize+101)){
    // read raw accel/gyro measurements from device
    accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
    
    if (i>100 && i<=(buffersize+100)){ //First 100 measures are discarded
      buff_ax=buff_ax+ax;
      buff_ay=buff_ay+ay;
      buff_az=buff_az+az;
      buff_gx=buff_gx+gx;
      buff_gy=buff_gy+gy;
      buff_gz=buff_gz+gz;
    }
    if (i==(buffersize+100)){
      mean_ax=buff_ax/buffersize;
      mean_ay=buff_ay/buffersize;
      mean_az=buff_az/buffersize;
      mean_gx=buff_gx/buffersize;
      mean_gy=buff_gy/buffersize;
      mean_gz=buff_gz/buffersize;
    }
    i++;
    delay(2); //Needed so we don't get repeated measures
  }
}

void calibration(){
  ax_offset=-mean_ax/8;
  ay_offset=-mean_ay/8;
  az_offset=(16384-mean_az)/8;

  gx_offset=-mean_gx/4;
  gy_offset=-mean_gy/4;
  gz_offset=-mean_gz/4;
  while (1){
    int ready=0;
    accelgyro.setXAccelOffset(ax_offset);
    accelgyro.setYAccelOffset(ay_offset);
    accelgyro.setZAccelOffset(az_offset);

    accelgyro.setXGyroOffset(gx_offset);
    accelgyro.setYGyroOffset(gy_offset);
    accelgyro.setZGyroOffset(gz_offset);

    meansensors();
    Serial.println("...");

    if (abs(mean_ax)<=acel_deadzone) ready++;
    else ax_offset=ax_offset-mean_ax/acel_deadzone;

    if (abs(mean_ay)<=acel_deadzone) ready++;
    else ay_offset=ay_offset-mean_ay/acel_deadzone;

    if (abs(16384-mean_az)<=acel_deadzone) ready++;
    else az_offset=az_offset+(16384-mean_az)/acel_deadzone;

    if (abs(mean_gx)<=giro_deadzone) ready++;
    else gx_offset=gx_offset-mean_gx/(giro_deadzone+1);

    if (abs(mean_gy)<=giro_deadzone) ready++;
    else gy_offset=gy_offset-mean_gy/(giro_deadzone+1);

    if (abs(mean_gz)<=giro_deadzone) ready++;
    else gz_offset=gz_offset-mean_gz/(giro_deadzone+1);

    if (ready==6) break;
  }
}

And the output is this-
Send any character to start sketch.

MPU6050 Calibration Sketch

Your MPU6050 should be placed in horizontal position, with package letters facing up.
Don’t touch it until you see a finish message.

MPU6050 connection successful

Reading sensors for first time…

Calculating offsets…



but it just keeps printing the … i left it running for half an hour and still no luck. A picture of how it is wired is attached.

You might need a wire between pin D2 and the int(errupt) pin on the GY-521 (on the far right of your picture)

Wait 5 to 10 min before trusting the results to have the module reach stable operating t°

i have tried with the same results.

Display the variable ready to see if it increases at all in the calibration() function (inside the while(1) which is where you get stuck in an infinite loop.

Also display the readings and see if they make sense when you move your unit around

This is the new output with is printing the ready variable.

Send any character to start sketch.


MPU6050 Calibration Sketch

Your MPU6050 should be placed in horizontal position, with package letters facing up. 
Don't touch it until you see a finish message.

MPU6050 connection successful

Reading sensors for first time...

Calculating offsets...
...
2
...
0
...
1
...
2
...
1
...
0
...
0
...
0
...
0
...
0
...
1
...
0
...
0
...
0
...
1
...
0
...
0
...
0
...
0
...
1
...
1
...
1
...
0
...
0
...
0
...
0
...
0
...
0
...
1
...
0
...
0
...
0
...
0
...
0
...
2
...
0
...
0
...
0
...
0
...
1

MR-Turtle: I am using a GY-521 a 6 axis gyro

So what dimension do you come from then?!

You mean 6DoF IMU.

yea its the same as the one here https://www.robotshop.com/en/6-dof-gyro-accelerometer-imu-mpu6050.html

Can you,make sure you have the latest i2cdevlib

change the Serial.println("..."); to

Serial.print(mean_ax);
Serial.print("\t");
Serial.print(mean_ay);
Serial.print("\t");
Serial.print(mean_az);
Serial.print("\t");
Serial.print(mean_gx);
Serial.print("\t");
Serial.print(mean_gy);
Serial.print("\t");
Serial.println(mean_gz);

see what you get

Now turn the module around from what is being asked and try again - what do you get?

I Up dated the library. this is what I got with your Serial print

Send any character to start sketch.


MPU6050 Calibration Sketch

Your MPU6050 should be placed in horizontal position, with package letters facing up. 
Don't touch it until you see a finish message.

MPU6050 connection successful

Reading sensors for first time...

Calculating offsets...
862 -73 16997   -17 -4  32
-367    -47 16570   36  7   -69
232 0   16243   -45 -9  88
60  44  16284   30  5   -80
-182    177 16576   -23 -3  81
488 133 15908   13  -2  -85
-446    -277    16679   -3  8   78
115 127 16304   1   -15 -76
-145    224 16736   -6  20  80
435 -362    16212   10  -29 -88
-410    120 17025   8   37  88
-223    -338    16413   -11 -33 -88
267 287 16096   7   35  84
-142    -267    16501   1   -29 -89
113 616 15809   -21 21  96
-55 -330    16366   25  -26 -94
-37 -68 16411   -14 22  87
143 77  17042   5   -18 -81
235 -91 16341   5   20  69
-245    -195    16261   22  -20 -59
59  438 16621   -43 26  53
-66 -202    16324   34  -36 -44

This is with it vertical

Send any character to start sketch.


MPU6050 Calibration Sketch

Your MPU6050 should be placed in horizontal position, with package letters facing up. 
Don't touch it until you see a finish message.

MPU6050 connection successful

Reading sensors for first time...

Calculating offsets...
-1753   205 18434   9   2   -14
582 -270    16152   -11 -2  23
-285    46  16376   24  4   -25
365 -311    16229   -41 -7  14
-471    425 16353   52  10  -4
625 92  16303   -63 -14 -2
-303    68  16639   51  19  8
-500    155 15984   -30 -19 -4
-68 -590    16648   10  9   13
524 224 16138   0   1   -22
113 10  16654   -1  0   20
-405    -167    16089   13  0   -32
439 -186    16558   -14 3   32
-696    212 15780   0   -6  -13
359 36  17101   0   5   8
162 -7  16219   3   -2  -5

Try running a standard sketch printing whatever your read from the 6 DOF

Make sure to reset the offset in the setup

 // reset offsets
  accelgyro.setXAccelOffset(0);
  accelgyro.setYAccelOffset(0);
  accelgyro.setZAccelOffset(0);
  accelgyro.setXGyroOffset(0);
  accelgyro.setYGyroOffset(0);
  accelgyro.setZGyroOffset(0);

And see when moving the unit if the data you print makes any sense

yes they do, when I tip it the respected number goes up and down. in my sketch it only has

    mpu.setXGyroOffset(0);
    mpu.setYGyroOffset(0);
    mpu.setZGyroOffset(0);
    mpu.setZAccelOffset(0); // 1688 factory default for my test chip

Hello, it’s me
I was wondering if after all these years you’d like to meet
To go over everything
They say that time’s supposed to heal ya
But I ain’t done much healing
Hello, can you hear me?
I’m in California dreaming about who we used to be
When we were younger and free
I’ve forgotten how it felt
Before the world fell at our feet

There’s such a difference between us
And a million miles

Hello from the other side
I must have called a thousand times
To tell you I’m sorry for everything that I’ve done
But when I call you never seem to be home
Hello from the outside
At least I can say that I’ve tried
To tell you I’m sorry for breaking your heart
But it don’t matter, it clearly doesn’t tear you apart
Anymore

Hello, how are you?
It’s so typical of me to talk about myself, I’m sorry
I hope that you’re well
Did you ever make it out of that town
Where nothing ever happened?

It’s no secret that the both of us
Are running out of time

So hello from the other side (other side)
I must have called a thousand times (thousand times)
To tell you I’m sorry for everything that I’ve done
But when I call you never seem to be home
Hello from the outside (outside)
At least I can say that I’ve tried (I’ve tried)
To tell you I’m sorry for breaking your heart
But it don’t matter, it clearly doesn’t tear you apart
Anymore

(Highs, highs, highs, highs, lows, lows, lows, lows)
Anymore
(Highs, highs, highs, highs, lows, lows, lows, lows)
Anymore
(Highs, highs, highs, highs, lows, lows, lows, lows)
Anymore
(Highs, highs, highs, highs, lows, lows, lows, lows)
Anymore

Hello from the other side (other side)
I must have called a thousand times (thousand times)
To tell you I’m sorry for everything that I’ve done
But when I call you never seem to be home
Hello from the outside (outside)
At least I can say that I’ve tried (I’ve tried)
To tell you I’m sorry for breaking your heart
But it don’t matter, it clearly doesn’t tear you apart
Anymore.
by adele
No one is responding to me.