Jump to content
I2Cdevlib Forums
luisrodenas

Arduino Sketch to automatically calculate MPU6050 offsets

Recommended Posts

Why i get this ? 

Arduino: 1.8.1 (Windows 10), Board: "WeMos D1 R2 & mini, 80 MHz, 921600, 4M (3M SPIFFS)"

D:\MPU6050_calibration\MPU6050_calibration.ino: In function 'void setup()':

MPU6050_calibration:62: error: 'TWBR' was not declared in this scope

   TWBR = 24; // 400kHz I2C clock (200kHz if CPU is 8MHz). Leonardo measured 250kHz.

   ^

exit status 1
'TWBR' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
 

Share this post


Link to post
Share on other sites
On 1/31/2014 at 4:12 PM, luisrodenas said:

Fixed!! There was a bug in the sketch.

Variables were overflowing in 8-bit Arduinos, like the UNO or Leonardo.

 

Now it works fine in my Leonardo, but took a while to converge, 2-3 minutes.

 

Thank you for your contribution and let me know if it works for you now.

how did you get this fixed? I am using uno board and it is taking forever. a help will be nice.

Send any character to start sketch.

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...
-234    -135    17729    -3    0    -1
102    -52    16091    -5    0    -1
-75    -36    16578    5    -2    -3
0    352    16065    -3    3    -11
-33    24    16581    4    -4    25
-150    -251    16330    -5    6    -30
223    343    16379    2    -6    33
-204    -174    16238    0    1    -32
303    190    16519    -2    1    27
-49    -118    16471    0    0    -23
-125    80    16413    -4    3    27
-73    51    16230    7    0    -34
139    -36    16406    -2    1    38
50    452    16604    0    -4    -38
163    -124    16578    -2    5    38
175    0    16349    6    -1    -41
-306    -179    16161    -7    -4    41
305    182    16530    5    6    -43
-413    -400    16120    -7    -7    50
-118    203    16312    5    12    -50
156    -264    16642    0    -15    47
-235    -107    16077    0    12    -41
50    24    16520    -8    -10    36
-176    -310    16327    10    6    -30
76    506    16280    -5    -2    24
510    261    16770    11    0    -25
68    152    16361    -14    0    18
63    -96    16255    23    -1    -23
-520    -344    16262    -29    3    26
487    544    16904    35    -4    -26
268    -118    16165    -36    8    25
-318    -83    16343    34    -4    -21
-405    -280    16202    -36    -3    23
270    153    16597    33    -2    -31
187    109    16321    -29    5    33
5    -21    16362    24    -1    -29
-459    -326    16217    -30    1    29
196    130    16524    34    0    -25
308    93    16500    -28    -6    17
-210    225    16245    23    12    -16
-97    -108    16423    -14    -8    23
27    -88    16324    6    0    -22
-215    -191    16229    -5    5    19
163    40    16440    7    -3    -16
-42    -23    16342    -6    -1    20
144    462    16583    8    -2    -27
101    -270    16409    -7    2    25
-31    141    16387    12    -5    -18
-174    -79    16374    -23    3    12
-58    38    16192    15    5    -6
 

 

 

these are my readings....

Share this post


Link to post
Share on other sites
On 5/8/2017 at 1:15 AM, ximbax007 said:

I have tried to use the program, but he don´t calculate the offsets. He show the serial, but  stoped and not show anything

I had the same problem.

Make sure you don't have anything else connected to your Arduino and that you haven't connected the ADO pin to your arduino

Share this post


Link to post
Share on other sites

I'm doing a project with the MPU6050 accelerometer. First I calculate the displacements of the MPU only once executing the sketch of Luis Ródenas,

Later, once I get the displacements, I copy them into the configuration of a second sketch and I always execute it with the same values.

