MPU6050 Accelerometer: Seamless Connection to Arduino Explained

by AKP in Circuits > Arduino

129 Views, 4 Favorites, 0 Comments

MPU6050 Accelerometer: Seamless Connection to Arduino Explained

mpu6050-accel-gyro-arduino-tutorial.png

During our childhood, the gyroscopes showcased at science fairs never ceased to captivate us with their peculiar movements, seemingly defying gravity. Their distinctive qualities render them indispensable in various applications, ranging from small RC helicopters to the sophisticated navigation systems aboard space shuttles.

In recent times, innovative engineers have successfully developed micromachined gyroscopes known as MEMS (microelectromechanical system) gyroscopes. These advancements have opened doors to a host of creative applications, including gesture recognition, enhanced gaming, augmented reality, panoramic photo capture, vehicle navigation, and fitness monitoring, among others.

Undoubtedly, gyroscopes and accelerometers exhibit exceptional qualities individually. However, when combined, they provide incredibly precise data about an object’s orientation. This is where the MPU6050 comes into play. The MPU6050 incorporates both a gyroscope and an accelerometer, enabling the measurement of rotation along all three axes, static acceleration due to gravity, and dynamic acceleration due to motion.

Before incorporating the MPU6050 Accelerometer into our Arduino project, it is beneficial to comprehend the workings of accelerometers and gyroscopes.

How Does an Accelerometer Operate?

Accelerometer-Working-Illustration-Weightless-State.jpg
Accelerometer-Working-Illustration-Sudden-Movement.jpg
Accelerometer-Working-Illustration-Gravitation-Force.jpg

To comprehend the functioning of accelerometers, envision a ball situated within a 3D cube.

In the scenario where the cube exists in outer space, where weightlessness prevails, the ball will merely hover at the center of the cube.

Now, consider each wall representing a distinct axis.

If we abruptly shift the box to the left with an acceleration of 1g (where a single G-force 1g equals gravitational acceleration of 9.8 m/s²), the ball will inevitably collide with wall X. By measuring the force exerted by the ball on wall X, we can derive an output value of 1g along the X axis.

Let’s explore the situation when we place the cube on Earth. The ball will simply fall onto wall Z, exerting a force of 1g, as depicted in the diagram below:

In this instance, although the box remains stationary, we still register a 1g reading on the Z axis. This is because gravity, a form of acceleration, is pulling the ball downward with a force of 1g.

While this model doesn’t precisely mirror the construction of a real-world accelerometer sensor, it proves useful in understanding why an accelerometer’s output signal is typically specified in ±g, or why an accelerometer records 1g in the z-axis when at rest, and what accelerometer readings one can anticipate in various orientations.

In reality, accelerometers utilize Micro-Electro-Mechanical Systems (MEMS fabrication technology). Let’s delve into how a MEMS accelerometer operates.

How Does a MEMS Accelerometer Function?

MEMS-Accelerometer-Working.gif

A MEMS (Micro-Electro-Mechanical System) accelerometer is a micro-machined structure constructed atop a silicon wafer.

This structure is suspended by polysilicon springs, enabling it to deflect when subjected to acceleration along the X, Y, and/or Z axes.

Upon deflection, the capacitance between fixed plates and those attached to the suspended structure undergoes alteration. This change in capacitance is directly proportional to the acceleration along the respective axis.

The sensor interprets this variation in capacitance and translates it into an analog output voltage.

MPU6050 Accelerometer Module Pinout

MPU6050-3-axis-Accelerometer-Gyroscope-Module-Pinout.png

The pinout for the MPU6050 module is outlined as follows:

  • VCC: Supplies power to the module.
  • GND: Ground pin.
  • SCL: Serial clock pin for the I2C interface.
  • SDA: Serial data pin for the I2C interface.
  • XDA: External I2C data line. This facilitates the connection of external sensors, such as a magnetometer, via the external I2C bus.
  • XCL: External I2C clock line.
  • AD0: Permits the adjustment of the I2C address for the MPU6050 module. This feature helps prevent conflicts with other I2C devices or allows the connection of two MPU6050s to the same I2C bus. When the ADO pin is unconnected, the default I2C address is 0x68HEX; connecting it to 3.3V changes the I2C address to 0x69HEX.
  • INT: Interrupt Output pin. The MPU6050 can be programmed to generate an interrupt upon detecting gestures, panning, zooming, scrolling, tap detection, and shake detection.



