Introduction

The MPU-6050 is a low-cost, highly accurate IMU with 6 degrees of freedom. Don’t fret if that last sentence sounded like a foreign language, all will be explained soon!

Today we will look at the MPU-6050 module to see how it works and how to use it with an Arduino. We will also build a very sensitive electronic level.  Although our level will be constructed on a breadboard it could also serve as the basis for a permanent project.

MPU-6050 IMU

The MPU-6050 belongs to a class of devices known as Inertial Measurement Units, or IMUs.  These devices can measure acceleration, inertia and a number of other parameters to allow you to determine their spatial position and velocity.

MPU-6050 Electronic Level with Arduino

IMUs like the MPU-6050 are used in a number of different applications:

  • UAVs like quadcopters and helicopters. Here an IMU is used to determine yaw, pitch, and roll, and to level the devices while flying.
  • Robotics. From balancing robots to robot arms it is common to make use of IMUs to establish position and velocity.
  • Game Controllers. Games like the Wii use an IMU to detect level and motion.
  • Phones and tablets. It is common to find IMUs in phones and tablets for determining the orientation of the device to switch between portrait and landscape modes.
  • Hard drives. An IMU or accelerometer is included in hard drives designed for portable use. They can detect if the drive is being dropped and take appropriate action, like stopping the platters and locking the heads.

Accelerometer

One of the key components of the MPU-6050 is an Accelerometer.  As its name would imply, this is a device that can measure acceleration.

Acceleration is described as being the rate of change of the velocity of an object. An object moving at a constant speed does not have any acceleration.

A common unit of acceleration is G-force. One G-force here on Earth is 9.9 meters per second squared, which is the acceleration of gravity here. On other planets the figure is different.

An accelerometer needs to take into account static acceleration like the force of gravity when making its measurements.

The accelerometer used in the MPU-6050 is a triple-axis accelerometer, meaning that it senses acceleration on an X, Y and Z axis.

Gyroscope

Another key component in the MPU-6050 is a Gyroscope.

A gyroscope can measure angular momentum or rotation along the X, Y and Z axis. These components are critical in maintaining balance for aircraft and spacecraft.

A mechanical gyroscope consists of a wheel or disk mounted so that it can spin rapidly about an axis which is free to move in any direction.

The type of gyroscope used in the MPU-6050 is a “Micro Electro Mechanical System” or MEMS gyroscope. It consists of three sensors, one per axis, that produce a voltage when they are rotated. This voltage is internally sampled using a 16-bit analog to digital converter.

External Sensors

The MPU-6050 can also make use of external sensors. These external devices are interfaced to the MPU-6050 using a second I2C bus, independent of the main I2C bus.

A common use for this external connection is to attach a magnetometer, which can measure magnetic fields on three axes.

This adds an extra three Degrees of Freedom, or DOF, to the sensor. By itself, the MPU-6050 has 6 DOF, three each for the accelerometer and the gyroscope.  Adding a magnetometer would give it 9 DOF.

The MPU-6050 also has an internal temperature sensor.

DMP

The data from the accelerometer, gyroscope and any third-party sensors is passed to an internal Digital Motion Processor, or DMP.

This device correlates the data and formats it to be used on the I2C bus. It is essentially a high-performance microprocessor that is dedicated to processing motion data.

The term DMP was coined by Invensense, a division of TDK. This is the company that developed the MPU-6050, along with many other IMUs.

Pinouts

The MPU-6050 is available in several different configurations and on several different breakout modules.

Here is an illustration of the pinouts of the module that I am using in these experiments:

MPU-6050 Pinouts

This module has an internal voltage regulator, so it is supplied with 5-volts power. The MPU-6050 chip itself uses 3.3-volt logic. The module also has resistors to change the data levels to 3.3-volts.

The pin descriptions of the module are as follows:

  • VCC – 5-volt DC power supply.
  • GND – Ground
  • SCL – This is the I2C clock line.
  • SDA – The I2C data line.
  • XDA – This is the external I2C data line. The external I2C bus is for connecting external sensors.
  • XCL – This is the external I2C clock line.
  • AD0 – This line allows you to change the internal I2C address of the MPU-6050 module. It can be used if the hmodule is conflicting with another I2C device, or if you wish to use two MPU-6050s on the same I2C bus.
  • INT – This is the Interrupt Output.

