กลับหน้าหลัก
views
How to Use GY-521 MPU6050 Tilt Sensor with Arduino
Last updated on

How to Use GY-521 MPU6050 Tilt Sensor with Arduino


How to Use GY-521 MPU6050 Tilt Sensor with Arduino

The GY-521 module with MPU6050 chip is an IMU (Inertial Measurement Unit) that combines both Accelerometer and Gyroscope. It measures acceleration and rotation across 3 axes. Easy to use with I2C Protocol and available libraries. This tutorial covers wiring and code to read tilt values and control LEDs.

Circuit diagram showing GY-521 MPU6050 connected to Arduino UNO with 3 LEDs indicating tilt direction

Required Components

  • Arduino UNO R3
  • USB cable for code upload
  • 9V 2A Adapter (optional)
  • MB-102 Breadboard (830 points)
  • Jumper wires (M-M, M-F, F-F)
  • 220 Ohm resistors
  • Red, Yellow, Green LEDs (1 each)
  • GY-521 MPU6050 module

Wiring GY-521 MPU6050 to Arduino

GY-521 uses I2C communication. On Arduino UNO, the I2C pins are A4 (SDA) and A5 (SCL). Other boards like Mega use different pins - check your board pinout before wiring.

Arduino UNOGY-521 MPU6050
5VVCC
GNDGND
A4 (SDA)SDA
A5 (SCL)SCL

LED Connection

Arduino PinLED ColorPurpose
Pin 3RedLevel position indicator (Z-axis > 150)
Pin 4YellowPerpendicular indicator (X-axis < 50)
Pin 5GreenLeft-right tilt indicator (Y-axis < 50 or > 150)

Note: Connect 220 Ohm resistor in series with each LED to limit current and prevent damage.

Pin mapping table showing connections between Arduino UNO, GY-521 MPU6050, and indicator LEDs

Installing MPU6050 Library

Download the library from GitHub - ElectronicCats/MPU6050 or search for “MPU6050” in Arduino IDE Library Manager.

Installation via Library Manager:

  1. Open Arduino IDE
  2. Go to Sketch > Include Library > Manage Libraries
  3. Search for “MPU6050”
  4. Select the library and click Install

Arduino Code - Reading Tilt Sensor

#include "Wire.h"
#include "MPU6050.h"

MPU6050 mpu;

// LED pin definitions
const int LED_RED = 3;    // Level indicator (Z-axis)
const int LED_YELLOW = 4;  // Perpendicular indicator (X-axis)
const int LED_GREEN = 5;  // Left-right tilt indicator (Y-axis)

void setup() {
  Serial.begin(9600);
  
  // Initialize I2C
  Wire.begin();
  
  // Initialize MPU6050
  mpu.initialize();
  
  // Verify connection
  if (mpu.testConnection()) {
    Serial.println("MPU6050 initialized successfully");
  } else {
    Serial.println("MPU6050 connection failed");
  }
  
  // Set LED pins as OUTPUT
  pinMode(LED_RED, OUTPUT);
  pinMode(LED_YELLOW, OUTPUT);
  pinMode(LED_GREEN, OUTPUT);
  
  // Turn off all LEDs initially
  digitalWrite(LED_RED, LOW);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_GREEN, LOW);
}

void loop() {
  int16_t ax, ay, az;
  int16_t gx, gy, gz;
  
  // Read Accelerometer and Gyroscope values
  mpu.getMotion6(&ax, &ay, &az, &gx, &gy, &gz);
  
  // Convert Accelerometer values to g (gravity)
  // MPU6050 full-scale is ±2g, which gives approximately ±16384 ADC values
  double accelX = (double)ax / 16384.0;
  double accelY = (double)ay / 16384.0;
  double accelZ = (double)az / 16384.0;
  
  // Scale values for display (multiply by ~16000)
  int zValue = (int)(az / 109);  // Returns ~150 when level
  int xValue = (int)(ax / 109);
  int yValue = (int)(ay / 109);
  
  // Display in Serial Monitor
  Serial.print("X: ");
  Serial.print(xValue);
  Serial.print(" | Y: ");
  Serial.print(yValue);
  Serial.print(" | Z: ");
  Serial.println(zValue);
  
  // Turn off all LEDs first
  digitalWrite(LED_RED, LOW);
  digitalWrite(LED_YELLOW, LOW);
  digitalWrite(LED_GREEN, LOW);
  
  // Check tilt and light appropriate LED
  // Z > 150 = Level (Z-axis receives full gravity)
  if (zValue > 150) {
    digitalWrite(LED_RED, HIGH);
  }
  
  // X < 50 = Perpendicular to ground
  if (xValue < 50) {
    digitalWrite(LED_YELLOW, HIGH);
  }
  
  // Y < 50 or Y > 150 = Tilted to either side
  if (yValue < 50 || yValue > 150) {
    digitalWrite(LED_GREEN, HIGH);
  }
  
  delay(100);
}

Testing Procedure

  1. Complete the circuit wiring as shown
  2. Open Arduino IDE and paste the code above
  3. Select Board: Arduino UNO from Tools > Board
  4. Select the correct Port from Tools > Port
  5. Upload the code to the board
  6. Open Serial Monitor from Tools > Serial Monitor (baud rate 9600)
  7. Observe the values and LED behavior when tilting the sensor

Expected Results

Serial Monitor showing X Y Z values in different sensor orientations: level, perpendicular, and tilted left-right
Sensor PositionX ValueY ValueZ ValueLED On
Level (parallel to ground)~0~0> 150Red
Perpendicular to ground< 50~0~0Yellow
Tilted to right~0< 50~0Green
Tilted to left~0> 150~0Green

Adjustable Points: The threshold values (150, 50) in the code are reference values. Actual readings depend on each module’s calibration. If LEDs don’t light as expected, adjust the threshold values in the code to match your hardware.

Reference Video

GY-521 MPU6050 with Arduino Tutorial

อยากทำโปรเจคแบบนี้?

รับทำโปรเจค Arduino / IoT จบงานไว ส่งงานครบ พร้อมสอน

If you need Arduino project service or urgent IoT development, see full service details on the home page

จ้างทำโปรเจคเลย

ความคิดเห็น