Tag Archives: velocity

Arduino 6DOF Motion Sensor: The MPU6050 Inertial Measurement Unit

Arduino 6DOF Motion Sensor: The MPU6050 Inertial Measurement Unit

Arduino is an open source electronic prototyping platform build with the single desire to bring the immense power of microcontroller to the general public who wishes to incorporate some level of logic circuitry in their projects.  The most powerful feature of Arduino is its capability to handle various kinds of sensors with minimum or no change of its stock hardware. Plus the community that has grown over the years has made it even easier and faster for anybody to interface sensors with Arduino and do some seriously cool stuff.

Once of such amazing sensor that can be used with Arduino is the MPU-6050 Inertial Measurement Unit. Sounds unfamiliar? It should because it is a bit of an advanced sensor that is mainly used projects concerning orientation and motion. MPU-6050 is the world’s first 6DOF orientation measuring sensor on a chip. Built by the world famous InvenSense, a company specializing in motion sensing equipment.

If you have used motion sensors before or have researched about it then you will know that IMU boards having accelerometer and gyroscopes often are misaligned and needs a lot of pins or analog input channel from the microcontroller/PC. However the MPU6050 only needs 2 pins in most of the cases and 3 pins if you want to use the interrupt feature of Arduino.

If you have read so far you may be wondering what are IMUs, gyroscopes and accelerometers. For sake of keeping this article focused on the MPU6050 I have chosen not to go too much in details about the said topics.

However some important concepts are

1)   Gyroscope measure the rate of change of angular orientation i.e angular velocity with respect to a reference frame. Often MEMS gyro or digital gyros have ratings in dps (degree per second unit). The MPU6050 has ranges between ±250 to ±2000 dps. The more range you select the more the device will be sensitive to the change.

2)   Accelerometers measures rate of change of linear velocity not angular velocity. This is a common mistake made by people when using accelerometers. Accelerometers are used to find the change of liner acceleration the body experiences when a force is applied on it. Often the unit of measurement for this device is in G-force i.e multiple of acceleration due to gravity (g=9.81 m/s2). MPU6050 has user selectable accelerometer range between ±2g, ±4g, ±8g and ±16g.

3)   Accelerometer and Gyroscope when placed in one board then it is called Inertial Measurement Unit.

4)   For being a cheap chip it is quite versatile. 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.

You may be thinking many people have done orientation solution using only gyros like ITG-3000 series so why is the accelerometer needed? Why even the DMP is given?

The answer to these questions will require a lot of words which will not be the focus of this article. However the best answer to the above question is that gyroscope readings suffers from an error within a short amount of time. This is called drifting. To solve this problem accelerometer data is needed. Together with these two data we can get 6axis orientation data which will give greater amount of stability and finer control.

The DigitalMotionProcessor or DMP adds to the feature of 9 Axis Motion Fusion by taking data from an external magnetometer to calculate Euler angle and subsequently Quaternions. The key principle of DMP is that it will handle the heavy duty of calculating the Euler angle when all the input variables will be given. What you have got to do this take out the Euler angles from the MPU6050 using your microcontroller via the I2C bus preferably at 400Khz. The faster you read data the more accurate your calculations will be. Hence this chip has solved a major issue with IMUs. Other IMUs needs the processing power of the microcontroller which often take up a large space of already limited RAM of microcontrollers.  This often clogs up the processor and makes it difficult to make time sensitive applications.

 Uses:

IMU are widely used in projects where balancing or motion sensing is needed. For example Quadcopters (Multi Rotor Helicopters or more commonly known as UAVs) use this sensor to stabilize in automode while begin in air. Balancing robots use the orientation data to decide which wheel should get more power and which should get less in order to remain stable without falling down.

In order to use MPU-6050 you will need to buy a break-out board which makes it easy for Arduino to connect with the chip. A picture of such a board is given below

MPU-6050_pic1

This particular board is called GY-521. It is quite famous and has a big community with a vast array of libraries to run it.

If you have experimented with other Arduino sensors like Parallex Ping Ultrasonic sensor, LDRs etc you will see that there is no analog output from this sensor. This chip communicates with Arduino using I2C or (read as I square C) communication protocol.  I2C bus one of the most common method of communication between varies microcontrollers, sensors etc. Please visit this link: http://www.gammon.com.au/forum/?id=10896.  It is one of the best tutorials teaching the use of I2C with Arduino. I2C is also called Two-Wire or just Wire communication protocol by some chip manufacturing companies.

Don’t worry you don’t need to know too much of this protocol if you want to test the device Arduino has a library called Wire which pretty much takes care of the hard part of I2C. All you need to do is connect the sensor properly.

If you have a GY-521 then you can do the following test. Follow the instruction exactly and you will see get to see the full power of this sensor. Other boards are also compatible but since I have this particular board on hand I have done this experiment with it.

Instructions:

MPU-6050_pic2

