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).
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
application/zip3.38 MB
Fritzing File
-
LCD LCD1602-I2C module with 4 wires
application/zip0.01 MB -
MPU-6050 Board GY-521
application/zip0.01 MB