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
- 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
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.
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 here. You 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.
- Comment the line : #define OUTPUT_READABLE_YAWPITCHROLL
- Uncomment the line : #define OUTPUT_TEAPOT
- Now load the updated MPU6050_DMP code to Arduino.
- Run the MPUTeapot.pde in Processing
- You’ll see the small plane moving
So hope this would help someone.
Thank You.
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
ReplyDeleteI 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?
DeleteHi 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. :-)
Deletethank 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.
Deletehi! 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.
DeleteHi 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. :-)
DeleteDon'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
DeleteGreat to hear that El, I believe i could get some help from you :-) I started again with MPU6050
DeleteThis comment has been removed by the author.
ReplyDeletethank you dummy class...
ReplyDeleteafter 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
ReplyDeleteHi Dhiyvya, I'm really sorry for the late reply. But did you try changing the device address to 0x69?
DeleteHow do you change the device address? My wiring is all correct.. My device address is 0x53 for some reason right now.
DeleteIn the Krodal's code you can do it by changing the line no 627.
Delete// 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.
This comment has been removed by the author.
ReplyDeletean u please tell the wiring for arduino uno?
ReplyDeleteHi Sidharth,
DeleteYou 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
Thank you Kinath for your help. i appreciate it.
DeleteYou are most welcome Sidharth :-)
DeleteHi Kinath,
ReplyDeleteThanks 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
This comment has been removed by the author.
DeleteHi Edward,
DeleteThank 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?
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...
DeleteTa!
And in the code, did you do the following?
DeleteComment the line :
#define OUTPUT_READABLE_YAWPITCHROLL
Uncomment the line :
#define OUTPUT_TEAPOT
Yep!
DeleteI'm doing this all on a mac, I wouldn't have thought that would have anything to do with it?
Got it! In the processing code the 'use first available port' line forces it to use the wrong port - I needed number 5!
DeleteThanks again :)
Ed
That's great.. Thanks for sharing your information. :-)
DeleteHi Kinath
ReplyDeleteThank 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!
problem solved! :)
DeleteHi Joe, Thanks for the compliments and glad this was a help.
Deletehi joe, how did you solved this problem? this is also my problem now. ty
DeleteOne thing though - I've noticed that the bitbucket link for toxiclibs isn't working at the moment... Just a heads up
ReplyDeleteI found the libraries here:
Deletehttps://bitbucket.org/postspectacular/toxiclibs/downloads
Hello, how do you change the scale to 16 G?Thanks in advance
ReplyDeleteThis comment has been removed by the author.
DeleteIndeed this worked :
DeleteMPU6050_write_reg (MPU6050_ACCEL_CONFIG, MPU6050_AFS_SEL_16G);
MPU6050_write_reg (MPU6050_ACCEL_CONFIG, MPU6050_FS_SEL_2000);
hello , I would be grateful if you let me know how can I put the MPU6050 in the sleep mode , thanks in advance
ReplyDeleteMPU6050_write_reg (MPU6050_PWR_MGMT_1, 1)
DeleteHope this helps
This comment has been removed by the author.
ReplyDeletehello, my firend, I have a question, for what is it useful INT pin 2 (interrupcions)?, Can you explain me?
ReplyDeleteHi Carlos, Yes its for interruptions.
DeletePleaase how to get and use angles
ReplyDeleteI'm working on quadcopter,for my project in school,can you help me with connecting mpu to 4 dc motors ???
ReplyDeleteAnd program it :D
DeleteThanks a flippin million brother! Amazing tutorial! very precise and exactly what i needed!
ReplyDeleteCan you make a video tutorial of it and share? It will be a big favor!
ReplyDeleteHi Kinath!
ReplyDeleteI'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!
everything i sketch this was the output.
DeleteI2C Scanner
Scanning...
I2C device found at address 0x68 !
done
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.
Deletehi Kinath
ReplyDeletei can't find toxic lib in your link that you gave. please help me
Try This.
Deletehttps://bitbucket.org/postspectacular/toxiclibs/downloads
This comment has been removed by the author.
Deletehmmm. seems like i still found trouble. processing shows error. it says that no library of toxi.geom and toxi.processing. where is the files?
DeleteHi, Could you please try this link?
Deletehttps://bitbucket.org/postspectacular/toxiclibs/issues/59/no-library-found-for-toxigeom-and
Scroll down to the last comment.
YOU ARE SO AWESOME!very fast response. thank you so much buddy
ReplyDeleteInitializing I2C devices...
ReplyDeleteTesting 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?
:/
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 ?
ReplyDeleteNever mind.....doh.....its in the PDE....working as it should....stellar work!!!!!!!!!!!!
DeleteIn 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?
ReplyDeleteConnected AD0 TO GND AND PROBLEM WAS SOLVED NOW IT IS showing proper readings😀😁😁
DeleteSerial.print(accel_t_gyro.value.x_gyro, DEC);
ReplyDeleteI need this write to the float x. How ?
This comment has been removed by the author.
ReplyDeleteHi! I keep getting this error.
ReplyDeleteMPU6050_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!
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?
DeleteI 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.
DeleteCould 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.
DeleteThis comment has been removed by the author.
DeleteThe serial monitor only shows this:
DeleteInitializing I2C devices...
Testing device connections...
MPU6050 connection failed
Send any character to begin DMP programming and demo:
What must be the problem? Thank you.
have you selected board correctly in Arduino software. if yes then make sure you have connected ad0 to ground i was having same error
DeleteHi
ReplyDeleteI 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?
Hi, this was the most useful info on MPU6050 and Arduino Mega 2560.
ReplyDeleteBut my I2C device keeps saying not found. No matter what way I try. Please help.
Thank you Rajkumar and glad it helped. Did you try the I2C scanner and check if it gets detected?
DeleteHii, 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....
ReplyDeletethanks.. :)
I have uploaded I2C scanner code
ReplyDeletein serial monitor, it says
scanning...
no I2C devices found
i have cross checked every connection. still the same.
pls help me
this helped a lot ,thanks for sharing your resources....
ReplyDeleteHello!
ReplyDeleteI 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!
ReplyDeleteI 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
I am trying to interface the mpu 650 module to node mcu to calibrating the values for robot i am
ReplyDeletetrying 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
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?
ReplyDeleteIs 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.
ReplyDeleteSEE MORE THIS-
Binance support number
Binance customer support number
Binance customer care number
Binance customer service number
Binance customer support number
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