MPU6050: module for positioning with Arduino

Plate MPU6050

If you need to create a project and position it, i.e. to know how it is positioned with respect to space, you can use the module MPU6050. That is, this module is an inertial measurement unit (IMU) with 6 degrees of freedom (DoF). That is thanks to the 3-axis accelerometer type sensors and a 3-axis gyroscope that it implements to work.

This MPU6050 can know how the object containing it is positioned for use in navigation, goniometry, stabilization, gesture control, etc. applications. Mobile phones usually include this type of sensor to, for example, control certain functions by gestures, such as stopping if the smartphone is turned around, driving vehicles in video games by turning the mobile as if it were a steering wheel, etc.

What are an accelerometer and a gyroscope?

MEMS examples
MEMS examples

Well, let’s go by parts. The first thing is to see what these types of sensors are that are capable of detecting acceleration and spin, as can be deduced from their own names.

  • Accelerometer: measures acceleration, that is, the variation of speed per unit of time. Remember that in physics, the change of velocity with respect to time (a=dV/dt) is the definition of acceleration. According to Newton’s Second Law, we also have that a=F/m, and that’s what accelerometers use to work, that is, they use force and mass parameters of the object. In order to implement this in electronics, MEMS (Micro Electro Mechanical Systems) techniques are used, which differ from conventional electronic chip manufacturing techniques, since mechanical parts are created in a MEMS. In this case, tracks or elements capable of measuring accelerations are created. This implies that many other units can be taken out, such as velocity (if acceleration in time is integrated), if it is integrated again you have the displacement, etc. That is, very interesting parameters to know the position or detect movement of an object.
  • Gyroscope: also called gyroscope, is a device that measures the angular velocity of an object, ie the angular displacement per unit of time or how fast a body rotates around its axis. In this case, MEMS techniques are also used to measure this speed using an effect known as Coriolis. Thanks to that you can measure the angular velocity or, by integrating the angular velocity with respect to time, you can obtain the angular displacement.

Module MPU6050

 

pinout del MPU6050

Now that you know what the accelerometer and gyros are, the MPU6050 module is an electronic board that integrates these two elements to allow you to measure these changes in the position of an element and generate a reaction. For example, that when an object moves an LED lights up, or other much more complex things.

As I said, it has 6 axes of freedom, DoF, 3 axes for the X, Y, and Z accelerometer of acceleration, and another 3 axes of the gyro to measure the angular velocity. You have to take into account not to make a mistake in the way you position the module and the direction of the rotation for the measurements, because if you make a mistake in the sign it will be a bit chaotic. Look at the following image where it specifies the direction of the axes (however, note that the PCB itself also has it printed on the side):

Datasheet MPU6050

Taking this into account and the pinout, you have more or less everything clear to start using the MPU6050. As you can see in the picture above, the connections are quite simple and allow for I2C communication to be compatible with most microcontrollers, including Arduino. The SCL and SDA pins have a pull-up resistor on the board for direct connection to the Arduino board, so you don’t have to worry about adding them yourself.

To work with both addresses on the I2C bus, you can use these ‘string’ pins and addresses:

  • AD0=1 or High (5v): for address 0x69 of I2C.
  • AD0=0 or Low (GND or Nc): for the address 0x68 of the I2C bus.

Remember that the operating voltage of the model is 3v3, but fortunately it has an integrated regulator, so you can aliemtnar with the 5v of Arduino without problem and will transform it into 3.3v.

By the way, since it has an internal resistance to GND, if this pin is not connected, the default address will be 0x68, since it will be connected to ground by default, which is interpreted as a logical 0.

Integration with Arduino

Arduino and MPU6050 connection

You can get more information about the I2C bus in this article. You know that depending on the Arduino board the pins used for the bus vary, but on the Arduino UNO it is the analog pin A4 and A5, for SDA (data) and SCL (clock) respectively. These are the only Arduino pins, along with the 5v and GND pins for powering the board, that you should use. So the connection is as simple as possible.

For the functions of the MPU6050 you can use the libraries that you can get more information about in this link for the I2C of the module and the MPU6050 bus.

Programming the Arduino board is not too easy with the MPU6050, so it is not for beginners. Also, knowing the limits of the accelerations or ranges of angles, you can calibrate it to determine what the exact movement or acceleration was. However, so that you can at least have an example of how to comment on using it, you can see this code from the example sketch for your Arduino IDE which will read the values recorded by the accelerometer and gyroscope:


// Necessary libraries:
#include "I2Cdev.h"
#include "MPU6050.h"
#include "Wire.h"

// Depending on the AD0 status, the address can be 0x68 or 0x69, to control the slave that will read through the I2C bus
MPU6050 sensor;

// RAW or raw values read from the accelerometer and gyro in the x,y,z axes
int ax, ay, az;
int gx, gy, gz;

void setup() {
  Serial.begin(57600); //Function to start the serial port with 57600 baud
  Wire.begin(); //Start for the I2C bus
  sensor.initialize(); //Initializing the sensor MPU6050

  if (sensor.testConnection()) Serial.println("Sensor started correctly");
  else Serial.println("Error when starting sensor");
}

void loop() {
  // Read the accelerations and angular velocities
  sensor.getAcceleration(&ax, &ay, &az);
  sensor.getRotation(&gx, &gy, &gz);

  // Displays the readings you are recording separated by a tab
  Serial.print("a[x and z] g[x and z]:t");
  Serial.print(ax); Serial.print("t");
  Serial.print(ay); Serial.print("t");
  Serial.print(az); Serial.print("t");
  Serial.print(gx); Serial.print("t");
  Serial.print(gy); Serial.print("t");
  Serial.println(gz);

  delay(100);
}

If you are a beginner and do not know well how to program with Arduino IDE, this will be difficult for you to understand, so you can consult our manual with the Arduino programming initiation course for free .

Leave a Comment

*

*