Search Code

د MPU-6050 سینسر ډاټا په LCD 1602 یا LCD2004 کې د Arduino سره ښودل

د MPU-6050 سینسر ډاټا په LCD 1602 یا LCD2004 کې د Arduino سره ښودل

په دې ټیوټوریل کې به موږ زده کړو چې څنګه د MPU-6050 سینسر څخه معلومات په LCD 1602 یا LCD2004 باندې د Arduino په کارولو سره ښکاره کړو. MPU-6050 یو څو اړخیز سینسر دی چې د 3-axis gyroscope او 3-axis accelerometer سره یوځای کوي، چې دا د مختلفو غوښتنلیکونو لکه د حرکت کشف او سمت ټاکلو لپاره مثالی کوي. د دې سینسر په LCD ښودونکي سره نښلولو سره، موږ کولی شو د ریښتیني وخت سینسر معلومات، په شمول د زاویو او تودوخې، وګورو.

لکه څنګه چې موږ د دې پروژې له لارې پرمختګ کوو، موږ به اړین هارډویر اجزا، د تارونو نښلولو تفصیلات، او د کوډ پلي کول پوښو. دا به تاسو سره مرسته وکړي چې پوه شئ چې څنګه MPU-6050 تنظیم کړئ او د هغې محصول په LCD باندې ښکاره کړئ. د کوډ په اړه د نورو روښانتیا لپاره، ډاډ ترلاسه کړئ چې ویډیو په (په ویډیو کې په 00:00) کې وګورئ.

هارډویر تشریح شوی

د دې پروژې لومړني اجزا د Arduino بورډ، MPU-6050 سینسر، او LCD ښودونکی (یا 1602 یا 2004) دي. Arduino د مایکروکنټرولر په توګه کار کوي چې د MPU-6050 څخه معلومات پروسس کوي او LCD ته یې لیږي.

MPU-6050 سینسر د I2C اړیکه کاروي ترڅو معلومات Arduino ته واستوي. دا یو accelerometer او gyroscope لري، کوم چې دا توان ورکوي چې حرکت او سمت حس کړي. LCD ښودونکی د سینسر معلوماتو څخه اخیستل شوي زاویې ښودلو لپاره کارول کیږي. دا هم د I2C له لارې Arduino سره نښلول کیږي، کوم چې د تارونو نښلول او اړیکه ساده کوي.

د ډیټاشیټ تفصیلات

جوړونکیInvensense
د پرزې شمېرهMPU-6050
د منطق/IO ولټاژ3.3 V / 5 V
د بریښنا ولټاژ3.3 V څخه تر 5 V
د محصول ډیټا کچه1 kHz (اعظمي)
د تودوخې حد-40 څخه تر +85 °C
بسته بنديQFN
یادښتونه / بڼېGyroscope او accelerometer مدغم شوي

  • ډاډ ترلاسه کړئ چې د بریښنا رسول په ټاکل شوي ولټاژ حد کې وي.
  • که اړتیا وي، د I2C کرښو باندې pull-up resistors وکاروئ.
  • د 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 په analog پن A4 سره ونښلوئ، او د SCL پن د analog پن A5 سره ونښلوئ.

د LCD ښودونکي لپاره، د VCC پن د Arduino په 5V پن سره او د GND پن د Arduino په GND پن سره ونښلوئ. د LCD د SDA پن د هغه ورته A4 پن سره ونښلوئ چې د MPU-6050 لپاره کارول شوی، او د LCD د SCL پن د A5 پن سره ونښلوئ. په دې توګه، دواړه MPU-6050 او LCD ورته I2C کرښې شریکوي، چې د تارونو نښلول ساده کوي.

ډاډ ترلاسه کړئ چې اړیکې خوندي دي او هیڅ نرم تارونه شتون نلري. که ستاسو LCD یا سینسر چالان نشي، د تارونو او اړیکو بیا وګورئ.

د کوډ مثالونه او تفصیلي کتنه

په کوډ کې، موږ د MPU-6050 او LCD لپاره د اړینو کتابتونونو په شاملولو سره پیل کوو:

#include 
#include 
#include 

دلته، موږ د MPU-6050 او LCD دواړو لپاره بیلګې جوړوو. MPU-6050 د I2C اړیکې لپاره د `Wire` کتابتون سره پیل کیږي، پداسې حال کې چې LCD د خپل پته او ابعادو سره تنظیم کیږي.

په setup فنکشن کې، موږ سینسر او LCD پیل کوو:

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

دا کوډ د ډیبګ کولو لپاره سریال اړیکه جوړوي، د I2C اړیکه پیل کوي، او LCD د ښودلو لپاره چمتو کوي. backlight چالان کیږي ترڅو ښودنه لیدل کیږي.

په loop فنکشن کې، موږ په دوامداره توګه د 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 باندې د ارزښتونو څارنه به تاسو ته اجازه درکړي چې وګورئ سینسر څنګه حرکت او د سمت بدلونونو ته ځواب ورکوي. که ارزښتونه ثابت یا ناسم ښکاري، اړیکې وګورئ او ډاډ ترلاسه کړئ چې سینسر په سمه توګه کار کوي.

Images

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)
Language: 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)
Language: 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);   

  
    }
 
}

Resources & references

Files📁

Arduino Libraries (zip)

Fritzing File

Other files

  • 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