Arduino MPU6050

MPU6050 gyroscope and a accelerometer sensor with Arduino and servo. In this example I’m using raw accelerometer data.

If you liked this blog post I’d appreciate if you followed me on Twitter

Related Post

Remember to share...Share on FacebookTweet about this on TwitterShare on Google+Share on LinkedInEmail this to someone

60 thoughts on “Arduino MPU6050

  1. Hello. I copied the code into Arduino IDE, and I got the following error:

    sketch_may29a.ino: In function ‘void loop()’:
    sketch_may29a:29: error: ‘amp’ was not declared in this scope
    sketch_may29a:29: error: expected ;' before ')' token

    I added the code 'int amp;', and I got the following error again:

    sketch_may29a.ino: In function 'void loop()':
    sketch_may29a:30: error: expected
    )’ before ‘;’ token
    sketch_may29a:30: error: expected `;’ before ‘)’ token

    any suggestion to fix this error?

    thanks

    1. Hi, thanks for the comment. WordPress encoded & into &

      I updated the post again, the correct line is:

      mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);

      Regards,
      Luka.

  2. the code here is able to move the servo if you rotate the sensor around its x and y axis. but how can I let the servo move if I rotate the sensor around its z axis, for example, put the sensor on the table, and then just rotate it, the servo will move as well.

    I try to modify the code. For example, in Line_31, I change the code from ax to az, but the servo doesn’t move if I rotate the sensor around its z-axis

    cheers

    1. Hi Nick,

      Did you manage to get the z axis to work? I too can only get the x and y axes to work, but can’t pull out the z values.

      Please let me know,

      Nick

  3. i use ur code and i have some errors … can u help me plz ?
    the error is
    {
    sketch_jun27a:5: error: ‘MPU6050’ does not name a type
    sketch_jun27a.cpp: In function ‘void setup()’:
    sketch_jun27a:21: error: ‘mpu’ was not declared in this scope
    sketch_jun27a.cpp: In function ‘void loop()’:
    sketch_jun27a:28: error: ‘mpu’ was not declared in this scope
    sketch_jun27a:28: error: ‘amp’ was not declared in this scope
    sketch_jun27a:28: error: expected `;’ before ‘)’ token
    }

          1. Yes … and i exchange (mpu.initialize)by(accelgyro.initialize) and there is no error
            After that i burn it to arduino uno but its not work with the servo lik urs …

            Maybe the problem in wire connection between gyro , servo , arduino !!!

  4. Yes … and i exchange (mpu.initialize)by(accelgyro.initialize) and there is no error .After that i burn it to arduino uno but its not work with the servo lik urs …
    Maybe the problem in wire connection between gyro , servo , arduino !!!

  5. Hello. Why do you use map() function? I mean is it really 90% (or even more?) of data coming from MPU is just noise that has to be cut ?

  6. Hey, good project but I couldn’t work the codes. Can you send me the real code as e-mail ? or other way you choose. Thank you so much

  7. sir , i want the the libraries
    #include “I2Cdev.h”
    #include “MPU6050.h”
    give me the links to these libraries

  8. Hi Luka. I’d be very grateful if you could send me the wiring of your project. I really like the code, because it’s not so complicated like others. Thank you.

  9. Hi, i use your code and i have some errors … 🙁

    sketch_aug29b.ino:4:11: error: #include expects “FILENAME” or
    sketch_aug29b:11: error: ‘Servo’ does not name a type
    sketch_aug29b.ino: In function ‘void setup()’:
    sketch_aug29b:24: error: ‘myservo’ was not declared in this scope
    sketch_aug29b.ino: In function ‘void loop()’:
    sketch_aug29b:34: error: ‘myservo’ was not declared in this scope

  10. Hi Luka, very well done. i am a beginner at this, and working on a project that could use this feature, is it possible to send me a pic of how to connect it together. the schematic.
    thank you, take care.

  11. #include “Wire.h”
    #include “I2Cdev.h”
    #include “MPU6050.h”
    #include “Servp.h”

    MPU6050 mpu;

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

    Servo myservoy;
    Servo myservox;
    Servo myservoz;
    Servo myservogy;
    Servo myservogx;
    Servo myservogz;

    int valy;
    int valx;
    int prevValy;
    int prevValx;
    int valz;
    int prevValz;
    int valgy;
    int valgx;
    int prevValgy;
    int prevValgx;
    int valgz;
    int prevValgz;

    void setup()
    {
    Wire.begin();
    Serial.begin(115200);

    Serial.println(“Initialize MPU”);
    mpu.initialize();
    Serial.println(mpu.testConnection() ? “Connected” : “Connection failed”);
    myservoy.attach(9);
    myservox.attach(8);
    myservoz.attach(7);
    myservogy.attach(6);
    myservogx.attach(5);
    myservogz.attach(4);

    }

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

    valy = map(ay, -17000, 17000, 0, 179);
    Serial.print(“\t”);
    if (valy != prevValy)
    {
    myservoy.write(valy);
    Serial.print(“Y=”);
    Serial.print(valy);
    prevValy = valy;
    }

    valx = map(ax,-17000,17000,0,179);
    Serial.print(“\t”);
    if (valx != prevValx)
    {
    myservox.write(valx);
    Serial.print(“x=”);
    Serial.print(valx);
    prevValx = valx;

    }

    valz = map(az, -17000, 17000, 0, 179);
    Serial.print(“\t”);
    if (valz != prevValz)
    {
    myservoz.write(valz);
    Serial.print(“z=”);
    Serial.print(valz);
    prevValz = valz;
    }

    valgy = map(gy, -17000, 17000, 0, 179);
    Serial.print(“\t”);
    if (valgy != prevValgy)
    {
    myservogy.write(valgy);
    Serial.print(“gY=”);
    Serial.print(valgy);
    prevValgy = valgy;
    }

    valgx = map(gx,-17000,17000,0,179);
    Serial.print(“\t”);
    if (valgx != prevValgx)
    {
    myservogx.write(valgx);
    Serial.print(“gx=”);
    Serial.print(valgx);
    prevValgx = valgx;

    }

    valgz = map(gz, -17000, 17000, 0, 179);
    Serial.print(“\t”);
    if (valgz != prevValgz)
    {
    myservogz.write(valgz);
    Serial.print(“gz=”);
    Serial.print(valgz);
    prevValgz = valgz;
    }

    Serial.println();
    delay(50);
    }

  12. Hi. I noticed that you are using the accelerometer values. So it is sensing the gravitation of earth and that is why you can just use it with “y” and “x” axis. The point of this, when it senses a vibration, the servo also reacts. so the ideal programming would be using the gyro values instead of the accelerómeter. But this is more difficult, because you have to integer 2 times to get the angle from the angular acceleration from the gyro.

  13. Hello, i saw that you use a map function like this: map(gy, -17000, 17000, 0, 179)
    Can you explain why did you use -17000 and 17000?

    1. Hello Ngyuyen its probably because of the range… a range of 2g gives 16384 which is arbitrary 17000 LSB if u divide lika a random accel_x_out value . Lets say 690/16384 you would get 0.04 which is gravidity. Its just a scaling thing , look at the datasheet try to learn and code yourcode self.

  14. hi Luka,
    please, give me link library for MPU6050

    ploblem my project is ‘MPU6050’ does not name a type, when verify code
    please give me solution!

    Thx

  15. Hi excuse my bad English, I wanted to know the pin for the accelerometer in the video to arduino, thanks for the quick response

  16. Hi
    excuse my bad English, i wanted to know why the IDE don’t load the MPU libraries and the word MPU6050.h not turn orange, thi is a one of errors:
    arduino\libraries\MPU6050\MPU6050.cpp:87: error: ‘I2Cdev’ has not been declared

    Thanks

  17. Hi! thanks for sharing your knowledge with us, but no way this sketch works. I believe the problem is that the libraries
    “I2Cdev.h” and “MPU6050.h” that are not installed. U have the link so I can download?

  18. I stuck in very strange issue working with the MPU 6050
    If i read 0x75 register i get 0x68 which is correct also if i write any register i of mpu & read it back i get correct data that menace my connection & driver is correct
    but now the issue is if i read the Gyro or Acclerometer data i always get 0 i checked power management register it is correct i am making SLEEP bit zero
    i have checked this same behavior on the two sensors
    I am using my own initialization routine also micro controller is also different its microchip make

    I have tried all the codes which are on internet but its not working I am always getting Zero. If i write & read the other configuration register i can read correct data that mean my connection is 100% correct I2c line also looks normal i have checked it on DSO it is good

    I think i am missing something while initializing the MPU6050 sensor & can anyone just tell me list of register i need to write to start reading the sensor

  19. Does anybody could pls give the link for the libraries “I2Cdev.h” and “MPU6050.h”? many people are not finding it including me, i tried github unsucessfuly. appreciate some directons!!!

  20. hi
    i copied your codes and i removed servo parts at last i am trying to print mpu6050 value but its printing special characters.
    what is the problem?

  21. Hello,

    When i check the projet, i have this error message :

    This report would have more information with
    “Show verbose output during compilation”
    enabled in File > Preferences.
    Arduino: 1.0.6 (Windows 7), Board: “Arduino Uno”
    sketch_jan08c.cpp.o: In function __static_initialization_and_destruction_0':
    C:\Program Files\Arduino/sketch_jan08c.ino:6: undefined reference to
    MPU6050::MPU6050()’
    sketch_jan08c.cpp.o: In function loop':
    C:\Program Files\Arduino/sketch_jan08c.ino:29: undefined reference to
    MPU6050::getMotion6(int*, int*, int*, int*, int*, int*)’
    sketch_jan08c.cpp.o: In function setup':
    C:\Program Files\Arduino/sketch_jan08c.ino:22: undefined reference to
    MPU6050::initialize()’
    C:\Program Files\Arduino/sketch_jan08c.ino:23: undefined reference to `MPU6050::testConnection()’

    Why ???

    Thank’s

  22. Did anyone every get the values for az (Z or Yaw axis) to change? Mine shows very little change around 17,000 – 18,000 raw values for az when it is rotated 360 degrees. The pitch and roll (ay and ax) results are accurate.

  23. hello every one ,i a a b-tech student finall year so i request any body please give me the code for making segay vehicle……

  24. Hi, this error

    ‘mpu’ was not declared in this scope

    This libraries
    #include “Wire.h”
    #include “I2Cdev.h”
    #include “MPU6050.h”
    #include “Servo.h”

    Anybody help, please?

    Thank

  25. Hi, I am getting an error message ‘MPU6050’ does not name a type. I am beginner, not sure why I am getting this error message. Could you please help me rectify this?

    I have installed all the necessary libraries for MPU, I2Cdev, Wire and Servo.

    Please help

  26. I need Help.

    #include “I2Cdev.h”

    ^

    compilation terminated.

    exit status 1
    Fehler beim Kompilieren für das Board Arduino Nano.

    I don’t know, what to do…

  27. UNA PREGUNTA POR QUE CUANDO VERIFICA LA PROGRAMACIÓN ME MARCA EL SIGUIENTE ERROR:
    #include “I2Cdev.h”

    ^

    compilation terminated.

    exit status 1
    Error compilando para la tarjeta Arduino/Genuino Uno.

Leave a Reply

Your email address will not be published. Required fields are marked *