ArduinoでLCD 1602またはLCD2004にMPU-6050センサーデータを表示する
このチュートリアルでは、MPU-6050センサーのデータをArduinoを使ってLCD 1602またはLCD2004に表示する方法を学びます。MPU-6050は3軸ジャイロスコープと3軸加速度計を組み合わせた多用途のセンサーで、動作検出や姿勢推定などのさまざまな用途に適しています。このセンサーをLCDディスプレイに接続することで、角度や温度を含むリアルタイムのセンサーデータを可視化できます。

このプロジェクトを進めるにあたり、必要なハードウェア部品、配線の詳細、そしてコードの実装について説明します。これにより、MPU-6050のセットアップ方法とその出力をLCDに表示する方法が理解できるようになります。コードについてさらに明確にしたい場合は、(in video at 00:00) をぜひご覧ください。
ハードウェア解説
このプロジェクトの主要な構成要素は Arduino ボード、MPU-6050 センサー、および LCD ディスプレイ(1602 または 2004)です。Arduino は MPU-6050 からのデータを処理して LCD に送信するマイクロコントローラとして機能します。
MPU-6050センサーはI2C通信を用いてデータをArduinoに送信します。加速度計とジャイロスコープを内蔵しており、これにより動きや姿勢を検知できます。LCDディスプレイはセンサーデータから導出した角度を表示するために使用されます。LCDもI2C経由でArduinoに接続されるため、配線と通信が簡素化されます。
データシートの詳細
| 製造元 | インヴェンセンス |
|---|---|
| 部品番号 | MPU-6050 |
| ロジック/IO電圧 | 3.3 V / 5 V |
| 供給電圧 | 3.3 Vから5 Vまで |
| 出力データレート | 1 kHz(最大) |
| 温度範囲 | -40〜+85°C |
| パッケージ | QFN(クワッドフラットノーリード) |
| 備考/バリエーション | ジャイロスコープと加速度計を内蔵 |
- 指定された電圧範囲内で適切な電源供給を確保してください。
- 必要に応じて、I2Cラインにプルアップ抵抗を使用してください。
- I2Cアドレスがご利用のセットアップと互換性があるか確認してください。
- 正確な読み取りのためにセンサーを校正してください。
- 配線には注意して短絡を避けてください。
配線手順

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ピンに、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を表示できるように準備します。表示が見えるようにバックライトを点灯します。
loop() 関数では、MPU-6050からデータを継続的に読み取り、LCDに表示します:
void loop() {
mpu6050.update();
lcd.clear();
lcdDisplay(mpu6050.getAngleX(), mpu6050.getAngleY(), mpu6050.getAngleZ());
delay(100);
}
このスニペットはセンサーデータを更新し、100ミリ秒ごとにLCDを新しい読み取り用にクリアします。角度をLCDに表示するためにlcdDisplay関数が呼び出されます。
完全に理解するには、対応するビデオでコード全体が示されている箇所(ビデオの00:00)をご覧ください。
デモンストレーション / 期待される内容
すべてが正しく設定されていれば、LCDにはX軸、Y軸、Z軸の角度がリアルタイムで表示されます。MPU-6050センサーを傾けると表示の変化が確認できます。問題が発生した場合は、配線の極性が逆になっていないか、またはI2Cアドレスが正しく設定されているかを確認してください。
LCD上の値を監視することで、センサーが動きや姿勢の変化にどのように反応するかを確認できます。値が静的に見える、または正しくない場合は、接続を確認し、センサーが正常に動作しているかを確かめてください。
/*
*
* 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);
}
}
リソースと参考文献
-
外部Get the library from GitHubgithub.com
-
外部メーカーのウェブサイトinvensense.com
ファイル📁
Arduinoライブラリ(zip)
-
MPU9250用Arduinoライブラリ
robojax-MPU9250-master.zip3.38 MB
フリッツィングファイル
-
4本のワイヤーを持つLCD LCD1602-I2Cモジュール
LCD1602-I2C.fzpz0.01 MB -
MPU-6050 ボード GY-521
MPU-6050 Board GY-521.fzpz0.01 MB -
LCD2004-I2C
LCD2004-I2C.fzpz0.02 MB
他のファイル
-
LCD1602 リキッドクリスタルライブラリ
robojax-LCD1602_LiquidCrystal.zip