코드 검색

MPU-6050 센서 데이터를 Arduino와 함께 LCD 1602 또는 LCD2004에 표시하기

MPU-6050 센서 데이터를 Arduino와 함께 LCD 1602 또는 LCD2004에 표시하기

이 튜토리얼에서는 Arduino를 사용하여 MPU-6050 센서의 데이터를 LCD 1602 또는 LCD2004에 표시하는 방법을 배웁니다. MPU-6050은 3축 자이로스코프와 3축 가속도계를 결합한 다용도 센서로, 모션 감지 및 방향 감지와 같은 다양한 응용 분야에 적합합니다. 이 센서를 LCD 디스플레이에 연결하면 각도와 온도를 포함한 실시간 센서 데이터를 시각화할 수 있습니다.

이 프로젝트를 진행하면서 필요한 하드웨어 구성 요소, 배선 세부 사항 및 코드 구현을 다룰 것입니다. 이를 통해 MPU-6050을 설정하고 그 출력을 LCD에 표시하는 방법을 이해하는 데 도움이 될 것입니다. 코드에 대한 추가 설명이 필요하면 (비디오 00:00 시점) 비디오를 확인하세요.

하드웨어 설명

이 프로젝트의 주요 구성 요소는 Arduino 보드, MPU-6050 센서 및 LCD 디스플레이(1602 또는 2004)입니다. Arduino는 MPU-6050의 데이터를 처리하여 LCD로 보내는 마이크로컨트롤러 역할을 합니다.

MPU-6050 센서는 I2C 통신을 사용하여 Arduino에 데이터를 보냅니다. 여기에는 가속도계와 자이로스코프가 포함되어 있어 모션과 방향을 감지할 수 있습니다. LCD 디스플레이는 센서 데이터에서 파생된 각도를 표시하는 데 사용됩니다. 또한 I2C를 통해 Arduino에 연결되므로 배선과 통신이 간소화됩니다.

데이터시트 세부 정보

제조업체Invensense
부품 번호MPU-6050
로직/IO 전압3.3 V / 5 V
공급 전압3.3 V ~ 5 V
출력 데이터 속도1 kHz (최대)
온도 범위-40 ~ +85 °C
패키지QFN
참고 사항 / 변형자이로스코프 및 가속도계 통합

  • 지정된 전압 범위 내에서 적절한 전원 공급을 확인하세요.
  • 필요한 경우 I2C 라인에 풀업 저항을 사용하세요.
  • 설정과의 호환성을 위해 I2C 주소를 확인하세요.
  • 정확한 판독을 위해 센서를 보정하세요.
  • 단락을 방지하기 위해 배선에 주의하세요.

배선 지침

arduino_wiring_MPU-6050_LCD2004_bb

MPU-6050을 Arduino에 배선하려면 먼저 MPU-6050의 VCC 핀을 Arduino의 5V 핀에 연결하세요. 다음으로 MPU-6050의 GND 핀을 Arduino의 GND 핀 중 하나에 연결합니다. I2C 통신을 위해 MPU-6050의 SDA 핀을 Arduino의 아날로그 핀 A4에 연결하고, SCL 핀을 아날로그 핀 A5에 연결합니다.

LCD 디스플레이의 경우 VCC 핀을 Arduino의 5V 핀에 연결하고 GND 핀도 Arduino의 GND 핀에 연결합니다. LCD의 SDA 핀을 MPU-6050에 사용된 동일한 A4 핀에 연결하고, LCD의 SCL 핀을 A5 핀에 연결합니다. 이렇게 하면 MPU-6050과 LCD가 동일한 I2C 라인을 공유하여 배선이 간소화됩니다.

연결이 안전하고 느슨한 전선이 없는지 확인하세요. LCD나 센서에 전원이 켜지지 않으면 배선과 연결을 다시 확인하세요.

코드 예제 및 설명

코드에서는 MPU-6050과 LCD에 필요한 라이브러리를 포함하는 것으로 시작합니다:

#include 
#include 
#include 

여기서 MPU-6050과 LCD 모두에 대한 인스턴스를 생성합니다. MPU-6050은 I2C 통신을 위해 `Wire` 라이브러리로 초기화되고, LCD는 주소와 크기로 설정됩니다.

설정 함수에서 센서와 LCD를 초기화합니다:

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

이 코드는 디버깅을 위한 시리얼 통신을 설정하고, I2C 통신을 초기화하며, LCD를 표시할 준비를 합니다. 백라이트를 켜서 디스플레이를 보이게 합니다.

루프 함수에서 MPU-6050의 데이터를 지속적으로 읽고 LCD에 표시합니다:

void loop() {
  mpu6050.update();
  lcd.clear();
  lcdDisplay(mpu6050.getAngleX(), mpu6050.getAngleY(), mpu6050.getAngleZ());
  delay(100);
}

이 코드 조각은 센서 데이터를 업데이트하고 100밀리초마다 새 판독값을 위해 LCD를 지웁니다. `lcdDisplay` 함수는 각도를 LCD에 표시하기 위해 호출됩니다.

전체 코드가 시연되는 해당 비디오(비디오 00:00 시점)를 시청하여 완전한 이해를 돕기 바랍니다.

시연 / 기대 효과

모든 것이 올바르게 설정되면 LCD에 X, Y, Z 축의 각도가 실시간으로 표시됩니다. MPU-6050 센서를 기울여 디스플레이의 변화를 확인할 수 있습니다. 문제가 발생하면 배선의 극성 반전을 확인하거나 I2C 주소가 올바르게 설정되었는지 확인하세요.

LCD의 값을 모니터링하면 센서가 움직임과 방향 변화에 어떻게 반응하는지 확인할 수 있습니다. 값이 정적이거나 잘못된 것 같으면 연결을 확인하고 센서가 제대로 작동하는지 확인하세요.

이미지

arduino_wiring_MPU-6050_LCD2004_bb
arduino_wiring_MPU-6050_LCD2004_bb
LCD2004_display-3
LCD2004_display-3
LCD2004_display-1
LCD2004_display-1
LCD2004_display-2
LCD2004_display-2
119-Arduino code for MPU-6050 accelerometer and gyroscope sensor (angles only)
언어: 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 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);   
 
}
120-Arduino code for the MPU-6050 accelerometer and gyroscope sensor (all data)
언어: C++
/*
 * 
 * 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);   

  
    }
 
}

자원 및 참고자료

파일📁

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

프리징 파일

다른 파일들

  • LCD1602 LiquidCrystal Library
    robojax-LCD1602_LiquidCrystal.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