Arduinompu6050 Tutorial
Arduinompu6050 Tutorial
com/arduino-mpu-6050-imu-sensor-tutorial/
In this post, I will be reviewing a few basic IMU (Inertia Measurement Unit)
sensors, compatible with arduino. I shall also give a short tutorial for
interfacing arduino with the best IMU sensor available. IMU sensors like the
MPU 6050 are used in self balancing robots, UAVs, smartphones, etc.
IMU sensors are one of the most inevitable type of sensors used today in all
kinds of electronic gadgets. They are seen in smartphones, wearables,
game controllers, etc. IMU sensors help us in getting the attitude of an
object, attached to the sensor in three dimensional space. These values
usually in angles, thus help us to determine its attitude. Thus, they are used
in smartphones to detect its orientation. And also in wearable gadgets like
the nike fuel band or fit bit, which use IMU sensors to track movement.
1 of 11 12/24/16, 11:49 AM
Arduino MPU 6050 - Best IMU Sensor Tutorial - DIY Hacking https://diyhacking.com/arduino-mpu-6050-imu-sensor-tutorial/
Software:
2 of 11 12/24/16, 11:49 AM
Arduino MPU 6050 - Best IMU Sensor Tutorial - DIY Hacking https://diyhacking.com/arduino-mpu-6050-imu-sensor-tutorial/
IMU sensors usually consists of two or more parts. Listing them by priority,
they are : accelerometer, gyroscope, magnetometer and altimeter. The
MPU 6050 is a 6 DOF (Degrees of Freedom) or a six axis IMU sensor, which
means that it gives six values as output. Three values from the
accelerometer and three from the gyroscope. The MPU 6050 is a sensor
based on MEMS (Micro Electro Mechanical Systems) technology. Both the
accelerometer and the gyroscope is embedded inside a single chip. This
chip uses I2C (Inter Integrated Circuit) protocol for communication.
3 of 11 12/24/16, 11:49 AM
Arduino MPU 6050 - Best IMU Sensor Tutorial - DIY Hacking https://diyhacking.com/arduino-mpu-6050-imu-sensor-tutorial/
4 of 11 12/24/16, 11:49 AM
Arduino MPU 6050 - Best IMU Sensor Tutorial - DIY Hacking https://diyhacking.com/arduino-mpu-6050-imu-sensor-tutorial/
The program we will be running here, also takes advantage of the arduino’s
interrupt pin. Therefore, connect arduino’s digital pin 2 (interrupt pin 0) to
the pin labelled as INT on the MPU 6050. Next, we need to set up the I2C
lines. For this connect the pin labelled as SDA on the MPU 6050 to the
arduino’s analog pin 4 (SDA). And the pin labelled as SCL on the MPU 6050
to the arduino’s analog pin 5 (SCL). And that’s it, you have finished wiring up
the Arduino MPU 6050.
Step 2: Uploading the code and testing the Arduino MPU 6050
To test the Arduino MPU 6050, first download the arduino library for MPU
6050, developed by Jeff Rowberg. You can find the library here. Next you
have to unzip/extract this library. And then take the folder named
“MPU6050” and paste it inside the arduino’s “library” folder. That is, go to
the location where you have installed arduino (Arduino –> libraries) and
paste it inside the libraries folder. You might also have to do the same thing
to install the I2Cdev library as well, if you don’t already have it for your
arduino. Do the same procedure as above for installing it, you can find the
file here: I2Cdev library.
If you have done this correctly, then when you open the arduino IDE, you
can see “MPU6050” in File –> Examples. Next, open the example program
5 of 11 12/24/16, 11:49 AM
Arduino MPU 6050 - Best IMU Sensor Tutorial - DIY Hacking https://diyhacking.com/arduino-mpu-6050-imu-sensor-tutorial/
from: File –> Examples –> MPU6050 –> Examples –> MPU6050_DMP6.
Now, you have to upload this code to arduino. After uploading the code,
open up the serial monitor and set the baud rate as 115200. Next, check if
you see stuff like “Initializing I2C devices…” on the serial monitor. If you
don’t, just press the reset button. Now, you’ll see a line saying “Send any
character to begin DMP programming and demo: “. So just type in any
character on the serial monitor and send it. And you will start seeing the
yaw, pitch and roll values coming in from the MPU 6050. Like so:
6 of 11 12/24/16, 11:49 AM
Arduino MPU 6050 - Best IMU Sensor Tutorial - DIY Hacking https://diyhacking.com/arduino-mpu-6050-imu-sensor-tutorial/
DMP stands for Digital Motion Processing. Here, the Invense’s MPU 6050
has an inbuilt motion processor. It processes the values from the
accelerometer and gyroscope to give us accurate 3D values.
Also, you will need to wait about 10 secs before you get accurate values
from the Arduino MPU 6050. After which, the values will begin to stabilize.
Just check out the video, if you have any doubts:
7 of 11 12/24/16, 11:49 AM
Arduino MPU 6050 - Best IMU Sensor Tutorial - DIY Hacking https://diyhacking.com/arduino-mpu-6050-imu-sensor-tutorial/
After installing the processing IDE, next you will need to download a library
called “Toxi”. This library is necessary for our Arduino MPU 6050 processing
example. Download the latest zip file for the library from here. Next, you
need to extract this file and paste the folder named “toxiclibs-
complete-0020” in the libraries directory of processing. You can find the
“libraries” folder inside the Sketchbook folder of processing. If you don’t,
then you will have to create a new folder called “libraries” there, and paste
the toxiclibs inside it.
8 of 11 12/24/16, 11:49 AM
Arduino MPU 6050 - Best IMU Sensor Tutorial - DIY Hacking https://diyhacking.com/arduino-mpu-6050-imu-sensor-tutorial/
Next, you have to open the processing example for the MPU 6050. Open
processing, then: File –> Open. And then navigate to the folder where you
had installed the MPU6050 library for arduino. You can find the processing
example in: MPU6050 –> Examples –> MPU6050_DMP6 –> Processing –>
MPUTeapot.
In this code, you have to then check the serial port which is defined in it. By
default the line that defines it for linux/mac users is like:
9 of 11 12/24/16, 11:49 AM
Arduino MPU 6050 - Best IMU Sensor Tutorial - DIY Hacking https://diyhacking.com/arduino-mpu-6050-imu-sensor-tutorial/
And for windows users, you need to comment the line which says:
And replace “COM4” with the COM port on which your arduino is connected
(check this by going into arduino and Tools –> Serial Port).
Now we can test the whole setup. First upload the arduino code
(MPU6050_DMP6) through arduino. And please remember NOT to open the
serial monitor. Next run the processing code (MPUTeapot) by pressing the
button with the “play” symbol. You will see a small plane like object. Wait
for about 10 secs for the MPU 6050 values to get stabilized. After, which
10 of 11 12/24/16, 11:49 AM
Arduino MPU 6050 - Best IMU Sensor Tutorial - DIY Hacking https://diyhacking.com/arduino-mpu-6050-imu-sensor-tutorial/
you can see the 3D model of your MPU 6050, which moves accordingly with
the sensor. Now, check out the demo for the Arduino MPU 6050 3D model:
11 of 11 12/24/16, 11:49 AM