Introduction
Accelerometers and gyroscopes are widely used sensors in robotics and automation, as they can provide valuable information about motion and orientation. The MPU6050 is a popular and affordable sensor that combines both an accelerometer and a gyroscope into a single package. By interfacing this sensor with an Arduino board, we can easily obtain real-time data about the orientation and movement of an object.
In this article, we will explore the basics of interfacing the “MPU6050 with an Arduino” board and show how to read data from the sensor using the I2C communication protocol.
What is MPU6050 Accelerometer & Gyro Sensor?
The MPU6050 is a popular sensor module that combines both an accelerometer and a gyroscope into a single package. The accelerometer measures the linear acceleration of an object in three axes (X, Y, Z), while the gyroscope measures the angular velocity of the object in the same three axes. The combination of these two sensors allows for more accurate and reliable motion tracking and orientation estimation.
Hardware Components
To interface MPU6050 Accelerometer & Gyro with Arduino, you’ll need the following hardware components to get started:
Components | Value | Qty |
---|---|---|
Arduino UNO | – | 1 |
USB Cable Type A to B | – | 1 |
DC Power for Arduino | – | 1 |
GYRO Sensor | MPU6050 | 1 |
Jumper Wires | – | 1 |
MPU6050 Pinout
Pin Name | Pin Description |
---|---|
VCC | 5 V input. You can connect 5 V on the Arduino UNO to the VCC |
GND | Ground connection |
SCL | I2C Serial clock line |
SDA | I2C Serial data line |
XDA | I2C Master data line (for connecting external sensors) |
XCL | I2C Master Clock line (for connecting external sensors) |
AD0 | I2C Slave Address LSB. You can connect it to Low or High. Hence you can have two I2C addresses for MPU6050 ICs on your board |
INT | Interrupt Digital Input |
MPU6050 Arduino Circuit
Make connections according to the circuit diagram given below.
Wiring / Connections
Arduino | GYRO sensor |
---|---|
3V3 | VCC |
GND | GND |
A4 | SDA |
A5 | SCL |
Installing Arduino IDE
First, you need to install Arduino IDE Software from its official website Arduino. Here is a simple step-by-step guide on “How to install Arduino IDE“.
Installing Libraries
Before you start uploading a code, download and unzip the following libraries at /Program Files(x86)/Arduino/Libraries (default), in order to use the sensor with the Arduino board. Here is a simple step-by-step guide on “How to Add Libraries in Arduino IDE“.
Code
Now copy the following code and upload it to Arduino IDE Software.
#include <Adafruit_MPU6050.h>
#include <Adafruit_Sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
void setup(void) {
Serial.begin(115200);
while (!Serial)
delay(10); // will pause Zero, Leonardo, etc until serial console opens
Serial.println("Adafruit MPU6050 test!");
// Try to initialize!
if (!mpu.begin()) {
Serial.println("Failed to find MPU6050 chip");
while (1) {
delay(10);
}
}
Serial.println("MPU6050 Found!");
mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
Serial.print("Accelerometer range set to: ");
switch (mpu.getAccelerometerRange()) {
case MPU6050_RANGE_2_G:
Serial.println("+-2G");
break;
case MPU6050_RANGE_4_G:
Serial.println("+-4G");
break;
case MPU6050_RANGE_8_G:
Serial.println("+-8G");
break;
case MPU6050_RANGE_16_G:
Serial.println("+-16G");
break;
}
mpu.setGyroRange(MPU6050_RANGE_2000_DEG);
Serial.print("Gyro range set to: ");
switch (mpu.getGyroRange()) {
case MPU6050_RANGE_250_DEG:
Serial.println("+- 250 deg/s");
break;
case MPU6050_RANGE_500_DEG:
Serial.println("+- 500 deg/s");
break;
case MPU6050_RANGE_1000_DEG:
Serial.println("+- 1000 deg/s");
break;
case MPU6050_RANGE_2000_DEG:
Serial.println("+- 2000 deg/s");
break;
}
mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
Serial.print("Filter bandwidth set to: ");
switch (mpu.getFilterBandwidth()) {
case MPU6050_BAND_260_HZ:
Serial.println("260 Hz");
break;
case MPU6050_BAND_184_HZ:
Serial.println("184 Hz");
break;
case MPU6050_BAND_94_HZ:
Serial.println("94 Hz");
break;
case MPU6050_BAND_44_HZ:
Serial.println("44 Hz");
break;
case MPU6050_BAND_21_HZ:
Serial.println("21 Hz");
break;
case MPU6050_BAND_10_HZ:
Serial.println("10 Hz");
break;
case MPU6050_BAND_5_HZ:
Serial.println("5 Hz");
break;
}
Serial.println("");
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);
}
Code Explanation
This Arduino code uses the Adafruit_MPU6050 library to interface with the MPU6050 accelerometer and gyro sensor. In the setup function, it initializes the sensor and sets its accelerometer range, gyro range, and filter bandwidth. In the loop function, it continuously reads data from the sensor, including the acceleration in the X, Y, and Z directions, the rotation in the X, Y, and Z directions, and the temperature. It then prints out these values to the serial monitor. The delay function is used to control the rate at which data is read from the sensor. This code allows the user to easily obtain real-time data about the orientation and movement of an object using the MPU6050 sensor and an Arduino board.
Applications
Here are some applications of the MPU6050 accelerometer and gyro sensor:
- Motion tracking in gaming and virtual reality systems
- Orientation estimation for drones, robots, and other autonomous vehicles
- Stabilization of camera gimbals and other photography equipment
- Vibration monitoring in industrial machinery
- Motion sensing for sports and fitness tracking devices
- Balance and posture Assessment in Healthcare and Rehabilitation
- Seismic monitoring for earthquake detection and analysis
- Impact detection and analysis in sports and automotive safety systems
- Navigation and attitude control in aerospace and satellite technology.
Conclusion
The MPU6050 accelerometer and gyroscope sensor with an Arduino board can be a valuable tool for robotics and automation projects. By following the steps outlined in this article, you can easily obtain real-time data about the orientation and movement of your object. The MPU6050 sensor is affordable, widely available, and easy to use, making it a great option for beginners and professionals alike.