Creative Commons License
.

Hello all, welcome to another Arduino Sensor Tutorial, in this Blog, we will learn how to wire and code MPU6050 which is a 6 axis Accelerometer, with our Arduino Board, in detail, so follow till end!

Supplies for mpu6050:

Hardware
  1. Arduino Nano
  2. MPU 6050
  3. Jumper Wire
Software
  1. Arduino IDE

( Thanks UTSOURCE to offer electronic components for my projects )

Step 1: Watch the Video Tutorial

MPU6050 is the world’s first integrated 6-axis Motion Tracking device that combines a 3-axis gyroscope, 3-axis accelerometer, and a Digital Motion Processor™ (DMP). A

ll in a small 4x4x0.9mm package which is theIntegrated Circuit in Middle. It is based on I2C communication protocol. Rather than discussing the specifics, refer the Datasheet of MPU 6050.

Step 3: Hardware

MPU6050 comes in a Module form, with 8 pins, but don’t worry.

we will use only 4 important pins and it will be sufficient to integrate with our Arduino Board.

So we have VCC, ground, which takes any input from 2v to 5v. Since this board has a voltage regulator on board and thus supports 3.3v logic high and 5v logic high.

Next we have few complimentary resistors and capacitors in SMD package. It is the most important PART the MPU6050 IC. Which is a MEMS or say micro electro mechanical system, which changes voltage depending on change in axis position.

This IC also has SCL SDA.

Which are I2C pins and XDA and XCL which are auxiliary Serial pins. we won’t use them with Arduino for this tutorial. We have AD0 which is address select between Auxiliary and Primary ports. Lastly we have INT interrupt pin,

connections for our Arduino UNO and NANO are as following:

VCC – 5v

GND – GND

SCL – A5

SDA – A4

(only SDA and SCL pins change for other Arduino boards.)

And that’s all for connection for mpu6050

( find all the components at UTSOURCE )

Step 4: Install Libraries

Before we start Coding, we will need a library called as Arduino MPU-6050 by jarzebski,

also we will need Wire Library, which is inbuilt, so we will just install MPU – 6050 Library.

here is the link to MPU6050 Library.

To install a new library into your Arduino IDE you can use the Library Manager.
  • Open the IDE and click to the “Sketch” menu and then Include Library > select the option to “Add .ZIP Library”.
  • Navigate to the .zip file’s location and open it.

for more information on importing, refer https://www.arduino.cc/en/guide/libraries

Step 5: Open Gyroscope Example.

once the MPU-6050 library is added to Arduino IDE, we have quite a list of examples to choose from, like

  • MPU6050_accel_pitch_roll
  • MPU6050_accel_simple
  • MPU6050_free_fall
  • MPU6050_gyro_pitch_roll_yaw
  • MPU6050_gyro_simple
  • MPU6050_motion
  • MPU6050_temperature

we need to start slow to understand the Library and Basics, so lets start with MPU6050_gyro_simpleexample.

(did you know MPU6050 has a Tempreature Sensor as well, but not very accurate one so we didn’t discuss it here!)

Step 6: Understand the code

Basically in this example, we will see if our sensor is working so we will display the sensor data on serial monitor.

  • So we begin the serial monitor in setup part.
Serial.begin(115200);
  • Execute the sensor test sequence in this While Loop,
while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G)) 
{    Serial.println("Could not find a valid MPU6050 sensor, check wiring!");
delay(500); 
}

Sometimes we are making a project, and have to set our sensor in a specific orientation, we need offsets, we don’t need it for this tutorial,

  • but to change offset, simply un-comment these lines.
// mpu.setGyroOffsetX(155);
// mpu.setGyroOffsetY(15);  
// mpu.setGyroOffsetZ(15);
( uncomment by removing "//" )
  • there is a calibration line, which will virtually set our sensor flat.
// Calibrate gyroscope. The calibration must be at rest.
// If you don't want calibrate, comment this line.
mpu.calibrateGyro();

