In this advanced tutorial, you will learn how to use the MPU6050 sensor with the Arduino. We will also download the library and other required things.
Contents:
1. Introduction
The MPU6050 IMU has both 3-Axis accelerometer and 3-Axis gyroscope integrated on a single chip. So it is a versatile sensor used in various DIY projects like the self balancing robot, gesture control car etc. It gives the output with accelerations and gyro of all three axes. With that, we can also deduce the yaw, pitch,raw. Let’s explore more!
2. Materials Required
- MPU6050 sensor
- Arduino board (Uno/nano)
- Jumper wires
3. The Circuit
The circuit is quite simple and straight forward. After connecting the power supplies, you need to connect the SCL and SDA pins too. In Arduino Uno/ Nano, they are basically A5 and A4 pins. Consider the circuit given below.
4. Using the library
While using the MPU6050 library, you need to use an external library named i2cDev or MPU6050. Click here to download the library. Install the library successfully. After this, go to it’s examples and open the MPU6050_raw named code in the Arduino IDE. You can also refer the code below. But remember to download the library.
5. Arduino Code
#include "I2Cdev.h"
#include "MPU6050.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
MPU6050 accelgyro;
//MPU6050 accelgyro(0x69); // <-- use for AD0 high
int16_t ax, ay, az;
int16_t gx, gy, gz;
#define OUTPUT_READABLE_ACCELGYRO
#define LED_PIN 13
bool blinkState = false;
void setup() {
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
// initialize serial communication
// (38400 chosen because it works as well at 8MHz as it does at 16MHz, but
// it's really up to you depending on your project)
Serial.begin(38400);
// initialize device
Serial.println("Initializing I2C devices...");
accelgyro.initialize();
// verify connection
Serial.println("Testing device connections...");
Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
}
void loop() {
// read raw accel/gyro measurements from device
accelgyro.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
// these methods (and a few others) are also available
//accelgyro.getAcceleration(&ax, &ay, &az);
//accelgyro.getRotation(&gx, &gy, &gz);
#ifdef OUTPUT_READABLE_ACCELGYRO
// display tab-separated accel/gyro x/y/z values
Serial.print("a/g:\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);
#endif
#ifdef OUTPUT_BINARY_ACCELGYRO
Serial.write((uint8_t)(ax >> 8)); Serial.write((uint8_t)(ax & 0xFF));
Serial.write((uint8_t)(ay >> 8)); Serial.write((uint8_t)(ay & 0xFF));
Serial.write((uint8_t)(az >> 8)); Serial.write((uint8_t)(az & 0xFF));
Serial.write((uint8_t)(gx >> 8)); Serial.write((uint8_t)(gx & 0xFF));
Serial.write((uint8_t)(gy >> 8)); Serial.write((uint8_t)(gy & 0xFF));
Serial.write((uint8_t)(gz >> 8)); Serial.write((uint8_t)(gz & 0xFF));
#endif
// blink LED to indicate activity
blinkState = !blinkState;
digitalWrite(LED_PIN, blinkState);
}
6. Inferring the output
I hope you were able to complete this project. In the output you will get accelerations of 3 axes, namely ax, ay, az. Also, you will get gyro data of those three axes. They can also be used to visualize it in 3D space. This task is done using a special software called “Processing”. One of the other main application of MPU6050 is the Gimbal camera. This is used in camera stabilization. I hope you will explore more about the sensor. You can also refer it’s datasheet for other technical doubts.
I hope you were able to complete the project. Comment if you face any sort of doubt. Happy Learning!