Arduino Code for MPU-6050 with LCD1602 I2C Display
In this tutorial, we will learn how to interface the MPU-6050 accelerometer and gyroscope with an LCD1602 display using I2C communication. The MPU-6050 will provide angle measurements that will be displayed on the LCD. This project will help you visualize the angles of the MPU-6050 in real-time, enhancing your understanding of sensor data and display interfacing.


By the end of this tutorial, you will have a working setup that displays the X, Y, and Z angles on the LCD screen. This is a great way to get hands-on experience with sensor data and display technology in Arduino projects. For further clarification, be sure to check the video at (in video at 00:00).
Hardware Explained
The main components of this project are the MPU-6050 sensor and the LCD1602 display module. The MPU-6050 is a versatile sensor that combines a 3-axis accelerometer and a 3-axis gyroscope, providing real-time angle measurements. It communicates over I2C, allowing seamless integration with the Arduino.
The LCD1602 display uses the I2C protocol as well, which simplifies wiring and reduces the number of pins needed for communication. The I2C interface allows multiple devices to be connected on the same bus, making it efficient for projects with multiple sensors or components.
Datasheet Details
| Manufacturer | InvenSense |
|---|---|
| Part number | MPU-6050 |
| Logic/IO voltage | 3.3 V / 5 V |
| Supply voltage | 3.3 V to 5 V |
| Output data rate | 1 kHz (max) |
| Gyroscope range | ±250, ±500, ±1000, ±2000 °/s |
| Accelerometer range | ±2g, ±4g, ±8g, ±16g |
| Package | QFN |
| Notes / variants | Includes Digital Motion Processor (DMP) |
- Check the I2C address (default is 0x68).
- Ensure proper power supply voltage (3.3 V or 5 V).
- Use pull-up resistors for SDA and SCL lines if not already on the board.
- Consider using decoupling capacitors near the power pins for stability.
- Verify wiring connections to avoid communication issues.
Wiring Instructions