Remember offset and calibration are two different things, offset will give you defined calibration, for example, you can mount this sensor at weird angle and yet it will act as reference point for zero.

  • Next we have sensitivity, which at default is 3.
// Set threshold sensivty. Default 3.
// If you don't want use threshold, comment this line or set 0. 
mpu.setThreshold(3);
  • In check loop section, basic hardware checking is done.
void checkSettings() { Serial.println(); Serial.print(" * Sleep Mode: "); Serial.println(mpu.getSleepEnabled() ? "Enabled" : "Disabled"); Serial.print(" * Clock Source: "); switch(mpu.getClockSource()) { case MPU6050_CLOCK_KEEP_RESET: Serial.println("Stops the clock and keeps the timing generator in reset"); break; case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println("PLL with external 19.2MHz reference"); break; case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println("PLL with external 32.768kHz reference"); break; case MPU6050_CLOCK_PLL_ZGYRO: Serial.println("PLL with Z axis gyroscope reference"); break; case MPU6050_CLOCK_PLL_YGYRO: Serial.println("PLL with Y axis gyroscope reference"); break; case MPU6050_CLOCK_PLL_XGYRO: Serial.println("PLL with X axis gyroscope reference"); break; case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println("Internal 8MHz oscillator"); break; } Serial.print(" * Gyroscope: "); switch(mpu.getScale()) { case MPU6050_SCALE_2000DPS: Serial.println("2000 dps"); break; case MPU6050_SCALE_1000DPS: Serial.println("1000 dps"); break; case MPU6050_SCALE_500DPS: Serial.println("500 dps"); break; case MPU6050_SCALE_250DPS: Serial.println("250 dps"); break; } Serial.print(" * Gyroscope offsets: "); Serial.print(mpu.getGyroOffsetX()); Serial.print(" / "); Serial.print(mpu.getGyroOffsetY()); Serial.print(" / "); Serial.println(mpu.getGyroOffsetZ()); Serial.println(); }
I highly suggest to leave this loop as it is.
  • in loop section, which is most important part of this entire code, that is getting the values from our sensor. First we need to call the values, using mpu.readRawGyro or mpu.readNormalizeGyro. now the concept of raw and normalized is such that raw are basically numbers and normalized values are values which go through filters and calculations. or you can say, processed data.

I am raw html block.
Click edit button to change this html

void loop() { Vector rawGyro = mpu.readRawGyro(); Vector normGyro = mpu.readNormalizeGyro();

We have 3 axis, called as x y and z, which can be called using variable name which we set as rawGyro. Followed by axis name,to make a project, we will need these 3 values of x,y and z using this variablename.axis command.

I am raw html block.
Click edit button to change this html

Serial.print(" Xraw = "); Serial.print(rawGyro.XAxis); Serial.print(" Yraw = "); Serial.print(rawGyro.YAxis); Serial.print(" Zraw = "); Serial.println(rawGyro.ZAxis); Serial.print(" Xnorm = "); Serial.print(normGyro.XAxis); Serial.print(" Ynorm = "); Serial.print(normGyro.YAxis); Serial.print(" Znorm = "); Serial.println(normGyro.ZAxis);
  • atlast, to view the values at comfortable speed, lets increase the delay from 10 to 1000
delay(10);
}
since our example code is ready and we understand what we did in code, its time to upload the code and view results.

Step 7: Check Results in Serial Port

when the upload is done open up the serial monitor and observe output

Don’t forget to match the serial port with the baud rate we defined in start of code which is 115200

If our wiring and hardware is proper, we should get values of each axis on Serial port for both Raw and Normalized gyroscopic change in sensor.

I am raw html block.
Click edit button to change this html

