検索コード

LCD1602(I2C)ディスプレイを使用したMPU-6050用Arduinoコード

LCD1602(I2C)ディスプレイを使用したMPU-6050用Arduinoコード

このチュートリアルでは、MPU-6050(加速度計・ジャイロスコープ)を I2C 通信で 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インターフェースでは複数のデバイスを同じバスに接続できるため、複数のセンサーやコンポーネントを用いるプロジェクトで効率的です。

データシートの詳細

製造元インベンセンス
部品番号MPU-6050
ロジック/入出力電圧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 に配線するには、次の接続を行ってください:

  • 接続してくださいVCCMPU-6050のピンを〜へ5VArduinoのピン。
  • 接続するGNDMPU-6050のピンを〜へGNDArduinoのピン。
  • 接続するSDAMPU-6050のピンを…にA4Arduinoのピン。
  • 接続してくださいSCLMPU-6050のピンを〜へA5Arduino上のピン。
  • LCD1602ディスプレイの場合は、次のように接続してくださいVCCにピン留めする5VArduino上で。
  • 接続するGNDLCD1602のピンを〜へGNDArduinoのピン。
  • 接続するSDALCD1602のピンを同じにするA4MPU-6050で使用されるピン。
  • 接続してくださいSCLLCD1602のピンを同じにするA5MPU-6050用のピン。

接続を必ず再確認してください。誤った配線は機器の誤動作や正常に通信できなくなるおそれがあります。

コード例と解説

コードはまず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()出力を正しく整形するために使用されます。記事の下に完全なコードが掲載されています。

デモンストレーション/期待できること

セットアップが完了してコードがアップロードされると、LCDにX、Y、Zの角度がリアルタイムで表示されるはずです。配線が正しく行われていれば表示は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);
 
}

必要かもしれないもの

リソースと参考文献

ファイル📁

Arduinoライブラリ(zip)

フリッツィングファイル