Hiển thị dữ liệu cảm biến MPU-6050 trên LCD 1602 hoặc LCD2004 với Arduino
Trong hướng dẫn này, chúng ta sẽ học cách hiển thị dữ liệu từ cảm biến MPU-6050 trên màn hình LCD 1602 hoặc LCD2004 sử dụng Arduino. MPU-6050 là một cảm biến đa năng kết hợp con quay hồi chuyển 3 trục và gia tốc kế 3 trục, khiến nó trở nên lý tưởng cho nhiều ứng dụng như phát hiện chuyển động và định hướng. Bằng cách kết nối cảm biến này với màn hình LCD, chúng ta có thể trực quan hóa dữ liệu cảm biến theo thời gian thực, bao gồm góc và nhiệt độ.

Khi tiến hành dự án này, chúng ta sẽ đề cập đến các thành phần phần cứng cần thiết, chi tiết đấu dây và triển khai mã. Điều này sẽ giúp bạn hiểu cách thiết lập MPU-6050 và hiển thị đầu ra của nó trên LCD. Để làm rõ hơn về mã, hãy xem video tại (trong video ở phút 00:00).
Giải thích phần cứng
Các thành phần chính cho dự án này là bo mạch Arduino, cảm biến MPU-6050 và màn hình LCD (1602 hoặc 2004). Arduino đóng vai trò là bộ vi điều khiển xử lý dữ liệu từ MPU-6050 và gửi đến LCD.
Cảm biến MPU-6050 sử dụng giao tiếp I2C để gửi dữ liệu đến Arduino. Nó bao gồm một gia tốc kế và một con quay hồi chuyển, cho phép cảm nhận chuyển động và định hướng. Màn hình LCD được sử dụng để hiển thị các góc suy ra từ dữ liệu cảm biến. Nó kết nối với Arduino qua I2C, giúp đơn giản hóa việc đấu dây và truyền thông.
Chi tiết bảng dữ liệu
| Nhà sản xuất | Invensense |
|---|---|
| Số bộ phận | MPU-6050 |
| Điện áp logic/IO | 3,3 V / 5 V |
| Điện áp cung cấp | 3,3 V đến 5 V |
| Tốc độ dữ liệu đầu ra | 1 kHz (tối đa) |
| Dải nhiệt độ | -40 đến +85 °C |
| Đóng gói | QFN |
| Ghi chú / biến thể | Tích hợp con quay hồi chuyển và gia tốc kế |
- Đảm bảo nguồn điện phù hợp trong dải điện áp quy định.
- Sử dụng điện trở kéo lên trên các đường I2C nếu cần.
- Kiểm tra địa chỉ I2C để tương thích với thiết lập của bạn.
- Hiệu chỉnh cảm biến để có số đọc chính xác.
- Cẩn thận với việc đấu dây để tránh đoản mạch.
Hướng dẫn đấu dây

Để đấu dây MPU-6050 với Arduino, bắt đầu bằng cách kết nối chân VCC của MPU-6050 với chân 5V trên Arduino. Tiếp theo, kết nối chân GND của MPU-6050 với một trong các chân GND trên Arduino. Để giao tiếp I2C, kết nối chân SDA của MPU-6050 với chân analog A4 trên Arduino và kết nối chân SCL với chân analog A5.
Đối với màn hình LCD, kết nối chân VCC với chân 5V trên Arduino và chân GND với chân GND trên Arduino. Kết nối chân SDA của LCD với cùng chân A4 được sử dụng cho MPU-6050 và chân SCL của LCD với chân A5. Bằng cách này, cả MPU-6050 và LCD chia sẻ cùng các đường I2C, đơn giản hóa việc đấu dây.

Đảm bảo các kết nối chắc chắn và không có dây lỏng lẻo. Nếu LCD hoặc cảm biến của bạn không bật nguồn, hãy kiểm tra lại việc đấu dây và kết nối.
Ví dụ mã & hướng dẫn chi tiết
Trong mã, chúng ta bắt đầu bằng cách bao gồm các thư viện cần thiết cho MPU-6050 và LCD:
#include
#include
#include
Ở đây, chúng ta tạo các đối tượng cho cả MPU-6050 và LCD. MPU-6050 được khởi tạo với thư viện `Wire` để giao tiếp I2C, trong khi LCD được thiết lập với địa chỉ và kích thước của nó.
Trong hàm setup, chúng ta khởi tạo cảm biến và LCD:

void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
lcd.begin();
lcd.backlight();
lcd.clear();
}
Mã này thiết lập giao tiếp nối tiếp để gỡ lỗi, khởi tạo giao tiếp I2C và chuẩn bị LCD để hiển thị. Đèn nền được bật để làm cho màn hình hiển thị rõ ràng.
Trong hàm loop, chúng ta liên tục đọc dữ liệu từ MPU-6050 và hiển thị trên LCD:
void loop() {
mpu6050.update();
lcd.clear();
lcdDisplay(mpu6050.getAngleX(), mpu6050.getAngleY(), mpu6050.getAngleZ());
delay(100);
}
Đoạn mã này cập nhật dữ liệu cảm biến và xóa LCD để có số đọc mới mỗi 100 mili giây. Hàm `lcdDisplay` được gọi để hiển thị các góc trên LCD.
Để hiểu đầy đủ, vui lòng xem video tương ứng nơi toàn bộ mã được trình bày (trong video ở phút 00:00).
Trình diễn / Điều gì sẽ xảy ra
Khi mọi thứ được thiết lập chính xác, LCD sẽ hiển thị các góc cho trục X, Y và Z theo thời gian thực. Bạn có thể nghiêng cảm biến MPU-6050 để thấy các thay đổi trên màn hình. Nếu gặp sự cố, hãy kiểm tra phân cực đảo ngược trong đấu dây hoặc đảm bảo địa chỉ I2C được đặt chính xác.
Giám sát các giá trị trên LCD sẽ cho phép bạn thấy cách cảm biến phản ứng với chuyển động và thay đổi định hướng. Nếu các giá trị có vẻ tĩnh hoặc không chính xác, hãy xác minh các kết nối và đảm bảo cảm biến hoạt động bình thường.
/*
*
* 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);
}
}
Tài nguyên & tài liệu tham khảo
-
Bên ngoàiGet the library from GitHubgithub.com
-
Bên ngoàiManufacturer websiteinvensense.com
Tập tin📁
Thư viện Arduino (tệp nén)
-
Arduino library for MPU9250
robojax-MPU9250-master.zip3.38 MB
Tập tin Fritzing
-
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 -
LCD2004-I2C
LCD2004-I2C.fzpz0.02 MB
Các tập tin khác
-
LCD1602 LiquidCrystal Library
robojax-LCD1602_LiquidCrystal.zip -
MPU6050_tockn-masterArduino library for the MPU6050 accelerometer and gyroscope module. Provides functions to read acceleration, gyro, and temperature data, with calibration and angle calculation. Designed for easy integration into motion-sensing projects.
robojax-MPU6050_tockn-master.zip