Prikaz podataka sa MPU-6050 senzora na LCD 1602 ili LCD2004 sa Arduinom
U ovom tutorijalu naučit ćemo kako prikazati podatke sa MPU-6050 senzora na LCD 1602 ili LCD2004 ekranu koristeći Arduino. MPU-6050 je svestran senzor koji kombinuje troosovinski žiroskop i troosovinski akcelerometar, što ga čini idealnim za različite primjene poput detekcije pokreta i orijentacije. Povezivanjem ovog senzora sa LCD ekranom, možemo vizualizirati podatke u realnom vremenu, uključujući uglove i temperaturu.

Kako budemo napredovali kroz ovaj projekat, pokrit ćemo potrebne hardverske komponente, detalje povezivanja i implementaciju koda. Ovo će vam pomoći da razumijete kako postaviti MPU-6050 i prikazati njegov izlaz na LCD ekranu. Za dodatna pojašnjenja o kodu, svakako pogledajte video (u videu na 00:00).
Objašnjenje hardvera
Primarne komponente za ovaj projekat su Arduino ploča, MPU-6050 senzor i LCD ekran (bilo 1602 ili 2004). Arduino služi kao mikrokontroler koji obrađuje podatke sa MPU-6050 i šalje ih na LCD.
MPU-6050 senzor koristi I2C komunikaciju za slanje podataka na Arduino. Uključuje akcelerometar i žiroskop, koji mu omogućavaju da osjeti pokret i orijentaciju. LCD ekran se koristi za prikaz uglova dobijenih iz podataka senzora. Povezuje se na Arduino također putem I2C-a, što pojednostavljuje ožičenje i komunikaciju.
Detalji iz tehničke dokumentacije
| Proizvođač | Invensense |
|---|---|
| Broj dijela | MPU-6050 |
| Logički/IO napon | 3.3 V / 5 V |
| Napon napajanja | 3.3 V do 5 V |
| Brzina izlaznih podataka | 1 kHz (maks.) |
| Temperaturni opseg | -40 do +85 °C |
| Pakovanje | QFN |
| Napomene / varijante | Integrisani žiroskop i akcelerometar |
- Osigurajte odgovarajuće napajanje unutar navedenog naponskog opsega.
- Koristite pull-up otpornike na I2C linijama ako je potrebno.
- Provjerite I2C adresu radi kompatibilnosti sa vašim postavljanjem.
- Kalibrirajte senzor za tačna očitavanja.
- Budite oprezni sa ožičenjem kako biste izbjegli kratke spojeve.
Upute za povezivanje

Da biste povezali MPU-6050 na Arduino, počnite spajanjem VCC pina MPU-6050 na 5V pin na Arduinu. Zatim povežite GND pin MPU-6050 na jedan od GND pinova na Arduinu. Za I2C komunikaciju, povežite SDA pin MPU-6050 na analogni pin A4 na Arduinu, a SCL pin na analogni pin A5.
Za LCD ekran, povežite VCC pin na 5V pin na Arduinu i GND pin na GND pin na Arduinu također. Povežite SDA pin LCD-a na isti A4 pin koji se koristi za MPU-6050, a SCL pin LCD-a na A5 pin. Na ovaj način, i MPU-6050 i LCD dijele iste I2C linije, što pojednostavljuje ožičenje.

Osigurajte da su veze sigurne i da nema labavih žica. Ako se vaš LCD ili senzor ne uključi, provjerite ožičenje i veze.
Primjeri koda i objašnjenje
U kodu, počinjemo uključivanjem potrebnih biblioteka za MPU-6050 i LCD:
#include
#include
#include
Ovdje kreiramo instance za MPU-6050 i LCD. MPU-6050 se inicijalizira sa `Wire` bibliotekom za I2C komunikaciju, dok se LCD postavlja sa svojom adresom i dimenzijama.
U setup funkciji, inicijaliziramo senzor i LCD:

void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
lcd.begin();
lcd.backlight();
lcd.clear();
}
Ovaj kod postavlja serijsku komunikaciju za debugging, inicijalizira I2C komunikaciju i priprema LCD za prikaz. Pozadinsko osvjetljenje se uključuje kako bi ekran bio vidljiv.
U loop funkciji, kontinuirano čitamo podatke sa MPU-6050 i prikazujemo ih na LCD-u:
void loop() {
mpu6050.update();
lcd.clear();
lcdDisplay(mpu6050.getAngleX(), mpu6050.getAngleY(), mpu6050.getAngleZ());
delay(100);
}
Ovaj isječak ažurira podatke senzora i čisti LCD za nova očitavanja svakih 100 milisekundi. Funkcija `lcdDisplay` se poziva za prikaz uglova na LCD-u.
Za potpuno razumijevanje, pogledajte odgovarajući video gdje je cijeli kod demonstriran (u videu na 00:00).
Demonstracija / Šta očekivati
Kada je sve pravilno postavljeno, LCD bi trebao prikazivati uglove za X, Y i Z osi u realnom vremenu. Možete nagnuti MPU-6050 senzor da vidite promjene na ekranu. Ako naiđete na probleme, provjerite obrnutu polarizaciju u ožičenju ili osigurajte da su I2C adrese pravilno postavljene.
Praćenje vrijednosti na LCD-u omogućit će vam da vidite kako senzor reagira na pokret i promjene orijentacije. Ako se vrijednosti čine statičnim ili netačnim, provjerite veze i osigurajte da senzor ispravno funkcionira.
/*
*
* 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);
}
}
Resursi i reference
-
EksternoGet the library from GitHubgithub.com
-
EksternoManufacturer websiteinvensense.com
Datoteke📁
Arduino biblioteke (zip)
-
Arduino library for MPU9250
robojax-MPU9250-master.zip3.38 MB
Fritzing datoteka
-
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
Ostale datoteke
-
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