Saturday, July 12, 2014

MPU6050 (GY-521 Breakout) + Arduino Mega 2560 Accelerometer and Gyroscope Application

Hi All,

I’m going to show you today how we can use the GY-521 Breakout with the Arduino Mega 2560 to get the readings from the sensor.

First I have to say that, I am totally new to Arduino and I have a limited knowledge in working with the registers and all the deep stuff. I’m trying to learn them and yes, this will be a beginning. J

So first let me show you what I have already bought.
First GY-521 Breakout for MPU-6050

 

Let me provide you some brief introduction about this product.
The InvenSense MPU-6050 sensor contains a MEMS accelerometer and a MEMS gyro in a single chip. It is very accurate, as it contains 16-bits analog to digital conversion hardware for each channel. Therefor it captures the x, y, and z channel at the same time.
- Chip: MPU-6050
- Power supply: 3.5V (But as there is a voltage regulator on the breakout board, you can use 5V directly)
- Communication mode: standard IIC communication protocol
- Chip built-in 16bit AD converter, 16bit data output
- Gyroscopes range: +/- 250 500 1000 2000 degree/sec
- Acceleration range: +/- 2g, +/- 4g, +/- 8g, +/- 16g
MPU-6050 Datasheet can be found here. Register map can be found here
Here is the schematic of GY-521



Now we have to get the Arduino Mega 2560 Board.



All the information related to this product can be found here.
So we have our items and the next important thing is to hookup these two together. Note the wiring is different for Arduino UNO. This wiring is for Arduino Mega 2560.



Here the connections are
GY-521                                  Arduino Mega
VCC                                        3.3V or 5V
GND                                      GND
SCL                                         Pin 21 (SCL)
SDA                                        Pin 20 (SDA)
INT                                         Pin 2 (Interrupt 0) – for more interrupt pins in Arduino click here


Now for the program. As we have a lot of resources regarding the usage of Arduino, let’s try the code from them (Although I had to spend several hours to work with them :P ).

First let’s try to find whether our device is connected with the Arduino. For that we can use the I2C Scanner code by Krodal to find out the connected I2C devices to Arduino. You can find the code here
Also following is the code. Compile the code and upload it to Arduino. Open the serial monitor and reset the Arduino.



It will find your GY-521 at the address 0x68 or 0x69. If it doesn’t find the IMU at that location, there might be an issue with your wiring. Please check it again. 


Now we can use the Krodal’s next code to get the raw outputs from the sensor. Krodal’s sketch can be found here. Please scroll to the end of the page and you’ll be able to find the code. Also I’ll be posting it here.


Here's the output.


Now we are ready to try the code from Jeff Rowberg. Download the I2C library from github. Here’s the link.

Now unzip the file and find the Arduino folder. Copy the I2Cdev and MPU6050 libraries to your Arduino libraries located at C:\Program Files (x86)\Arduino\libraries.



Now you are ready to use the I2Cdev and MPU6050 libraries. Locate example code in \Arduino\MPU6050\Examples\MPU6050_DMP6 and upload it to the Arduino. Now you will get the output from the Serial monitor. The output provides you the sensible values from the sensor unlike the raw outputs. Here Jeff has used the onboard Digital Motion Processor™ (DMP™) capable of processing complex 9-axis MotionFusion algorithms. You can find Jeff’s work here.






Now we can use the Processing demo provided by Jeff with the MPU6050 example. You can download Processing hereYou also have to install the toxiclib for Processing. You can find that here.

To install the toxic library just copy the contents of the zip file to the library folder in Pocessing projects directory.

Also create a folder named MPUTeapot in the Processing projects folder and copy the MPUTeapot.pde file to that folder from MPU6050 example code location.

Now you have to do the following changes in the Jeff’s MPU6050_DMF code.
  1. Comment the line  : #define OUTPUT_READABLE_YAWPITCHROLL
  2. Uncomment the line : #define OUTPUT_TEAPOT
  3. Now load the updated MPU6050_DMP code to Arduino.
  4. Run the MPUTeapot.pde in Processing
  5. You’ll see the small plane moving
So hope this would help someone. 
Thank You. 