Initialize MPU6050 * Sleep Mode: Disabled * Clock Source: PLL with X axis gyroscope reference * Gyroscope: 2000 dps * Gyroscope offsets: 0 / 0 / 0 Xraw = -65.00 Yraw = 25.00 Zraw = -29.00 Xnorm = 0.00 Ynorm = 0.00 Znorm = 0.00 Xraw = -62.00 Yraw = 20.00 Zraw = -32.00 Xnorm = 0.00 Ynorm = 0.00 Znorm = 0.00 Xraw = -64.00 Yraw = 25.00 Zraw = -30.00 Xnorm = 0.00 Ynorm = 0.00 Znorm = 0.00 Xraw = -66.00 Yraw = 23.00 Zraw = -29.00 Xnorm = 0.00 Ynorm = 0.00 Znorm = 0.00 Xraw = -67.00 Yraw = 20.00 Zraw = -33.00 Xnorm = 0.00 Ynorm = 0.00 Znorm = 0.00 Xraw = -64.00 Yraw = 22.00 Zraw = -31.00 Xnorm = 0.00 Ynorm = 0.00 Znorm = 0.00

here is the logs of what we got on Serial Port.

Step 8: What Next?

Now this was the basic part, we can also use our sensor to control a model Paper plane using processing software, but that’s a tutorial for next blog.

Subscribe and hit the notification button, to not miss any future blog/ video from Mission Critical.

Also give this post thumps up and share it with your friends. Until the next one, GoodBye!