If you are using a different module you can still perform the experiments listed here., Pay attention to the power requirements of your module as some do not have an internal voltage regulator and operate on 3.3-volt logic.

Note that the module also has orientation markings, to allow you to position it correctly in your device.

MPU-6050 with Arduino

Before we build our electronic level let’s do an experiment using the MPU-6050. To make things a bit easier we will use a couple of libraries and run a demonstration sketch that is included with one of them

I2C and MPU-6050 Libraries

The libraries we are going to be using in our demo are part of the collection of libraries developed by Jeff Rowberg for working with the I2C bus and some common I2C sensors. You can find the complete set of libraries on Github.

This is an extensive collection of libraries, not only for the Arduino but for a number of other common microcontrollers as well.

As it is a bit overwhelming you might find it useful just to get the two libraries we are going to use today – the I2C Development library and the MPU-6050 library.

If you would like more information regarding this excellent set of libraries visit Jeff’s webpage dedicated to I2C development.

These libraries are provided in ZIP format. You can install the ZIP files directly into your Arduino IDE.

  • Open the Arduino IDE.
  • Select the Sketch menu from the top menu bar.
  • Select Include Library. A sub-menu will open.
  • Select Add ZIP Library.
  • A dialog box will open. Navigate to where you saved the libraries and select one of them.
  • The library will be installed into your Arduino IDE.
  • You will need to repeat this step for the second library.

Now that you have the two libraries added to your Arduino IDE it’s time to hook up the MPU-6050 to the Arduino/

MPU-6050 and Arduino Hookup

Here is the hook up that we will use for the experiment:

MPU-6050 Arduino Hookup

The connections are pretty simple, as the MPU-6050 interfaces using the I2C bus. On the Arduino Uno the I2C connections are made using the Analog input pins, A4 is the SDA connection and A5 is the SCL.

We are also using the interrupt output from the MPU-6050. This connects to pin 2 on the Arduino Uno, which is interrupt number 0.

Demo Sketch

When you have everything hooked up try running one of the demo sketches provided in the MPU-6050 library we just installed.

  • Open the Arduino IDE.
  • Select the File menu from the top menu bar.
  • Select Examples. A sub-menu will open up.
  • Scroll down the sub-menu to the section entitled Examples from Custom Libraries.
  • Look for the MPU6050 entry and select it.
  • Another sub-menu will open that says Examples. Select it and two example sketches will be listed
  • Select the MPU6050_DMP6 sketch.

The sketch is quite complex but is very well commented. It will give you a taste of how much math is involved in extracting useful information from the MPU-6050 sensor.

Note that you will need to set your serial monitor to a speed of 115200 baud to try the sketch. There is a lot of data being sent back from the IMU and it requires this higher speed to display it.

MPU-6050 Demo

When you open the serial monitor with the sketch loaded to the Arduino you should see a message saying that the connection to the MPU-6050 has been made successfully.  In order to start running the sketch you need to place your cursor in the serial monitor input box and type a letter, then press Send (or just hit Enter on your keyboard).

You will see a wealth of data displaying the Yaw, Pitch and Roll values. Try moving your sensor around and note how the data changes.

Electronic Level Project

Now onto our electronic level project.

We will build a sensitive level that has both a digital and LED display. The LED display will show you if you are within a degree of being level, if you dare close or if you are over 2 degrees off.

Although I am building this on a solderless breadboard as an experiment this is a good candidate for a permanent project, as it is a truly useful device. If you do decide to build a permanent version of this device you may want to use an Arduino Nano or Pro Mini to save space.

Electronic Level Hookup

Here is the hookup diagram for our electronic level:

MPU-6050 Arduino Electronic Level

The LCD display I am using has an I2C adapter on it to make it easier to connect. It is a 2-line, 16-character display, a very common item.

I used a green LED to indicate that it is level, yellow LEDs to indicate that you are close and red LEDs to indicate you are more than 2 degrees off. You can substitute other colors if you wish, or just use a LED bargraph display.

The dropping resistors I used were 220 ohms, but any value from 150 to 470 ohms will suffice.

Electronic Level Sketch

Here is the sketch I used to make the electronic level work:

