Measure titling using MPU6050 ✈️

Have you ever traveled by plane?


Was the trip long, and do you think the pilot's profession is very difficult?


In fact, we agree with you, but you should also know that the autopilot controls the plane for most of the flight with the help of some sensors like the MPU6050, which can measure the plane's titling.


In this tutorial we will learn about a very interesting sensor called the MPU6050, which is very important in planes since it helps the autopilot control your flight in a safe manner.


Overview


The MPU6050 is a sensor that can provide us with very useful data about acceleration, velocity, and rotation along all three axes. That’s why it has a wide range of applications in drones, robots, smart phones, video game controllers, and many other devices.


Most likely, your smartphone has a sensor that is quite similar to the MPU6050. That sensor is responsible for many functions like rotating the screen when you tilt it to give you horizontal control of your phone. It is also used to control and interact with games on your smartphone!


Let’s find out more about this interesting sensor!


Getting the items


For this project you will need the following components you can buy them from our store.

Sale Off
Voltaat Arduino Uno R3 (Voltaat Version)
45 QAR
Sale Off
Voltaat Jumper Wires - Male to Female (40 Pack)
10 QAR

Wiring it up


To set up the MPU6050 sensor in the correct way, follow the instructions below. The image demonstrates how to connect the wires between the MPU6050 sensor and the Arduino. Once the MPU6050 sensor and the Arduino are connected to each other, connect the Arduino to your computer using the USB cable.



Connections from the Arduino to the MPU6050:

• Arduino 5V pin → MPU6050 VCC pin (+ pin)

• Arduino GND pin → MPU6050 GND pin (- pin)

• Arduino pin 5 → MPU6050 SCL pin

• Arduino pin 4 → MPU6050 SDA pin


Coding


The purpose of this sketch is to get readings from the MPU6050 sensor and print this data directly on your computer.


You will need to include the wire.h library for this sketch to work properly. It allows communication between the Arduino and the sensor through the I2C protocol.


We will also use a basic and easy library for the MPU6050 sensor, which will make the code extremely simple and straightforward to use.


Libraries are files that you can download and copy to the Arduino IDE software files so the Arduino can recognize different sensors. You can download the library files from the resources section and then install it by following this tutorial.


Let's put the code to the test together; you'll be able to understand it quickly by following the comments and instructions throughout the code.


/*
Voltaat learn (https://www.voltaat.com/pages/voltaat-learn)
Link for full tutorial: https://bit.ly/3UaN21X
Wire library: https://bit.ly/3zfCc2D
MPU6050 library: https://bit.ly/3gIrTOc

Tutorial: Measure titling using MPU6050

The purpose of this sketch is to get readings from the MPU6050 sensor
and print this data directly on your computer.

Connections from the Arduino to the MPU6050:
• Arduino 5V pin → MPU6050 VCC pin (+ pin)
• Arduino GND pin → MPU6050 GND pin (- pin)
• Arduino pin 5 → MPU6050 SCL pin
• Arduino pin 4 → MPU6050 SDA pin

*/


//Include MPU6050 Library
#include < MPU6050_tockn.h>
//This library allows you to connect with I2C/TWI devices
#include < Wire.h >

MPU6050 mpu6050(Wire);

//Define variable
long timer =0;

//Commands inside void setup run once
void setup(){
//Start the serial monitor at 19200 baud rate (19200 bits per second)
Serial.begin(9600);
//This function initializes the Wire library and join the I2C bus as a controller or a peripheral
Wire.begin();
//This function initializes the MPU6050 library
mpu6050.begin();
//See state of calculating calibration in serial monitor
mpu6050.calcGyroOffsets(true);
}

//Commands inside void loop run forever
void loop(){
//Get all data of MPU6050
mpu6050.update();

if(millis()- timer >1000){
//Print data to serial monitor
Serial.println("=======================================================");
Serial.print("temp : ");Serial.println(mpu6050.getTemp());
Serial.print("accX : ");Serial.print(mpu6050.getAccX());
Serial.print("\taccY : ");Serial.print(mpu6050.getAccY());
Serial.print("\taccZ : ");Serial.println(mpu6050.getAccZ());

Serial.print("gyroX : ");Serial.print(mpu6050.getGyroX());
Serial.print("\tgyroY : ");Serial.print(mpu6050.getGyroY());
Serial.print("\tgyroZ : ");Serial.println(mpu6050.getGyroZ());

Serial.print("accAngleX : ");Serial.print(mpu6050.getAccAngleX());
Serial.print("\taccAngleY : ");Serial.println(mpu6050.getAccAngleY());

Serial.print("gyroAngleX : ");Serial.print(mpu6050.getGyroAngleX());
Serial.print("\tgyroAngleY : ");Serial.print(mpu6050.getGyroAngleY());
Serial.print("\tgyroAngleZ : ");Serial.println(mpu6050.getGyroAngleZ());

Serial.print("angleX : ");Serial.print(mpu6050.getAngleX());
Serial.print("\tangleY : ");Serial.print(mpu6050.getAngleY());
Serial.print("\tangleZ : ");Serial.println(mpu6050.getAngleZ());
Serial.println("=======================================================\n");
timer =millis();

}

}

Testing it out




Now you must have correctly wired the MPU6050 sensor to the Arduino as we explained in the wiring section, as well as uploaded the code to your Arduino board.


You may now access the serial monitor on your Arduino IDE by clicking on the magnifying glass icon at the top right corner.



The serial monitor is a great tool that can facilitate communication between the computer and the Arduino. It allows us to send and receive different commands as well as view information directly from the Arduino.


Now as we see in the following image the serial monitor displays the reading values from the MPU6050 sensor.


You should also make sure you have chosen the right baud rate (9600) as specified in the code.


You can do this by clicking on the drop-down menu at the bottom right corner of the output window.


Resources 


Arduino Code

MPU6050 Library

Fritzing Wiring file

Related Tutorials


LCDs are used in a range of everyday applications, including the automobile radio and the house air conditioning remote. They display data and let you control it through menus.


A servo motor is just a simple DC motor with certain modifications. When you look at it, you'll notice that it has several gears and an electronic circuit.


In this tutorial we will use the IR remote control and the receiver module with the Arduino to receive orders when you press different buttons and display the result on your computer.