4 Comments

  1. Knight Sun Reply

    This line is in pink: while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))

    at the very bottom this is

    exit status 1

    ‘class MPU6050’ has no member named ‘begin’

    This is the error code:
    Arduino: 1.8.13 (Windows 10), Board: “Arduino Nano, ATmega328P (Old Bootloader)”

    C:\Users\*****\AppData\Local\Temp\Temp1_Arduino-MPU6050-master.zip\Arduino-MPU6050-master\MPU6050_accel_simple\MPU6050_accel_simple.ino: In function ‘void setup()’:

    MPU6050_accel_simple:20:14: error: ‘class MPU6050’ has no member named ‘begin’

    while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))

    ^~~~~

    MPU6050_accel_simple:20:20: error: ‘MPU6050_SCALE_2000DPS’ was not declared in this scope

    while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))

    ^~~~~~~~~~~~~~~~~~~~~

    C:\Users\Ianka\AppData\Local\Temp\Temp1_Arduino-MPU6050-master.zip\Arduino-MPU6050-master\MPU6050_accel_simple\MPU6050_accel_simple.ino:20:20: note: suggested alternative: ‘MPU6050_RA_FF_DUR’

    while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))

    ^~~~~~~~~~~~~~~~~~~~~

    MPU6050_RA_FF_DUR

    MPU6050_accel_simple:20:43: error: ‘MPU6050_RANGE_2G’ was not declared in this scope

    while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))

    ^~~~~~~~~~~~~~~~

    C:\Users\Ianka\AppData\Local\Temp\Temp1_Arduino-MPU6050-master.zip\Arduino-MPU6050-master\MPU6050_accel_simple\MPU6050_accel_simple.ino:20:43: note: suggested alternative: ‘MPU6050_RA_FF_DUR’

    while(!mpu.begin(MPU6050_SCALE_2000DPS, MPU6050_RANGE_2G))

    ^~~~~~~~~~~~~~~~

    MPU6050_RA_FF_DUR

    C:\Users\Ianka\AppData\Local\Temp\Temp1_Arduino-MPU6050-master.zip\Arduino-MPU6050-master\MPU6050_accel_simple\MPU6050_accel_simple.ino: In function ‘void checkSettings()’:

    MPU6050_accel_simple:45:10: error: ‘MPU6050_CLOCK_EXTERNAL_19MHZ’ was not declared in this scope

    case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println(“PLL with external 19.2MHz reference”); break;

    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

    C:\Users\Ianka\AppData\Local\Temp\Temp1_Arduino-MPU6050-master.zip\Arduino-MPU6050-master\MPU6050_accel_simple\MPU6050_accel_simple.ino:45:10: note: suggested alternative: ‘MPU6050_CLOCK_INTERNAL’

    case MPU6050_CLOCK_EXTERNAL_19MHZ: Serial.println(“PLL with external 19.2MHz reference”); break;

    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

    MPU6050_CLOCK_INTERNAL

    MPU6050_accel_simple:46:10: error: ‘MPU6050_CLOCK_EXTERNAL_32KHZ’ was not declared in this scope

    case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println(“PLL with external 32.768kHz reference”); break;

    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

    C:\Users\Ianka\AppData\Local\Temp\Temp1_Arduino-MPU6050-master.zip\Arduino-MPU6050-master\MPU6050_accel_simple\MPU6050_accel_simple.ino:46:10: note: suggested alternative: ‘MPU6050_CLOCK_INTERNAL’

    case MPU6050_CLOCK_EXTERNAL_32KHZ: Serial.println(“PLL with external 32.768kHz reference”); break;

    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~

    MPU6050_CLOCK_INTERNAL

    MPU6050_accel_simple:50:10: error: ‘MPU6050_CLOCK_INTERNAL_8MHZ’ was not declared in this scope

    case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println(“Internal 8MHz oscillator”); break;

    ^~~~~~~~~~~~~~~~~~~~~~~~~~~

    C:\Users\Ianka\AppData\Local\Temp\Temp1_Arduino-MPU6050-master.zip\Arduino-MPU6050-master\MPU6050_accel_simple\MPU6050_accel_simple.ino:50:10: note: suggested alternative: ‘MPU6050_CLOCK_INTERNAL’

    case MPU6050_CLOCK_INTERNAL_8MHZ: Serial.println(“Internal 8MHz oscillator”); break;

    ^~~~~~~~~~~~~~~~~~~~~~~~~~~

    MPU6050_CLOCK_INTERNAL

    MPU6050_accel_simple:54:14: error: ‘class MPU6050’ has no member named ‘getRange’; did you mean ‘getRate’?

    switch(mpu.getRange())

    ^~~~~~~~

    getRate

    MPU6050_accel_simple:56:10: error: ‘MPU6050_RANGE_16G’ was not declared in this scope

    case MPU6050_RANGE_16G: Serial.println(“+/- 16 g”); break;

    ^~~~~~~~~~~~~~~~~

    C:\Users\Ianka\AppData\Local\Temp\Temp1_Arduino-MPU6050-master.zip\Arduino-MPU6050-master\MPU6050_accel_simple\MPU6050_accel_simple.ino:56:10: note: suggested alternative: ‘MPU6050_RA_FF_DUR’

    case MPU6050_RANGE_16G: Serial.println(“+/- 16 g”); break;

    ^~~~~~~~~~~~~~~~~

    MPU6050_RA_FF_DUR

    MPU6050_accel_simple:57:10: error: ‘MPU6050_RANGE_8G’ was not declared in this scope

    case MPU6050_RANGE_8G: Serial.println(“+/- 8 g”); break;

    ^~~~~~~~~~~~~~~~

    C:\Users\Ianka\AppData\Local\Temp\Temp1_Arduino-MPU6050-master.zip\Arduino-MPU6050-master\MPU6050_accel_simple\MPU6050_accel_simple.ino:57:10: note: suggested alternative: ‘MPU6050_RA_FF_DUR’

    case MPU6050_RANGE_8G: Serial.println(“+/- 8 g”); break;

    ^~~~~~~~~~~~~~~~

    MPU6050_RA_FF_DUR

    MPU6050_accel_simple:58:10: error: ‘MPU6050_RANGE_4G’ was not declared in this scope

    case MPU6050_RANGE_4G: Serial.println(“+/- 4 g”); break;

    ^~~~~~~~~~~~~~~~

    C:\Users\Ianka\AppData\Local\Temp\Temp1_Arduino-MPU6050-master.zip\Arduino-MPU6050-master\MPU6050_accel_simple\MPU6050_accel_simple.ino:58:10: note: suggested alternative: ‘MPU6050_RA_FF_DUR’

    case MPU6050_RANGE_4G: Serial.println(“+/- 4 g”); break;

    ^~~~~~~~~~~~~~~~

    MPU6050_RA_FF_DUR

    MPU6050_accel_simple:59:10: error: ‘MPU6050_RANGE_2G’ was not declared in this scope

    case MPU6050_RANGE_2G: Serial.println(“+/- 2 g”); break;

    ^~~~~~~~~~~~~~~~

    C:\Users\Ianka\AppData\Local\Temp\Temp1_Arduino-MPU6050-master.zip\Arduino-MPU6050-master\MPU6050_accel_simple\MPU6050_accel_simple.ino:59:10: note: suggested alternative: ‘MPU6050_RA_FF_DUR’

    case MPU6050_RANGE_2G: Serial.println(“+/- 2 g”); break;

    ^~~~~~~~~~~~~~~~

    MPU6050_RA_FF_DUR

    MPU6050_accel_simple:63:20: error: ‘class MPU6050’ has no member named ‘getAccelOffsetX’; did you mean ‘getXAccelOffset’?

    Serial.print(mpu.getAccelOffsetX());

    ^~~~~~~~~~~~~~~

    getXAccelOffset

    MPU6050_accel_simple:65:20: error: ‘class MPU6050’ has no member named ‘getAccelOffsetY’; did you mean ‘getXAccelOffset’?

    Serial.print(mpu.getAccelOffsetY());

    ^~~~~~~~~~~~~~~

    getXAccelOffset

    MPU6050_accel_simple:67:22: error: ‘class MPU6050’ has no member named ‘getAccelOffsetZ’; did you mean ‘getXAccelOffset’?

    Serial.println(mpu.getAccelOffsetZ());

    ^~~~~~~~~~~~~~~

    getXAccelOffset

    C:\Users\Ianka\AppData\Local\Temp\Temp1_Arduino-MPU6050-master.zip\Arduino-MPU6050-master\MPU6050_accel_simple\MPU6050_accel_simple.ino: In function ‘void loop()’:

    MPU6050_accel_simple:74:3: error: ‘Vector’ was not declared in this scope

    Vector rawAccel = mpu.readRawAccel();

    ^~~~~~

    C:\Users\Ianka\AppData\Local\Temp\Temp1_Arduino-MPU6050-master.zip\Arduino-MPU6050-master\MPU6050_accel_simple\MPU6050_accel_simple.ino:74:3: note: suggested alternative: ‘perror’

    Vector rawAccel = mpu.readRawAccel();

    ^~~~~~

    perror

    MPU6050_accel_simple:75:10: error: expected ‘;’ before ‘normAccel’

    Vector normAccel = mpu.readNormalizeAccel();

    ^~~~~~~~~

    MPU6050_accel_simple:78:16: error: ‘rawAccel’ was not declared in this scope

    Serial.print(rawAccel.XAxis);

    ^~~~~~~~

    MPU6050_accel_simple:85:16: error: ‘normAccel’ was not declared in this scope

    Serial.print(normAccel.XAxis);

    ^~~~~~~~~

    exit status 1

    ‘class MPU6050’ has no member named ‘begin’

    This report would have more information with
    “Show verbose output during compilation”
    option enabled in File -> Preferences.

  2. David Rodriguez Reply

    I am using a different board, the MEGA2560, and I was wondering if there is a way to reassign the pin values. I notice that it says different boards will have different pin numbers, so is there a way I can simply assign the SCL and SDA pin.

  3. Ronaldo Rodrigues Luvian Reply

    I’m a layman I’m starting now, which axis would I use for the inclinometer in angles from 0 to 90 degrees, using a reset button to zero

Write A Comment

Minov

A Market Place for your Innovations

0