//Placa casa MPU6050 viejo
//-1172  -873  1351  76  -758  27
accelgyro.setXAccelOffset(-1172);
accelgyro.setYAccelOffset(-873);
accelgyro.setZAccelOffset(1351);
accelgyro.setXGyroOffset(76);
accelgyro.setYGyroOffset(-773);
accelgyro.setZGyroOffset(33);

The problem I have is that after a time of having executed the second sketch several times, the raw readings present a new displacement

I read that this problem can be by the temperature of the sensor, that it is necessary to compensate the readings with the temperature. But I can not find an example to do so. I wanted to ask if anyone knows what the problem might be and how I can fix it

Thank you very much

Share this post


Link to post
Share on other sites

Hi do you have mpu9250 version of your calibration code? i tried to make it wrk on 9250 but no success so far. it says mpu6050 conncetion failed.  of course i change  0x68 values and tried series of related numbers but no success.  If you can help me on this one really preciate it 

Share this post


Link to post
Share on other sites

Hi guys,

First of all, many thanks to luisrodenas for sharing this work! And to all who contributed on the thread along this years.

Studying the code, I have stumbled on a doubt that many here have posted without a definitive answer, which was asked also in many other sources that refers to this calibration code.

What is the motivation for the division by 8 and 4 in the calibration function?
 

  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;

I have searched a lot but couldn't figure it out. Was it based on trial and error?

Also, when we use the setOffset functions, what those really do in the sensor?

    accelgyro.setXAccelOffset(ax_offset);
    accelgyro.setYAccelOffset(ay_offset);
    accelgyro.setZAccelOffset(az_offset);

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


For example, if I took the ax_offset, ay_offset, az_offset, gx_offset, gy_offset, and gz_offset values, and subtract from raw data taken from the sensor (without any offset configuration), would that have the same effect? I am looking forward to experiment it myself, but I still don't have access to the resources yet.

Many thanks

Share this post


Link to post
Share on other sites

Dear luisrodenas,

May I know what is the motivation for the division by 8 and 4 in the calibration function?

 

  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;

Moreover in calibration function, unlike accelerometer offset value why the (gyro offset - mean gyro value) is  divided by 2 instead of 1 which is giro_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);

I would be really thankful if I get the reasons of using different divisor value.

Thank you.

Share this post


Link to post
Share on other sites

Dear luisrodenas,

May I know what is the motivation for the division by 8 and 4 in the calibration function?

 

  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;

Moreover in calibration function, unlike accelerometer offset value why the (gyro offset - mean gyro value) is divided by 2 instead of 1 which is giro_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);

I would be really thankful if I get the reasons for using different divisor value.

Thank you.

Share this post


Link to post
Share on other sites

Dear Susanrenrr3jrlin and anyone who might be confused about these calculations on the calibration function:

On 3/29/2018 at 8:49 PM, renrr3 said:

Studying the code, I have stumbled on a doubt that many here have posted without a definitive answer, which was asked also in many other sources that refers to this calibration code.

What is the motivation for the division by 8 and 4 in the calibration function?

 

On 6/14/2018 at 9:35 AM, Susan said:

May I know what is the motivation for the division by 8 and 4 in the calibration function?

 

On 6/14/2018 at 9:35 AM, Susan said:

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;

     I came upon this problem while trying to calibrate the sensor with a +/-16G range. It turns out that these constants rescale the offsets for the selected sensibility, as the values returned by the MPU6050 are in the maximum range. So, for a +/-2G accelerometer range (standard) it's necessary to divide the coefficients by 8 (16G/8 = 2G) and the gyroscope ones by 4 for a standard sensibility of 250dps (1000dps/4 = 250dps). In order to calibrate the sensor properly, I also had to change the constants 16384 by 2048. This is because the chip provides values between -32,768 and 32,767. With a range of 2G, the gravity acceleration is equivalent to the number 16384 (32767/2). If the range is 16G, then 1G is 2048 (32767/16).

  This explanation was provided by arduarn in the Arduino forum. You can check the original discussion here http://forum.arduino.cc/index.php?topic=535717

  Best wishes,

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×