Displaying MPU-6050 Sensor Data on LCD 1602 or LCD2004 with Arduino
In this tutorial, we will learn how to display data from the MPU-6050 sensor on an LCD 1602 or LCD2004 using Arduino. The MPU-6050 is a versatile sensor that combines a 3-axis gyroscope and a 3-axis accelerometer, making it ideal for various applications such as motion detection and orientation. By connecting this sensor to an LCD display, we can visualize real-time sensor data, including angles and temperature.

As we progress through this project, we will cover the necessary hardware components, wiring details, and code implementation. This will help you understand how to set up the MPU-6050 and display its output on an LCD. For further clarification on the code, be sure to check out the video at (in video at 00:00).
Hardware Explained
The primary components for this project are the Arduino board, the MPU-6050 sensor, and the LCD display (either 1602 or 2004). The Arduino serves as the microcontroller that processes the data from the MPU-6050 and sends it to the LCD.
The MPU-6050 sensor uses I2C communication to send data to the Arduino. It includes an accelerometer and a gyroscope, which allow it to sense motion and orientation. The LCD display is used to show the angles derived from the sensor data. It connects to the Arduino via I2C as well, which simplifies wiring and communication.
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) |
| Temperature range | -40 to +85 °C |
| Package | QFN |
| Notes / variants | Gyroscope and accelerometer integrated |
- Ensure proper power supply within the specified voltage range.
- Use pull-up resistors on the I2C lines if necessary.
- Check the I2C address for compatibility with your setup.
- Calibrate the sensor for accurate readings.
- Be cautious with wiring to avoid short circuits.
Wiring Instructions

To wire the MPU-6050 to the Arduino, start by connecting the VCC pin of the MPU-6050 to the 5V pin on the Arduino. Next, connect the GND pin of the MPU-6050 to one of the GND pins on the Arduino. For I2C communication, connect the SDA pin of the MPU-6050 to analog pin A4 on the Arduino, and connect the SCL pin to analog pin A5.
For the LCD display, connect the VCC pin to the 5V pin on the Arduino and the GND pin to the GND pin on the Arduino as well. Connect the SDA pin of the LCD to the same A4 pin used for the MPU-6050, and the SCL pin of the LCD to the A5 pin. This way, both the MPU-6050 and the LCD share the same I2C lines, simplifying the wiring.

Ensure that the connections are secure and that there are no loose wires. If your LCD or sensor does not power on, double-check the wiring and connections.
Code Examples & Walkthrough
In the code, we start by including the necessary libraries for the MPU-6050 and the LCD:
#include
#include
#include
Here, we create instances for both the MPU-6050 and the LCD. The MPU-6050 is initialized with the `Wire` library for I2C communication, while the LCD is set up with its address and dimensions.
In the setup function, we initialize the sensor and the LCD:

