Arduino Code for MPU-6050 with LCD1602 I2C Display

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.

LCD1602-I2C display module with 4 wires

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

ManufacturerInvenSense
Part numberMPU-6050
Logic/IO voltage3.3 V / 5 V
Supply voltage3.3 V to 5 V
Output data rate1 kHz (max)
Gyroscope range±250, ±500, ±1000, ±2000 °/s
Accelerometer range±2g, ±4g, ±8g, ±16g
PackageQFN
Notes / variantsIncludes 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

Arduino wiring for MPU-6050 with LCD

To wire the MPU-6050 and the LCD1602 display to your Arduino, follow these connections:

  • Connect the VCC pin of the MPU-6050 to the 5V pin on the Arduino.
  • Connect the GND pin of the MPU-6050 to the GND pin on the Arduino.
  • Connect the SDA pin of the MPU-6050 to the A4 pin on the Arduino.
  • Connect the SCL pin of the MPU-6050 to the A5 pin on the Arduino.
  • For the LCD1602 display, connect the VCC pin to 5V on the Arduino.
  • Connect the GND pin of the LCD1602 to the GND pin on the Arduino.
  • Connect the SDA pin of the LCD1602 to the same A4 pin used for the MPU-6050.
  • Connect the SCL pin of the LCD1602 to the same A5 pin 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).

Images

LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
LCD1602-I2C display module with 4 wires
MPU-6050-module-1
MPU-6050-module-1
MPU-6050-module-2
MPU-6050-module-2
MPU-6050-module-schematic
MPU-6050-module-schematic
MPU-6050-module-1
MPU-6050-module-1
Arduino wiring for MPU-6050 with LCD
Arduino wiring for MPU-6050 with LCD
118-Arduino code for MPU-6050 accelerometer and gyroscope sensor (all data)
Language: C++
Copied!

Things you might need

Resources & references

Files📁

Arduino Libraries (zip)

Fritzing File