코드 검색

MPU-6050과 LCD1602 I2C 디스플레이를 위한 아두이노 코드

MPU-6050과 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은 3축 가속도계와 3축 자이로스코프를 결합한 다용도 센서로, 실시간 각도 측정값을 제공합니다. I2C를 통해 통신하므로 Arduino와 원활하게 통합할 수 있습니다.

LCD1602 디스플레이도 I2C 프로토콜을 사용하므로 배선이 간단해지고 통신에 필요한 핀 수가 줄어듭니다. I2C 인터페이스를 사용하면 동일한 버스에 여러 장치를 연결할 수 있어 여러 센서나 구성 요소가 있는 프로젝트에 효율적입니다.

데이터시트 세부 정보

제조업체InvenSense
부품 번호MPU-6050
로직/IO 전압3.3 V / 5 V
공급 전압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에 배선하려면 다음 연결을 따르세요:

  • MPU-6050의 VCC 핀을 Arduino의 5V 핀에 연결하세요.
  • MPU-6050의 GND 핀을 Arduino의 GND 핀에 연결하세요.
  • MPU-6050의 SDA 핀을 Arduino의 A4 핀에 연결하세요.
  • MPU-6050의 SCL 핀을 Arduino의 A5 핀에 연결하세요.
  • LCD1602 디스플레이의 경우 VCC 핀을 Arduino의 5V에 연결하세요.
  • LCD1602의 GND 핀을 Arduino의 GND 핀에 연결하세요.
  • LCD1602의 SDA 핀을 MPU-6050에 사용된 동일한 A4 핀에 연결하세요.
  • LCD1602의 SCL 핀을 MPU-6050에 사용된 동일한 A5 핀에 연결하세요.

잘못된 배선은 장치 오작동이나 통신 실패를 초래할 수 있으므로 연결을 다시 확인하세요.

코드 예제 및 설명

코드는 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 센서를 초기화합니다. 또한 LCD를 초기화하고 백라이트를 켭니다. 루프에는 각도를 업데이트하고 표시하는 로직이 포함되어 있습니다:

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 각도가 LCD에 실시간으로 표시되는 것을 볼 수 있습니다. 모든 것이 올바르게 배선되었다면 디스플레이는 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);
 
}

필요할 수 있는 것들

자원 및 참고자료

파일📁

아두이노 라이브러리 (zip)

프리징 파일

다른 파일들

  • MPU6050_tockn-master
    Arduino 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