Close Menu
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
Facebook X (Twitter) Instagram
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us
Facebook X (Twitter) Instagram Pinterest YouTube LinkedIn
How To Electronics
  • Articles
    • Learn Electronics
    • Product Review
    • Tech Articles
  • Electronics Circuits
    • 555 Timer Projects
    • Op-Amp Circuits
    • Power Electronics
  • Microcontrollers
    • Arduino Projects
    • STM32 Projects
    • AMB82-Mini IoT AI Camera
    • BLE Projects
  • IoT Projects
    • ESP8266 Projects
    • ESP32 Projects
    • ESP32 MicroPython
    • ESP32-CAM Projects
    • LoRa/LoRaWAN Projects
  • Raspberry Pi
    • Raspberry Pi Projects
    • Raspberry Pi Pico Projects
    • Raspberry Pi Pico W Projects
  • Electronics Calculator
How To Electronics
Home » Measure Tilt Angle Using MPU6050 Gyro/Accelerometer & Arduino
Arduino Projects

Measure Tilt Angle Using MPU6050 Gyro/Accelerometer & Arduino

Mamtaz AlamBy Mamtaz AlamUpdated:November 26, 202313 Comments4 Mins Read
Share Facebook Twitter LinkedIn Telegram Reddit WhatsApp
Measure Angle MPU6050 Arduino
Share
Facebook Twitter LinkedIn Pinterest Email Reddit Telegram WhatsApp

Overview

In this post we will learn how to Measure Tilt Angle using MPU6050 & Arduino. This can be done by simply interfacing MPU6050 6 axis Gyro/Accelerometer Sensor. The Accelerometer sends X, Y, and Z acceleration forces. We need to convert the forces into X, Y, Z 3D angle to determine the 3D Orientation of the sensor.

The gyroscope measures rotational velocity or rate of change of the angular position over time, along the X, Y, and Z-axis. It uses MEMS technology and the Coriolis Effect for measuring. The outputs of the gyroscope are in degrees per second, so in order to get the angular position, we just need to integrate the angular velocity.



The accelerometer can measure gravitational acceleration along the 3 axes and using some trigonometry math we can calculate the angle at which the sensor is positioned. So, if we fuse, or combine the MPU6050 accelerometer and gyroscope data we can get very accurate information about the sensor orientation. Hence MPU6050 with Arduino can measure tilt angle.

Check this post:
1. Interfacing MPU6050 Accelerometer & Gyroscope with Arduino
2. DC Motor Control using MPU6050 Gyro/Accelerometer Sensor & Arduino
3. ESP8266 & MPU6050 Tilt Angle Monitor on IoT Blynk


Components Required

C++
1
2
3
4
1. Arduino Board
2. MPU6050 Gyro/Accelerometer Sensor
3. Breadboard
4. Connecting Wires


MPU6050 Gyro/Accelerometer Sensor

Introduction:

MPU6050 Gyro/Accelerometer

The InvenSense MPU-6050 sensor contains a MEMS accelerometer and a MEMS gyro in a single chip. It is very accurate, as it contains a 16-bits analog to digital conversion hardware for each channel. Therefor it captures the x, y, and z channel at the same time. The sensor uses the I2C-bus to interface with the Arduino.

The MPU-6050 is not expensive, especially given the fact that it combines both an accelerometer and a gyro.

MPU6050 Pinout:

The MPU-6050 module has 8 pins:
INT: Interrupt digital output pin.
AD0: I2C Slave Address LSB pin. This is the 0th bit in the 7-bit slave address of the device. If connected to VCC then it is read as logic one and slave address changes.
XCL: Auxiliary Serial Clock pin. This pin is used to connect other I2C interface enabled sensors SCL pin to MPU-6050.
XDA: Auxiliary Serial Data pin. This pin is used to connect other I2C interface enabled sensors SDA pin to MPU-6050.
SCL: Serial Clock pin. Connect this pin to the microcontrollers SCL pin.
SDA: Serial Data pin. Connect this pin to the microcontrollers SDA pin.
GND: Ground pin. Connect this pin to the ground connection.
VCC: Power supply pin. Connect this pin to +5V DC supply.