void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
lcd.begin();
lcd.backlight();
lcd.clear();
}
This code sets up serial communication for debugging, initializes the I2C communication, and prepares the LCD for display. The backlight is turned on to make the display visible.
In the loop function, we continuously read data from the MPU-6050 and display it on the LCD:
void loop() {
mpu6050.update();
lcd.clear();
lcdDisplay(mpu6050.getAngleX(), mpu6050.getAngleY(), mpu6050.getAngleZ());
delay(100);
}
This snippet updates the sensor data and clears the LCD for new readings every 100 milliseconds. The `lcdDisplay` function is called to show the angles on the LCD.
For a complete understanding, please watch the corresponding video where the full code is demonstrated (in video at 00:00).
Demonstration / What to Expect
When everything is set up correctly, the LCD should display the angles for the X, Y, and Z axes in real-time. You can tilt the MPU-6050 sensor to see the changes on the display. If you encounter issues, check for reversed polarity in your wiring or ensure that the I2C addresses are correctly set.
Monitoring the values on the LCD will allow you to see how the sensor responds to movement and orientation changes. If the values seem static or incorrect, verify the connections and ensure the sensor is functioning properly.
/*
*
* 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 LCD2004-I2C display module.
*
* Library and code have been taken from:
* https://github.com/tockn/MPU6050_tockn
*
* Updated by Ahmad Shamshiri on July 07, 2018 at 13:43 in Ajax, Ontario, Canada
* for Robojax.com
* Get this code from Robojax.com
* Watch video instruction for this code at: https://youtu.be/ixe--SXemp8
*
* 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 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(false);
// initialize the LCD,
lcd.begin();
// Turn on the backlight and print a message.
lcd.backlight();
lcd.clear();
lcd.setCursor (0,0); //
lcd.print("Robojax MPU-6050");
lcd.setCursor (0,1); //
lcd.print("Please Wait - 3");
lcd.setCursor (0,1); //
delay(1000);
lcd.print("Please Wait - 2");
delay(1000);
lcd.setCursor (0,1); //
lcd.print("Please Wait - 1");
delay(1000);
}
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(
mpu6050.getAngleX(), // send angle X
mpu6050.getAngleY(), // send angle Y
mpu6050.getAngleZ() // send angle Z
);
delay(100);
}// loop end
/*
* Written by Ahmad Shamshiri for Robojax.com
* lcdDisplay(float x, float y, float z)
* displays value and title on LCD2004-I2C display
* How to use:
* just pass the three values and it will display them.
* lcdDisplay(mpu6050.getAngleX() , mpu6050.getAngleY() , mpu6050.getAngleZ() )
*/
void lcdDisplay(float x, float y, float z)
{
// Robojax.com MPU6050 Demo with LCD2004-I2C Display
lcd.setCursor (0,0); //
lcd.print("Robojax MPU-6050");
lcd.setCursor (0,1); //character zero, line 1
lcd.print("Angle X:");
lcd.setCursor (9,1); //
lcd.print(x);
lcd.setCursor (0,2); //character zero, line 2
lcd.print("Angle Y:");
lcd.setCursor (9,2); //
lcd.print(y);
lcd.setCursor (0,3); //character zero, line 3
lcd.print("Angle Z:");
lcd.setCursor (9,3); //
lcd.print(z);
}
/*
*
* This code is basic usage of the MPU-6050 accelerometer and gyroscope.
*
* This code displays data on the LCD2004 display based on
* the option you set. See below.
*
*
* Library and code have been taken from:
* https://github.com/tockn/MPU6050_tockn
*
* Updated by Ahmad Shamshiri on July 03, 2018 in Ajax, Ontario, Canada
* for Robojax.com
* Get this code from Robojax.com
* Watch video instructions for this code at: https://youtu.be/ixe--SXemp8
*
* 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);
long timer = 0;
#include <LiquidCrystal_I2C.h>
// Set the LCD address to 0x27 for a 20 chars and 4 line display
LiquidCrystal_I2C lcd(0x27, 20, 4);
int typeSelect = 6;// select type . See below
String type[]={ "GyroAng and Ang",// select 0
"GyroAng and Acc",// select 1
"GyroAng and Gyro",// select 2
"AccAng and Acc",// select 3
"AccAng & Gyro",// select 4
"AccAng and Ang",// select 5
"Angle and Temp"// select 6
};
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();
if(millis() - timer > 1000){
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();
}// if
lcd.clear();// clearn previous values from screen
switch (typeSelect)
{
case 0:// display GyroAngle and Angle
lcdDisplay(mpu6050.getGyroAngleX(),mpu6050.getGyroAngleY(),mpu6050.getGyroAngleZ(),
mpu6050.getAngleX(),mpu6050.getAngleY(),mpu6050.getAngleZ()
);
break;
case 1:// display GyroAngle and Acceleration value
lcdDisplay(mpu6050.getAngleX(),mpu6050.getAngleY(),mpu6050.getAngleZ(),
mpu6050.getAccX(),mpu6050.getAccY(),mpu6050.getAccZ()
);
break;
case 2:// display GyroAngle and Gyroscope value
lcdDisplay(mpu6050.getAngleX(),mpu6050.getAngleY(),mpu6050.getAngleZ(),
mpu6050.getGyroX(),mpu6050.getGyroY(),mpu6050.getGyroZ()
);
break;
case 3:// display Acceleration Angle and Acceleration value
lcdDisplay(mpu6050.getAccAngleX(),mpu6050.getAccAngleY(),0.0,
mpu6050.getAccX(),mpu6050.getAccY(),mpu6050.getAccZ()
);
break;
case 4:// display Acceleration Angle and Gyro value
lcdDisplay(mpu6050.getAccAngleX(),mpu6050.getAccAngleY(), 0.0,
mpu6050.getGyroX(),mpu6050.getGyroY(),mpu6050.getGyroZ()
);
break;
case 5:// display Acceleration Angle and Angle
lcdDisplay(mpu6050.getAccAngleX(),mpu6050.getAccAngleY(), 0.0,
mpu6050.getAngleX(),mpu6050.getAngleY(),mpu6050.getAngleZ()
);
break;
case 6:// display angle and Temperature
lcdDisplay(mpu6050.getAngleX(),mpu6050.getAngleY(),mpu6050.getAngleZ(),
mpu6050.getTemp(), 0.0, 0.0
);
break;
}// switch end
delay(100);
}// loop
/*
* Written by Ahmad Shamshiri for Robojax.com
* lcdDisplay(lcdDisplay(float x, float y, float z, float x1, float y1, float z1)
* displays values and titles on the LCD2004-I2C display.
* How to use:
* just pass the 6 values and it will display them.
lcdDisplay(mpu6050.getAccAngleX(),mpu6050.getAccAngleY(),mpu6050.getAccAngleZ(),
mpu6050.getAngleX(),mpu6050.getAngleY(),mpu6050.getAngleZ()
);
*/
void lcdDisplay(float x, float y, float z, float x1, float y1, float z1)
{
// Robojax.com MPU6050 Demo with LCD2004-I2C Display
lcd.setCursor (0,0); //
lcd.print(type[typeSelect]);
lcd.setCursor (17,0); //
String dis = "("+String(typeSelect)+")";
lcd.print(dis);
if(typeSelect !=6){
lcd.setCursor (0,1); //character zero, line 1
lcd.print("X:");
lcd.setCursor (2,1); //
lcd.print(x);
/////////////////////
lcd.setCursor (10,1); //character zero, line 1
lcd.print("X1:");
lcd.setCursor (13,1); //
lcd.print(x1);
lcd.setCursor (0,2); //character zero, line 2
lcd.print("Y:");
lcd.setCursor (2,2); //
lcd.print(y);
////////////////////////
lcd.setCursor (10,2); //character zero, line 1
lcd.print("Y1:");
lcd.setCursor (13,2); //
lcd.print(y1);
lcd.setCursor (0,3); //character zero, line 3
lcd.print("Z:");
lcd.setCursor (2,3); //
lcd.print(z);
//////////////////////
lcd.setCursor (10,3); //character zero, line 3
lcd.print("Z1:");
lcd.setCursor (13,3); //
lcd.print(z1);
}else{
lcd.setCursor (0,1); //character zero, line 1
lcd.print("X:");
lcd.setCursor (2,1); //
lcd.print(x);
/////////////////////
lcd.setCursor (10,1); //character zero, line 1
lcd.print("TMP: ");
lcd.setCursor (15,1); //
lcd.print(x1);
lcd.setCursor (0,2); //character zero, line 2
lcd.print("Y:");
lcd.setCursor (2,2); //
lcd.print(y);
lcd.setCursor (0,3); //character zero, line 3
lcd.print("Z:");
lcd.setCursor (2,3); //
lcd.print(z);
}
}
Ресурсы и ссылки
-
ВнешнийВеб-сайт производителяinvensense.com
-
ВнешнийПолучите библиотеку с GitHubgithub.com
-
Внутренний
Файлы📁
Библиотеки Arduino (zip)
-
Библиотека Arduino для MPU9250
robojax-MPU9250-master.zip3.38 MB
Файл Fritzing
-
LCD LCD1602-I2C module with 4 wires
LCD1602-I2C.fzpz0.01 MB -
Плата MPU-6050 GY-521
MPU-6050 Board GY-521.fzpz0.01 MB -
LCD2004-I2C
LCD2004-I2C.fzpz0.02 MB
Другие файлы
-
LCD1602 LiquidCrystal Library
robojax-LCD1602_LiquidCrystal.zip