A review of basic IMU sensors that work with Arduino, and how to interface Arduino with the best sensor available.
In this post, I will be reviewing a few basic IMU (inertia measurement unit) sensors that are compatible with Arduino. I will also give a short tutorial for interfacing an Arduino with the best IMU sensor available.
IMU sensors like the MPU 6050 are used in self-balancing robots, UAVs, smartphones, and more. IMU sensors help us get the position of an object attached to the sensor in three-dimensional space. These values are usually in angles to help us to determine its position. They are used to detect the orientation of smartphones, or in wearable gadgets like the Fitbit, which uses IMU sensors to track movement.
IMU sensors have a wide range of applications, and are even considered to be an inexorable component in quadcopters. Some of the sensors I was able to get my hands on were ...
- ADXL 345 accelerometer
- ITG 3200 gyroscope
- Sparkfun 6 DOF IMU sensor board
- MPU 6050
I was able to work with both accelerometers and gyroscopes separately. However, they are not as accurate alone as when they are combined. Among the lot, I found the Invensense MPU 6050 to be the most reliable and accurate IMU sensor. Apart from being significantly cheaper than the other sensors, the MPU 6050 also performs better.
In this tutorial, I will give you a basic introduction to the MPU 6050, demonstrate how it can be interfaced to an Arduino, and show you how to make a 3D model using the data from your Arduino MPU 6050.
How Does IMU Interfacing Work?
IMU sensors usually consist of two or more parts. Listing them by priority, they are the 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 are embedded inside a single chip. This chip uses I2C (inter-integrated circuit) protocol for communication.
How Does an Accelerometer Work?
Piezo Electric Accelerometer
An accelerometer works on the principle of the piezoelectric effect. Imagine a cuboidal box with a small ball inside it, like in the picture above. The walls of this box are made with piezoelectric crystals. Whenever you tilt the box, the ball is forced to move in the direction of the inclination due to gravity. The wall that the ball collides with creates tiny piezoelectric currents. There are three pairs of opposite walls in a cuboid. Each pair corresponds to an axis in 3D space: X, Y, and Z axes. Depending on the current produced from the piezoelectric walls, we can determine the direction of inclination and its magnitude.
How Does a Gyroscope Work?
Gyroscopes work on the principle of Coriolis acceleration. Imagine that there is a fork-like structure that is in a constant back-and-forth motion. It is held in place using piezoelectric crystals. Whenever you try to tilt this arrangement, the crystals experience a force in the direction of inclination. This is caused as a result of the inertia of the moving fork. The crystals thus produce a current in consensus with the piezoelectric effect, and this current is amplified. The values are then refined by the host microcontroller. Check this short video that explains how a MEMS gyroscope works.
Interfacing the Arduino MPU 6050
The MPU 6050 communicates with the Arduino through the I2C protocol. The MPU 6050 is connected to Arduino as shown in the following diagram. If your MPU 6050 module has a 5V pin, then you can connect it to your Arduino's 5V pin. If not, you will have to connect it to the 3.3V pin. Next, the GND of the Arduino is connected to the GND of the MPU 6050.
Arduino MPU 6050 connections
The program we will be running here also takes advantage of the Arduino's interrupt pin. Connect your Arduino's digital pin 2 (interrupt pin 0) to the pin labeled as INT on the MPU 6050.
Next, we need to set up the I2C lines. To do this, connect the pin labeled SDA on the MPU 6050 to the Arduino's analog pin 4 (SDA), and the pin labeled as SCL on the MPU 6050 to the Arduino's analog pin 5 (SCL). That's it! You have finished wiring up the Arduino MPU 6050.
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, take the folder named "MPU6050", and paste it inside the Arduino's "library" folder. To do this, 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 if you don't already have it for your Arduino. Do the same procedure as above to install it. You can find the file here.
If you have done this correctly, when you open the Arduino IDE, you can see "MPU6050" in File --> Examples. Next, open the example program from File --> Examples --> MPU6050 --> Examples --> MPU6050_DMP6.
Arduino MPU 6050 DMP code
Next, you have to upload this code to your Arduino. After uploading the code, open up the serial monitor and set the baud rate as 115200. Next, check if you see something 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." Just type in any character on the serial monitor and send it, and you should start seeing the yaw, pitch, and roll values coming in from the MPU 6050.
Arduino MPU 6050 Serial Monitor
DMP stands for Digital Motion Processing. The MPU 6050 has a built-in 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 seconds before you get accurate values from the Arduino MPU 6050, after which the values will begin to stabilize. Just check out the video below to see if yours is working correctly.
Modeling the Values from the Arduino MPU 6050 in 3D Using Processing (Optional)
If you want to see the 3D model of the sensor, continue reading. To view the 3D representation of the data from the MPU 6050, you need to install the processing software. Processing is similar to Arduino, except for a couple of functions. Processing is mainly used for visualizing data and rendering it in 2D/3D models.
Arduino MPU 6050 with Processing
After installing the processing IDE, you will need to download a library called "Toxi". This library is necessary for our Arduino MPU 6050 processing example. Next, extract this file and paste the folder named "toxiclibs-complete-0020" in the "libraries" folder's directory of processing. You can find the "libraries" folder inside the Sketchbook folder for processing. If you don't, then you will have to create a new folder called "libraries" there and paste the toxiclibs inside it.
To visualize the 3D model in processing, first you have to upload the Arduino code for MPU 6050 (MPU6050_DMP6). Before doing that, you need to comment the line in the Arduino MPU6050_DMP6 code that says ...
#define OUTPUT_READABLE_YAWPITCHROLL by //#define OUTPUT_READABLE_YAWPITCHROLL"
And uncomment the line that says ...
//#define OUTPUT_TEAPOT by #define OUTPUT_TEAPOT".
Next, you have to open the processing example for the MPU 6050. Open processing, then File --> Open, then navigate to the folder where you 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 that is defined in it. By default the line that defines it for LinuxMmac users is ...
String portName = "/dev/ttyUSB1";.
You need to change ttyUSB1 to the port where your Arduino is connected.
Editing the Processing file for the MPU 6050
For Windows users, you need to comment the line that says ...
String portName = "/dev/ttyUSB1"; by //String portName = "/dev/ttyUSB1";.
And uncomment the line that says ...
//String portName = "COM4"; by String portName = "COM4";.
And replace "COM4" with the COM port where 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 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 seconds for the MPU 6050 values to stabilize. After that, you can see the 3D model of your MPU 6050, which moves in accordance with the sensor. Check out the demo for the Arduino MPU 6050 3D model!