This sketch requires another library, the NewLiquidCrystal Library for I2C. You can download the latest version from BitBucket. It is a ZIP file, once again you will need to install this into your Arduino IDE.

Unlike the last sketch, it does not use any custom libraries for the MPU-6050. Instead, the Arduino Wire library for I2C is used and the registers in the MPU-6050 are manipulated directly.

At the beginning of the sketch in the Setup, we calibrate the sensor by reading the acceleration and gyroscope data 100 times. This is then averaged to get a baseline.  Because of this operation, the device will delay a few seconds before it gives its first readings.

Again there is a lot of math in the sketch. Unlike the previous sketch, we are just interested in obtaining the Pitch and Roll values in degrees.

As the data is extracted too quickly for the LCD display to handle we use a counter and just take a reading for every 100 passes.

During the pass that we take to update the display we also look at the Pitch value and use it to determine which LED to illuminate.

MPU-6050 Electronic Level Demo

Load the sketch and run it. After a delay of a few seconds, the LCD display should show the angles, both pitch and roll. The LEDs should light appropriately.

Try moving the level around and note the readings and the LED display.  If you have a conventional level try comparing readings.

Conclusion

The MPU-6050 is a very useful device that has brought a lot of advanced technology to Arduino makers, at a very low price. It has many uses, and we will be using it again in several projects in the future (including the DB1 Robot project).

So get out your breadboard and dream up some well-balanced projects!

Resources

Electronic Level Sketch – The sketch used to create the electronic level.

Jeff Rowberg’s I2C Dev Library – The I2C development library for Arduino, by Jeff Rowberg.

Jeff Rowberg’s MPU-6050 Library – The MPU-6050 library for Arduino, by Jeff Rowberg.

 

Build a Digital Level with MPU-6050 and Arduino
Summary
Build a Digital Level with MPU-6050 and Arduino
Article Name
Build a Digital Level with MPU-6050 and Arduino
Description
We will look at the MPU-6050, an Inertial Measurement Unit that contains both a gyroscope and accelerometer. We will use the MPU-6050 to build a digital level with both an LCD and LED display.
Author
Publisher Name
DroneBot Workshop
Publisher Logo
Tagged on:     

6
Leave a Reply

newest oldest most voted
Michael S

I love all your tutorials a lot, including this one.
You’re one of the best providers of clean and useful information on the subjects of your projects.

Only one shortcoming I see is that you don’t provide any references to the websites where components used in the projects can be purchased.

Thanks again for your great tutorials.

philip wamutitu

hi am a keen follower of your articles from kenya. i recently got an upgrade on my computers operating system to windown 7 ultimate. however my arduino cannot communicate with my laptop. on the device manager the coms and Lpt are not visible, so are other devices hence i cannot configure the arduino drivers. any assistance is welcome.

Thomas Borns

First of all I have to praise you for these really well done tutorials. There are a lot of them on you tube, but yours are among the best. But now to my problem: I copied the mpu-6050-level tutorial, but get an error message when trying to upload the sketch to the arduino. In line 23 ” LiquidCrystal_I2C lcd(i2c_addr, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE); appears the message : ‘POSITIVE’ was not declared in this scope What could be the reason? Unfortunately I can’t find my mistake. Maybe you can help me with a solution. That would… Read more »

Thomas Borns

First of all I have to praise you for these really well done tutorials. There are a lot of them on you tube, but yours are among the best. But now to my problem: I copied the mpu-6050-level tutorial, but get an error message when trying to upload the sketch to the arduino. In line 23 ” LiquidCrystal_I2C lcd(i2c_addr, en, rw, rs, d4, d5, d6, d7, bl, POSITIVE); appears the message : ‘POSITIVE’ was not declared in this scope What could be the reason? Unfortunately I can’t find my mistake. Maybe you can help me with a solution. That would… Read more »

Welcome to the Workshop!

The DroneBot Workshop website will be undergoing a “facelift’ and getting some exciting new features.

These changes will be done over a period of several days.  During this time the website will be operational but some features or pages may be temporarily unavailable.

I apologize for any inconvenience this may cause. Hopefully, these changes will make the site an even more useful resource for you.

I would be interested in getting your feedback on the website once these updates are completed.

Thanks for your understanding.

Bill