To wire the MPU-6050 and the LCD1602 display to your Arduino, follow these connections:
- Connect the
VCCpin of the MPU-6050 to the5Vpin on the Arduino. - Connect the
GNDpin of the MPU-6050 to theGNDpin on the Arduino. - Connect the
SDApin of the MPU-6050 to theA4pin on the Arduino. - Connect the
SCLpin of the MPU-6050 to theA5pin on the Arduino. - For the LCD1602 display, connect the
VCCpin to5Von the Arduino. - Connect the
GNDpin of the LCD1602 to theGNDpin on the Arduino. - Connect the
SDApin of the LCD1602 to the sameA4pin used for the MPU-6050. - Connect the
SCLpin of the LCD1602 to the sameA5pin used for the MPU-6050.
Make sure to double-check your connections, as incorrect wiring may lead to device malfunction or failure to communicate properly.
Code Examples & Walkthrough
The code begins by including the necessary libraries for the MPU-6050 and the LCD display. The following excerpt shows how to initialize the MPU-6050:
MPU6050 mpu6050(Wire);
Here, we instantiate the MPU6050 object, which will be used to read data from the sensor. Next, we need to set up the device in the setup() function:
void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
lcd.begin();
lcd.backlight();
}
This code initializes the serial communication, the I2C bus, and the MPU-6050 sensor. It also initializes the LCD and turns on its backlight. The loop contains the logic to update and display angles:
void loop() {
mpu6050.update();
lcd.clear();
lcdDisplay(0, 0, "X:", 2, 0, mpu6050.getAngleX());
lcdDisplay(13, 0, "Y:", 0, 1, mpu6050.getAngleY());
lcdDisplay(7, 1, "Z:", 9, 1, mpu6050.getAngleZ());
delay(100);
}
In this loop, the angles for X, Y, and Z are updated and displayed on the LCD. The function lcdDisplay() is utilized to format the output correctly. You can find the full code loaded below the article.
Demonstration / What to Expect
When the setup is complete and the code is uploaded, you should see the angles for X, Y, and Z displayed on the LCD in real-time. If everything is wired correctly, the display will refresh every 100 milliseconds, showing the current orientation of the MPU-6050.
Common pitfalls include ensuring that the I2C connections are correct and that the power supply is stable. If the MPU-6050 is not properly initialized, you may see incorrect or no data on the LCD (in video at 01:15).
/*
*
* This code is basic usage of the MPU-6050 accelerometer and gyroscope.
* Running this code, you will get angles only.
* The angles at X, Y, and Z are displayed on the LCD1602-I2C display module.
*
* Library and code have been taken from:
* https://github.com/tockn/MPU6050_tockn
*
* Updated by Ahmad Shamshiri on July 05, 2018 at 22:19 in Ajax, Ontario, Canada
* for Robojax.com
* Get this code from Robojax.com
* Watch video instructions for this code at: https://youtu.be/uIz6WIis4dc
*
* You will need to watch two videos before following the instructions in this video:
* 1-MPU6050 Introduction video and code: https://youtu.be/uhh7ik02aDc
* 2-LCD1602 with I2C module video and code: https://youtu.be/q9YC_GVHy5A
*/
#include <MPU6050_tockn.h>
#include <Wire.h>
MPU6050 mpu6050(Wire);
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 16 chars and 2 line display
LiquidCrystal_I2C lcd(0x27, 16, 2);
void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
// initialize the LCD,
lcd.begin();
// Turn on the backlight and print a message.
lcd.backlight();
}
void loop() {
mpu6050.update();
Serial.print("angleX : ");
Serial.print(mpu6050.getAngleX());
Serial.print("\tangleY : ");
Serial.print(mpu6050.getAngleY());
Serial.print("\tangleZ : ");
Serial.println(mpu6050.getAngleZ());
lcd.clear();// clearn previous values from screen
lcdDisplay(
// to print X:
0, // character 0
0, // line 0
"X:",
// to print AngleX
2, // character 2
0, // line 0
mpu6050.getAngleX()
);
lcdDisplay(
// to print Y:
13, // character 13
0, // line 0
"Y:",
// to print AngleY
0, // character 0
1, // line 1
mpu6050.getAngleY()
);
lcdDisplay(
// to print Z:
7, // character 7
1, // line 1
"Z:",
// to print AngleZ
9, // character 9
1, // line 0
mpu6050.getAngleZ()
);
delay(100);
}// loop end
/*
* lcdDisplay(int tc, int tr, String title, int vc, int vr, float value)
* displays value and title on LCD1602
* How to use:
* If you want to display: "Voltage: 13.56mV" starting from the first character
* on the second row.
* use:
* lcdDisplay(0, 1, "Voltage: ", 13.56)
*
* tc is the character number (0)
* tr is the row number in the LCD (1)
* title is the text ("Voltage:")
* vc is the character number for the value
* vr is the row number for the value
* value is the value (13.56)
*/
void lcdDisplay(int tc, int tr, String title, int vc, int vr, float value)
{
// Robojax.com MPU6050 Demo
lcd.setCursor (tc,tr); //
lcd.print(title);
lcd.setCursor (vc,vr); //
lcd.print(value);
}
Things you might need
-
AmazonPurchase LCD1602-I2C from Amazonamzn.to
-
AmazonPurchase MPU-6050 from Amazonamzn.to
-
eBayPurchase LCD1602-I2C from eBayebay.us
-
AliExpressPurchase 10pcs LCD1602-I2C from AliExpresss.click.aliexpress.com
Resources & references
-
ExternalManufacturer websiteinvensense.com
Files📁
Arduino Libraries (zip)
-
LCD1602 LCD Arduino library from Robojax
robojax-LCD1602-I2C-library-master.zip0.01 MB -
Arduino library for MPU9250
robojax-MPU9250-master.zip3.38 MB
Fritzing File
-
LCD LCD1602-I2C module with 4 wires
LCD1602-I2C.fzpz0.01 MB -
MPU-6050 Board GY-521
MPU-6050 Board GY-521.fzpz0.01 MB