Hook up the sensor with your Arduino board. If you have gone through Nick’s tutorial then the connection scheme will appear easy to you.

MPU-6050_pic3

The connection scheme is

  • Arduino’s +5V to Vcc of MPU6050
  • Arduino’s GND to GND of MPU6050
  • Arduino’s SCL (AN5 in UNO board) to SCL of MPU6050
  • Arduino’s SDA (AN4 in UNO board) to SCK of MPU6050
  • Arduino’s digital pin 2 (INT0) to INT of MPU6050

Don’t connect anything to AD0, XDA, XCL. AD0 determines the address of this device and XDA and XCL are used for connecting a secondary sensor (magnetometer) with MPU6050 for 9Axis Motion Fusion.

Once the connection is done connect the Arduino to your PC. The GY521 board should power up and the PowerUp LED should be lit as shown in the above figure.  Now go to http://www.i2cdevlib.com/devices/mpu6050#links and from there download their library. Instruction of setting up the library is given in the website.

Once the library for Arduino is set up now comes the part of visualization.  For this you will need the Processing language. You can download Processing for free in from their website. If you haven’t used Processing before you will find it identical to Arduino IDE. Don’t be alarmed, Arduino is based on Processing and for that reason both the IDEs looks alike.

Once Processing is setup and running you will need to install the Toxiclib Library. Go to this link and you should be able to find the required library http://hg.postspectacular.com/toxiclibs/downloads/

The procedure for setting up a Processing Library is same as that of setting up the Arduino library.  Now with the library installed go into the folder which contains “MPU6050_DMP6” folder. There will be another folder called “Processing” with a file called “MPUTeapot.pde”. THIS is the file for Processing just like a file with (.ino) extension is used to mean Arduino sketch.

Now open a new window of Processing and just drag MPUTeapot file into the window. Immediately you will see Arduino style coding.  Keep that window open and go back to Arduino. Now from the Example tab find out the library that you installed from i2Cdevlib. Open the MPU6050_DMP6 sketch.  Don’t just upload it yet, scroll down a few lines and you will see that some line resembling are commented out i.e lines having (//) before it.

Example: //#define OUTPUT_READABLE_QUATERNION

If you want to see the graphical representation of the data from MPU6050 then you will need to uncomment/activate some of the lines. If you are feeling lazy just copy paste the following lines after “include <Wire.h>” line

#define OUTPUT_READABLE_QUATERNION

#define OUTPUT_READABLE_EULER

#define OUTPUT_READABLE_YAWPITCHROLL

#define OUTPUT_READABLE_REALACCEL

#define OUTPUT_READABLE_WORLDACCEL

#define OUTPUT_TEAPOT

Now verify the sketch and upload it into the Arduino board/ ATMEGA 328P microcontroller.

Now comes the final part (FINALLY!)

You will need to make a small adjustment in the Processing code. If you are using Windows then whenever you connect your Arduino board you will need to select the serial port or COM port to make Arduino talk with your PC. Make a note of which comport your Arduino is connected. In my case my Arduino UNO is connected to COM8. Now in the Processing code look for a line like the following picture

MPU-6050_pic7

Change “COM6” to match your comport number for instance I changed mine to COM8 from COM6 i.e just change the number and keep other things untouched.

Now press the RUN button and as they say watch the magic.

If all goes well you will see a small window pop up which will have a arrow/ plane like structure. Now if you move the chip you will see that the plane will follow your movement i.e the using DMP you are getting very precise orientation data used to control the virtual arrow. Some screen shots are given below:

MPU-6050_pic5MPU-6050_pic6

That’s it! You have successfully achieved communication between your orientation sensor and Ardunio board.

Don’t worry if you haven’t being successful. This kind of Arduino experiment is for advanced users and I strongly discourage beginners to trying this before getting warmed up to Arduino.

Reference:

For more information regarding this sensor please visit the products official page and the following links

http://www.i2cdevlib.com/devices/mpu6050#links

https://www.sparkfun.com/products/10937

http://www.geekmomprojects.com/gyroscopes-and-accelerometers-on-a-chip/#comment-3575

http://en.wikipedia.org/wiki/Inertial_Measurement_Unit

http://en.wikipedia.org/wiki/Accelerometer

http://en.wikipedia.org/wiki/Gyroscope

http://en.wikipedia.org/wiki/G-force

http://electronics.stackexchange.com/questions/72927/how-to-add-additional-i2c-sensor-module-on-arduino

http://playground.arduino.cc//Main/I2CBi-directionalLevelShifter#.Uw3-qGeyL1U

https://github.com/TKJElectronics/KalmanFilter

A few notes before ending this article

Try using Processing 1.51 it is the most stable version

If you have used MPU6050 the I recommend GY86 10DOF board for more accurate navigation

Reading the datasheet given by Ivensense on this MPU6050 specifically revision 4 of the datasheet.

I used Arduino IDE V1.0.5, it is the most stable of them all and supports all the libraries till to date