81 comments:

  1. hi! I can´t get jeff examples to work properly, the raw example and de dmp6 also, I just read weird symbols at the serial monitor, any advice? I have connected properrly the MPU6050, even connected the A0 to Arduino's GND to make sure it is at 0x68 adress, as another fact the serial monitor increases its horizontal scroll bar, so, its writing just from left to right all data without line jumps, I preciate any kind of help

    ReplyDelete
    Replies
    1. I have solved it, it just was the adjustment of bauds at the serial monitor as same as the code, thank you! Now my doubt is only linking processing with the code, it´s only matter of pasting it at the processing IDE? I never have used Processing, and the type of code saiys 'Java', it´s that ok?

      Delete
    2. Hi El, happy to hear that you have solved your issue. Regarding the processing one, you just have to copy and paste the code. You can use it without an issue. Sorry for the late reply and thank you for your interest. :-)

      Delete
    3. thank you very much! it work fine! With this awesome tutorial done, I want to work with the values of the degrees of inclination and declination, and also measure the vibration over the MPU6050, I mean, if the board is at more than 90 degrees from the axis, one Arduino's output will be HIGH, is that possible to configure with this program or I have to use another? thanks bro.

      Delete
    4. hi! again!, I always solve my doubts immediatly I ask you about'em hehe I´m sorry, now I´m just gonna tell you what's next for my arduino project. Now I want to connect the mpu6050 and the rfid module within to tne I2C bus, if you know something that I can use I´m listening for advices, I will keep you up to date with my project and also if someone else on this site are having similar troubles or projects.

      Delete
    5. Hi El, Sorry I'm late in responding again. I haven't really worked with RFID yet. But I believe with all your hard work and interest will help you to do a successful project. I would suggest you to write a blog post on your project :-). So others will benefit from that. Thanks El. :-)

      Delete
    6. Don't worry. Now I'm using MPU6050 with SD Shield, with the RFID shield, Tiny clock shield, SD audio files to play when position of MPU's data specifies to activate an interruption, this will be register at a txt file. It is working fine together but my project isn't done yet, i'll keep in touch

      Delete
    7. Great to hear that El, I believe i could get some help from you :-) I started again with MPU6050

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. after uploading these codes I always see i2c device not found in serial monitor what can I do for that please respond me as soon as possible

    ReplyDelete
    Replies
    1. Hi Dhiyvya, I'm really sorry for the late reply. But did you try changing the device address to 0x69?

      Delete
    2. How do you change the device address? My wiring is all correct.. My device address is 0x53 for some reason right now.

      Delete
    3. In the Krodal's code you can do it by changing the line no 627.

      // Default I2C address for the MPU-6050 is 0x68.
      // But only if the AD0 pin is low.
      // Some sensor boards have AD0 high, and the
      // I2C address thus becomes 0x69.
      #define MPU6050_I2C_ADDRESS 0x68

      I'm not exactly sure why your device address is 0x53. Normally for MPU6050 it should be 0x68 or 0x69.

      Delete
  4. This comment has been removed by the author.

    ReplyDelete
  5. an u please tell the wiring for arduino uno?

    ReplyDelete
    Replies
    1. Hi Sidharth,
      You can connect to Arduino UNO with GY-521 as given in the following image.

      http://42bots.com/wp-content/uploads/2014/03/MPU6050-Arduino-Uno-Connections.jpg

      If you refer to the wire library you will see that pins A4 and A5 are indicated as the SDA and SCL pins respectively. Interrupt pin for UNO is 2.

      http://arduino.cc/en/reference/wire

      Delete
    2. Thank you Kinath for your help. i appreciate it.

      Delete
    3. You are most welcome Sidharth :-)

      Delete
  6. Hi Kinath,

    Thanks for the guide - it's great!

    I'm having a little difficulty with the processing plane; Jeff's code works well but the plane does not move. May be a communication error but the code seems fine - any ideas?

    Thanks,
    Ed

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Hi Edward,
      Thank you for the compliments. :-) Did you recheck the wiring? And did you run the I2C scanner and check the address of the IMU? Some time that might be the issue. Device address can be either 0x68 or 0x69. Could you check it?

      Delete
    3. Yep, wiring is okay as far as i'm aware! Address is default at 0x68. I can get the code to spit out angle etc but as soon as processing gets involved there seems to be a road block...

      Ta!

      Delete
    4. And in the code, did you do the following?

      Comment the line :
      #define OUTPUT_READABLE_YAWPITCHROLL
      Uncomment the line :
      #define OUTPUT_TEAPOT

      Delete
    5. Yep!
      I'm doing this all on a mac, I wouldn't have thought that would have anything to do with it?

      Delete
    6. Got it! In the processing code the 'use first available port' line forces it to use the wrong port - I needed number 5!

      Thanks again :)
      Ed

      Delete
    7. That's great.. Thanks for sharing your information. :-)

      Delete
  7. Hi Kinath

    Thank you for the guide - it's the clearest one around for sure.

    I'm having issues at the Arduino code stage. I've uploaded Jeff's script to the Arduino Mega after checking that I am definitely receiving a variety of the Raw data from the mpu6050. Unfortunately when I bring up the Serial Monitor after uploading Jeff's code I am just seeing:
    Initializing I2C devices...
    Testing device connections...
    MPU6050 connection successful

    Send any character to begin DMP programming and demo:


    .....and then nothing. Also me moving the MPU itself is having no effect on this

    Thanks and I hope you can help!


    ReplyDelete
    Replies
    1. Hi Joe, Thanks for the compliments and glad this was a help.

      Delete
    2. hi joe, how did you solved this problem? this is also my problem now. ty

      Delete
  8. One thing though - I've noticed that the bitbucket link for toxiclibs isn't working at the moment... Just a heads up

    ReplyDelete
    Replies
    1. I found the libraries here:
      https://bitbucket.org/postspectacular/toxiclibs/downloads

      Delete
  9. Hello, how do you change the scale to 16 G?Thanks in advance

    ReplyDelete
    Replies
    1. This comment has been removed by the author.

      Delete
    2. Indeed this worked :
      MPU6050_write_reg (MPU6050_ACCEL_CONFIG, MPU6050_AFS_SEL_16G);
      MPU6050_write_reg (MPU6050_ACCEL_CONFIG, MPU6050_FS_SEL_2000);

      Delete
  10. hello , I would be grateful if you let me know how can I put the MPU6050 in the sleep mode , thanks in advance

    ReplyDelete
    Replies
    1. MPU6050_write_reg (MPU6050_PWR_MGMT_1, 1)

      Hope this helps

      Delete
  11. This comment has been removed by the author.

    ReplyDelete
  12. hello, my firend, I have a question, for what is it useful INT pin 2 (interrupcions)?, Can you explain me?

    ReplyDelete
  13. Pleaase how to get and use angles

    ReplyDelete
  14. I'm working on quadcopter,for my project in school,can you help me with connecting mpu to 4 dc motors ???

    ReplyDelete
  15. Thanks a flippin million brother! Amazing tutorial! very precise and exactly what i needed!

    ReplyDelete
  16. Can you make a video tutorial of it and share? It will be a big favor!

    ReplyDelete
  17. Hi Kinath!
    I'm having issues just like joe, but he already solved it, but i dont know how did he do that, can you pls help me. tnx!

    ReplyDelete
    Replies
    1. everything i sketch this was the output.


      I2C Scanner
      Scanning...
      I2C device found at address 0x68 !
      done

      Delete
    2. Hi aaron, I can't exactly guess the reason for this. But did you recheck your wiring? Have you used the correct interrupt pin? Please check the code too. There might be an issue in the loop.

      Delete
  18. hi Kinath

    i can't find toxic lib in your link that you gave. please help me

    ReplyDelete
    Replies
    1. Try This.
      https://bitbucket.org/postspectacular/toxiclibs/downloads

      Delete
    2. This comment has been removed by the author.

      Delete
    3. hmmm. seems like i still found trouble. processing shows error. it says that no library of toxi.geom and toxi.processing. where is the files?

      Delete
    4. Hi, Could you please try this link?
      https://bitbucket.org/postspectacular/toxiclibs/issues/59/no-library-found-for-toxigeom-and

      Scroll down to the last comment.

      Delete
  19. YOU ARE SO AWESOME!very fast response. thank you so much buddy

    ReplyDelete
  20. Initializing I2C devices...
    Testing device connections...
    MPU6050 connection failed

    Send any character to begin DMP programming and demo:
    Initializing DMP...
    DMP Initialization failed (code 1)

    Any ideas how to fix it?
    :/

    ReplyDelete
  21. running into a comport issue ....my arduino is on com12, but I have other devices on other com ports..how would I change the arduino code to be only 12 ?

    ReplyDelete
    Replies
    1. Never mind.....doh.....its in the PDE....working as it should....stellar work!!!!!!!!!!!!

      Delete
  22. In MPU6050 example code it is showing that mpu 6050 connection failed but in compilation there is no error and all connection were looking proper as i2c connection test showed i2c device connected at 0X68 what should i do?

    ReplyDelete
    Replies
    1. Connected AD0 TO GND AND PROBLEM WAS SOLVED NOW IT IS showing proper readings😀😁😁

      Delete
  23. Serial.print(accel_t_gyro.value.x_gyro, DEC);
    I need this write to the float x. How ?

    ReplyDelete
  24. This comment has been removed by the author.

    ReplyDelete
  25. Hi! I keep getting this error.

    MPU6050_DMP6.ino: In function 'void setup()':
    MPU6050_DMP6.ino:216:60: error: 'digitalPinToInterrupt' was not declared in this scope
    Error compiling.

    I am new to this and I do not how to fix it. Please help me. Thank you so much!

    ReplyDelete
    Replies
    1. Hi Leia, Have you connected the interrupt pin correctly with the MPU6050? And could you please tell me which code block you are trying to execute? Did you try the I2C Scanner code to check whether the module is correctly connected to Arduino board?

      Delete
    2. I already tried the I2C Scanner code. oh, and I am using Intel Galileo Board Gen 2, is there a significant difference? I am trying to execute the code of Jeff.

      Delete
    3. Could you get correct output from the I2C scanner code? I have not tried this with the intel board. But for the Arduino, it should work without a problem. Please check with the interrupt pin of your board. You should be able to find this using a guide.

      Delete
    4. This comment has been removed by the author.

      Delete
    5. The serial monitor only shows this:
      Initializing I2C devices...
      Testing device connections...
      MPU6050 connection failed

      Send any character to begin DMP programming and demo:


      What must be the problem? Thank you.

      Delete
    6. have you selected board correctly in Arduino software. if yes then make sure you have connected ad0 to ground i was having same error

      Delete
  26. Hi
    I have used the code and have the following output:
    Accel x, y, z: 224, 988, -19292

    The sensor has not been moved. Why I do not get the value zero for x, y, z?

    ReplyDelete
  27. Hi, this was the most useful info on MPU6050 and Arduino Mega 2560.

    But my I2C device keeps saying not found. No matter what way I try. Please help.

    ReplyDelete
    Replies
    1. Thank you Rajkumar and glad it helped. Did you try the I2C scanner and check if it gets detected?

      Delete
  28. Hii, i am using MPU6050 and arduino mega 2560 and 4 ESC's and receiver and transmitter. So anybody please send my code and connections for it....
    thanks.. :)

    ReplyDelete
  29. I have uploaded I2C scanner code
    in serial monitor, it says
    scanning...
    no I2C devices found

    i have cross checked every connection. still the same.
    pls help me

    ReplyDelete
  30. this helped a lot ,thanks for sharing your resources....

    ReplyDelete
  31. Hello!
    I did everything as stated in the instructions, but when I fill MPU6050_DMP6 code in Arduino, the compiler gives an incomprehensible big error:
    In file included from C:\Program Files (x86)\Arduino\libraries\MPU6050\Examples\MPU6050_DMP6\MPU6050_DMP6.ino:49:0:

    C:\Program Files (x86)\Arduino\libraries\MPU6050-0.0.1\src/MPU6050_6Axis_MotionApps20.h: In member function 'uint8_t MPU6050::dmpGetGravity(int16_t*, const uint8_t*)':

    C:\Program Files (x86)\Arduino\libraries\MPU6050-0.0.1\src/MPU6050_6Axis_MotionApps20.h:689:65: warning: integer overflow in expression [-Woverflow]

    - (int32_t)qI[2] * qI[2] + (int32_t)qI[3] * qI[3]) / (2 * 16384);

    ^

    libraries\MPU6050-0.0.1\I2Cdev.cpp.o (symbol from plugin): In function `I2Cdev::I2Cdev()':

    (.text+0x0): multiple definition of `I2Cdev::I2Cdev()'

    libraries\I2Cdev\I2Cdev.cpp.o (symbol from plugin):(.text+0x0): first defined here

    Please tell me how to fix this problem? What could cause this kind of error?
    I will be grateful!

    ReplyDelete

  32. I am trying to interface the mpu 650 module to node mcu to calibrating the values for robot i am
    trying to useed the display the calibrating values (X-Y axis) please provide the code for node mcu.
    thankyou.
    link:http://bigbelectronics.in/product.php?product=gy-521-mpu6050-module-3-accelerometer-arduino

    ReplyDelete
  33. I am trying to interface the mpu 650 module to node mcu to calibrating the values for robot i am
    trying to useed the display the calibrating values (X-Y axis) please provide the code for node mcu.
    thankyou.
    link:http://bigbelectronics.in/product.php?product=gy-521-mpu6050-module-3-accelerometer-arduino

    ReplyDelete
  34. I have a problem with MPU6050 gyro sensors, I am making school project with Vehicle Accident GPS tracking. When the normal condition, eventhing is ok. Gyro is working his axis,but when over limit gyro's x,y,z values, LCD and Serial monitor is stop their process. how can i do that?

    ReplyDelete
  35. Is your Binance account locked? Tried every possible solution but still unable to unlock Binance account? If yes, it’s time to switch to the customer care professionals who can take you out from such errors in an accessible manner. The support experts team are trained and know the accurate solution related to your error. To get in touch with the Binance experts team, you have to dial Binance phone number or drop your queries via an email or chat box.The professionals are always there at your service.

    SEE MORE THIS-

    Binance support number
    Binance customer support number
    Binance customer care number
    Binance customer service number
    Binance customer support number

    ReplyDelete
  36. on the MPU6050_DMP6 code I keep getting the "error compiling for board arduino mega or mega 2560" even though im using the arduino mega 2560

    ReplyDelete