Connecting an MPU6050 Module to an Arduino

Wiring-MPU6050-Accel-Gyro-Module-with-Arduino.jpg

The wiring process is simple. Start by linking the VCC pin to the 5V output on the Arduino and the GND pin to the ground.

Next, focus on the pins designated for I2C communication. It's important to note that different Arduino boards have distinct I2C pins that must be connected accurately. On Arduino boards featuring the R3 layout, the SDA (data line) and SCL (clock line) can be found on the pin headers near the AREF pin. They are alternatively labeled as A5 (SCL) and A4 (SDA).

Arduino Example Code

Here is a simple program that reads the linear acceleration, angular rotation, and temperature from the MPU6050 module and prints them on the serial monitor.

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>


Adafruit_MPU6050 mpu;


void setup(void) {
Serial.begin(115200);


// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
  delay(10);
}
}
Serial.println("MPU6050 Found!");


// set accelerometer range to +-8G
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);


// set gyro range to +- 500 deg/s
mpu.setGyroRange(MPU6050_RANGE_500_DEG);


// set filter bandwidth to 21 Hz
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);


delay(100);
}


void loop() {
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);


/* Print out the values */
Serial.print("Acceleration X: ");
Serial.print(a.acceleration.x);
Serial.print(", Y: ");
Serial.print(a.acceleration.y);
Serial.print(", Z: ");
Serial.print(a.acceleration.z);
Serial.println(" m/s^2");


Serial.print("Rotation X: ");
Serial.print(g.gyro.x);
Serial.print(", Y: ");
Serial.print(g.gyro.y);
Serial.print(", Z: ");
Serial.print(g.gyro.z);
Serial.println(" rad/s");


Serial.print("Temperature: ");
Serial.print(temp.temperature);
Serial.println(" degC");


Serial.println("");
delay(500);
}

Make sure you set the baud rate to “115200” in the serial port monitor. Because the MPU6050 returns an excessive amount of data, this higher speed is required to display it.

There will be a lot of information displayed, such as linear acceleration, angular rotation, and temperature. Move your sensor around and observe how the data changes.

Arduino Example Code – Plotting MPU6050 Data

Simply looking at the raw data from the MPU6050 Accelerometer will not help. Use a Serial Plotter if you really want to see how your MPU6050 reacts when you move it around.

The Arduino IDE includes a useful tool called the serial plotter. It can provide real-time visualizations of variables. This is extremely useful for visualizing data, debugging code, and visualizing variables as waveforms.

Let’s give it a shot with the updated code below. Compile and upload the program below, then navigate to Tools > Serial Plotter (Ctrl+Shift+L). The code uses a baud rate of 115200; ensure that the serial plotter is also set to 115200.

#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>


Adafruit_MPU6050 mpu;


void setup(void) {
Serial.begin(115200);


// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
  delay(10);
}
}


// set accelerometer range to +-8G
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);


// set gyro range to +- 500 deg/s
mpu.setGyroRange(MPU6050_RANGE_500_DEG);


// set filter bandwidth to 21 Hz
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);


delay(100);
}


void loop() {
/* Get new sensor events with the readings */
sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);


/* Print out the values */
Serial.print(a.acceleration.x);
Serial.print(",");
Serial.print(a.acceleration.y);
Serial.print(",");
Serial.print(a.acceleration.z);
Serial.print(", ");
Serial.print(g.gyro.x);
Serial.print(",");
Serial.print(g.gyro.y);
Serial.print(",");
Serial.print(g.gyro.z);
Serial.println("");


delay(10);
}

When you move the module up and down the Z axis, you should see something like this.


See Full Article Here