3-Axis Gyroscope:

The MPU6050 consists of a 3-axis Gyroscope with Micro Electro Mechanical System(MEMS) technology. It is used to detect rotational velocity along the X, Y, Z axes as shown in the below figure.
3-Axis Gyroscope


3-Axis Accelerometer:

The MPU6050 consist of 3-axis Accelerometer with Micro Electro Mechanical (MEMs) technology. It used to detect the angle of tilt or inclination along the X, Y, and Z axes as shown in the below figure.
3-Axis Accelerometer


Circuit Diagram & Connection

MPU6050 has I2C Pins. So it needs to be connected to I2C pins of Arduino. So connect SDA pins of MPU6050 to A4 of Arduino and SCL to A5. Supply a 5V input to MPU6050 and also connect the GND as shown in the figure below.

MPU6050 Arduino Connection


Source Code/Program

Below is the code to measure the tilt angle using MPU6050 & Arduino. Copy the code and upload it to the Arduino board.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<Wire.h>
const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int minVal=265;
int maxVal=402;
double x;
double y;
double z;
void setup(){
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
Serial.begin(9600);
}
void loop(){
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
int xAng = map(AcX,minVal,maxVal,-90,90);
int yAng = map(AcY,minVal,maxVal,-90,90);
int zAng = map(AcZ,minVal,maxVal,-90,90);
x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
Serial.print("AngleX= ");
Serial.println(x);
Serial.print("AngleY= ");
Serial.println(y);
Serial.print("AngleZ= ");
Serial.println(z);
Serial.println("-----------------------------------------");
delay(400);
}




Output/Results

So once the code is uploaded, you can click on the serial monitor to check the outputs. You need to tilt the MPU6050 Gyro/Accelerometer to detect the angular position of X, Y & Z-axis.

MPU6050 Angle Calculation

You can check this project as well: Measure Tilt Angle with MPU6050 & STM32 Microcontroller

Share. Facebook Twitter Pinterest LinkedIn Tumblr Email Reddit Telegram WhatsApp
Previous ArticleVoice Based Home Automation with NodeMCU & Alexa using fauxmoESP
Next Article Fan Speed Measurement using IR Sensor & Arduino

Related Posts

DC Energy Meter using Arduino

Build a DC Energy Meter using Arduino – 32V/5A

Updated:August 26, 20252K
Interfacing ADXL375 Accelerometer with Arduino

Interfacing ADXL375 Accelerometer with Arduino (±200g)

Updated:June 28, 2025
PZEM-004T Arduino Energy Meter

DIY AC Energy Meter using PZEM-004T & Arduino

Updated:March 6, 20258K
Interfacing BMI160 Accelerometer & Gyroscope with Arduino

Interfacing BMI160 Accelerometer & Gyroscope with Arduino

Updated:February 2, 20259K
Password Based Door Lock Security System Using Arduino & Keypad

Password Based Door Lock Security System Using Arduino & Keypad

Updated:February 2, 20252436K
Earthquake Detector Alarm with with Accelerometer & Arduino

Earthquake Detector Alarm with Accelerometer & Arduino

Updated:February 2, 2025661K
View 13 Comments

