搜索代码

MPU-6050 的 Arduino 代码与 LCD1602 I2C 显示器

MPU-6050 的 Arduino 代码与 LCD1602 I2C 显示器

在本教程中,我们将学习如何通过I2C通信将MPU-6050加速度计和陀螺仪与LCD1602显示屏连接起来。MPU-6050将提供角度测量,这些测量将显示在LCD上。该项目将帮助您实时可视化MPU-6050的角度,增强您对传感器数据和显示接口的理解。

LCD1602-I2C display module with 4 wires

在本教程结束时,您将拥有一个工作设置,能够在LCD屏幕上显示X、Y和Z角度。这是一个在Arduino项目中获得传感器数据和显示技术实际经验的好方法。如需进一步说明,请确保查看视频(在视频中,时间为00:00)。

硬件说明

该项目的主要组件是MPU-6050传感器和LCD1602显示模块。MPU-6050是一种多功能传感器,结合了三轴加速度计和三轴陀螺仪,提供实时角度测量。它通过I2C通信,允许与Arduino的无缝集成。

LCD1602 显示器也使用 I2C 协议,这简化了接线并减少了通信所需的引脚数量。I2C 接口允许多个设备连接到同一总线上,使其在需要多个传感器或组件的项目中更加高效。

数据表详情

制造商InvenSense
零件号MPU-6050
逻辑/IO电压3.3 伏 / 5 伏
供电电压3.3 V 到 5 V
输出数据速率1 kHz(最大)
陀螺仪范围±250、±500、±1000、±2000 °/s
加速度计范围±2g,±4g,±8g,±16g
包裹QFN
备注 / 变体包括数字运动处理器(DMP)

  • 检查I2C地址(默认是0x68)。
  • 确保电源电压正确(3.3 V 或 5 V)。
  • 如果电路板上没有,请为SDA和SCL线路使用上拉电阻。
  • 考虑在电源引脚附近使用解耦电容以提高稳定性。
  • 检查接线连接以避免通信问题。

接线说明

Arduino wiring for MPU-6050 with LCD

要将 MPU-6050 和 LCD1602 显示器连接到您的 Arduino,请按照以下连接方式进行:

  • 连接这个VCCMPU-6050的引脚到5V在Arduino上的引脚。
  • 连接上GNDMPU-6050的引脚到GNDArduino上的引脚。
  • 连接SDAMPU-6050 的引脚到A4Arduino上的引脚。
  • 连接SCLMPU-6050的引脚到A5Arduino上的引脚。
  • 对于LCD1602显示器,连接该VCC固定到5V在Arduino上。
  • 连接的GNDLCD1602的引脚到GNDArduino上的引脚。
  • 连接 השלושSDA将LCD1602的引脚连接到相同的位置A4用于MPU-6050的引脚。
  • 连接该SCL将LCD1602的引脚连接到相同的A5用于MPU-6050的引脚。

确保仔细检查您的连接,因为错误的接线可能导致设备故障或无法正常通信。

代码示例与步骤解析

代码首先包含了MPU-6050和LCD显示器所需的库。以下摘录展示了如何初始化MPU-6050:

MPU6050 mpu6050(Wire);

这里,我们实例化了MPU6050将用于从传感器读取数据的对象。接下来,我们需要在设备中设置setup()功能:

void setup() {
  Serial.begin(9600);
  Wire.begin();
  mpu6050.begin();
  mpu6050.calcGyroOffsets(true);
  lcd.begin();
  lcd.backlight();   
}

该代码初始化了串行通信、I2C总线和MPU-6050传感器。它还初始化了液晶显示器并打开了背光。循环中包含更新和显示角度的逻辑:

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);
}

在这个循环中,X、Y和Z的角度被更新并显示在LCD上。该函数lcdDisplay()用于正确格式化输出。您可以在文章下方找到完整的代码。

演示 / 期望内容

当设置完成并上传代码后,您应该可以在液晶显示屏上实时看到X、Y和Z的角度。如果一切连接正确,显示屏将每100毫秒刷新一次,显示MPU-6050的当前方向。

常见的陷阱包括确保 I2C 连接正确以及电源稳定。如果 MPU-6050 没有正确初始化,您可能会在 LCD 上看到错误的数据或没有数据(视频中的时间为 01:15)。

图像

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)
语言: C++
/*
 * 
 * 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);
 
}

|||您可能需要的东西

资源与参考

文件📁

Arduino 库(zip 格式)

Fritzing 文件