13 Comments

  1. FRANCIS on July 18, 2020 2:56 AM

    why we use this boundary value
    int minVal=265;
    int maxVal=402;

    Reply
  2. Chris on August 21, 2020 7:32 AM

    Can you mount the sensor anyway you want (likevertically) or does it have to be a default orientation?

    Reply
  3. Charles on August 29, 2020 9:05 PM

    Thanks
    Have run the code successfully
    Now I want to find the max and min tilt angle for repeated movements
    I can find tilt max using an ‘IF’ loop but it only provides max value not repeated max values
    tilt[i]=x; // create a ’tilt’ array

    if (tilt[i] > tiltmax) // check if tilt array value from the calculated X degrees is increasing
    tiltmax = tilt[i]; // if increasing assign the new value to tiltmax

    then print tiltmax. But it all stops when I use :
    if (tilt[i] < tiltmax) // if tilt array value is dereasing then print the value
    tiltmaxx = tilt[i];

    then print tiltmaxx
    Also the tilt array tilt[i] from a ‘FOR’ loop
    for (int i=0; i

    Reply
  4. Robin Wood on January 1, 2021 10:30 AM

    Hello, I am trying to write a ‘if’ statement to activate led and buzzer at 180deg , 50 deg, ? In that order , I would grateful if you could help
    Thank you
    Robin

    Reply
  5. Charles on January 3, 2021 10:48 PM

    Hi Robin
    I have been struggling with max and min values and working with if statements
    So in my experience the IF statement you might be able to use :

    if ( x = 180 ){
    // activate the pins for the LED and the buzzer
    }
    if ( x = 50 ){
    // activate the pins for the LED and the buzzer
    }

    This still might not give you the correct order –

    if it’s on a reducing angle after an increasing angle then you can add :

    x = current_angle;
    if ( current_angle < previous _angle && x = 180 ){
    // pins high
    }
    if ( current_angle < previous_angle && x = 50 ){
    //pins high
    }
    previous_angle = current_angle;
    }

    I am still very new to coding so unless I run the code with the prototype hardware (set-up) I will not see if I have made a mistake
    BW
    Charles

    Reply
  6. Shae Bihlmire on February 15, 2021 6:51 PM

    Do you have any other pictures of the completed bread board? The layout of your jumper wires is not easily comprehendable. I’m trying to recreate this exact project but i KNOW i don’t have the jumper wires connected correctly. PLEASE HELP WITH MORE PICTURES!

    Reply
  7. mark on February 20, 2021 4:15 PM

    H i want to built a tilt alarm for a car and use this code how would i do this as if the car was parked on a hill it would go off i need it to accept the incline of the hill while parked up thank you for any help with this project

    Reply
  8. Calete on June 21, 2021 4:58 AM

    Doesn’t work for me; I get this kind of readings no matter the position of the MPU6050; I modified the code to print the raw data for x, y, z:

    RawRead x:-17188 y:-2108 z:-26186
    AngleX= 354.04
    AngleY= 323.13

    AngleZ= 187.93

    RawRead x:-17212 y:-2134 z:-26200
    AngleX= 353.97
    AngleY= 323.08

    AngleZ= 188.00

    RawRead x:-17190 y:-2096 z:-26188
    AngleX= 354.06
    AngleY= 323.12

    AngleZ= 187.89

    RawRead x:-17176 y:-2152 z:-26198
    AngleX= 353.92
    AngleY= 323.13

    AngleZ= 188.08

    RawRead x:-17138 y:-2108 z:-26152
    AngleX= 354.04
    AngleY= 323.25
    AngleZ= 187.95

    Reply
  9. faheem on April 4, 2022 1:27 AM

    Doesn’t work for me; I get this kind of readings no matter the position of the MPU6050; I modified the code to print the raw data for x, y, z:

    RawRead x:-17188 y:-2108 z:-26186
    AngleX= 354.04
    AngleY= 323.13

    AngleZ= 187.93

    RawRead x:-17212 y:-2134 z:-26200
    AngleX= 353.97
    AngleY= 323.08

    AngleZ= 188.00

    RawRead x:-17190 y:-2096 z:-26188
    AngleX= 354.06
    AngleY= 323.12

    AngleZ= 187.89

    RawRead x:-17176 y:-2152 z:-26198
    AngleX= 353.92
    AngleY= 323.13

    AngleZ= 188.08

    RawRead x:-17138 y:-2108 z:-26152
    AngleX= 354.04
    AngleY= 323.25
    AngleZ= 187.95

    Reply
  10. Blanca on August 17, 2022 4:52 AM

    You are calculating the tilt using only the Accelerator data but I don’t see where you are using the Gyroscope data, am I missing anything?

    Reply
  11. madannagare on November 14, 2022 10:50 PM

    you can calculate angles about x and y axis only using above data but, not angle about z axis.

    Reply
  12. Greg on December 10, 2023 9:19 PM

    thanks heaps – I assume you need to calibrate this initially if measuring tilt (atitude) so horitontal is 0 degrees? If yes, how long will it remain calibrated for? Do you need to have a user interface or an approach so that it will “calibrate” each time the device is started, so that horitonal will be 0 degress?

    Reply
  13. Lior Salem on July 4, 2024 10:34 AM

    This code uses only an accelerometer; there is no data fusion here, although the post stated, “So, if we fuse, or combine the MPU6050 accelerometer and gyroscope data, we can get very accurate information about the sensor orientation.” This is very misleading.

    Reply

CommentsCancel reply

Latest Posts
IoT Based PM & Air Quality Monitoring System using ESP32

IoT Based PM & Air Quality Monitoring System using ESP32

May 31, 2026
DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

DIY ESP32 MLX90640 IR Thermal Camera with Live Web Display

May 10, 2026
IoT Activity Tracker with ESP32 & Accelerometer Gyroscope

IoT Activity Tracker with ESP32 & Accelerometer/Gyroscope

May 2, 2026
A Guide to Sourcing Obsolete ICs for Vintage Projects

Beyond AliExpress: A Guide to Sourcing Obsolete ICs for Vintage Projects

April 21, 2026

ESP32 IoT Vehicle Motion Analyzer with MPU6050 & LIS3MDL

April 27, 2026
Building a Smart Sensor Node with a BLE Microcontroller

Building a Smart Sensor Node with a BLE Microcontroller

February 26, 2026
High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

High-Accuracy Pitch, Roll, Yaw with ESP32 & BNO08x IMU

April 27, 2026
DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

DIY Colorimeter using AS7265x Spectroscopy Sensor & ESP32

February 1, 2026
Top Posts & Pages
  • 12V DC to 220V AC Inverter Circuit & PCB
    12V DC to 220V AC Inverter Circuit & PCB
  • IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
    IoT AC Energy Meter with PZEM-004T & ESP32 WebServer
  • IoT Based Drinking Water Quality Monitoring with ESP32
    IoT Based Drinking Water Quality Monitoring with ESP32
  • LD2410 Sensor with ESP32 - Human Presence Detection
    LD2410 Sensor with ESP32 - Human Presence Detection
  • ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
    ESP32 CAN Bus Tutorial | Interfacing MCP2515 CAN Module with ESP32
  • DIY IoT Water pH Meter using pH Sensor & ESP32
    DIY IoT Water pH Meter using pH Sensor & ESP32
  • Buck Converter: Basics, Working, Design & Application
    Buck Converter: Basics, Working, Design & Application
  • How to use Modbus RTU with ESP32 to read Sensor Data
    How to use Modbus RTU with ESP32 to read Sensor Data
Categories
  • Arduino Projects (197)
  • Articles (60)
    • Learn Electronics (19)
    • Product Review (15)
    • Tech Articles (28)
  • Electronics Circuits (46)
    • 555 Timer Projects (21)
    • Op-Amp Circuits (7)
    • Power Electronics (13)
  • IoT Projects (204)
    • ESP32 MicroPython (7)
    • ESP32 Projects (81)
    • ESP32-CAM Projects (15)
    • ESP8266 Projects (76)
    • LoRa/LoRaWAN Projects (22)
  • Microcontrollers (38)
    • AMB82-Mini IoT AI Camera (4)
    • BLE Projects (18)
    • STM32 Projects (19)
  • Raspberry Pi (93)
    • Raspberry Pi Pico Projects (57)
    • Raspberry Pi Pico W Projects (12)
    • Raspberry Pi Projects (24)
Follow Us
  • Facebook
  • Twitter
  • Pinterest
  • Instagram
  • YouTube
About Us

“‘How to Electronics’ is a vibrant community for electronics enthusiasts and professionals. We deliver latest insights in areas such as Embedded Systems, Power Electronics, AI, IoT, and Robotics. Our goal is to stimulate innovation and provide practical solutions for students, organizations, and industries. Join us to transform learning into a joyful journey of discovery and innovation.

Copyright © How To Electronics. All rights reserved.
  • About Us
  • Disclaimer
  • Privacy Policy
  • Contact Us
  • Advertise With Us

Type above and press Enter to search. Press Esc to cancel.

Ad Blocker Enabled!
Ad Blocker Enabled!
Looks like you're using an ad blocker. Please allow ads on our site. We rely on advertising to help fund our site.