Search Code

MPU-9250 加速度計、陀螺儀同磁力計代碼

MPU-9250 加速度計、陀螺儀同磁力計代碼

喺呢個教學入面,我哋會探討點樣用 MPU-9250 感測器,佢將加速度計、陀螺儀同磁力計結合喺一個細小模組入面。完成呢個指南之後,你就能夠讀取感測器數據,並理解佢喺你嘅項目入面嘅意義。呢個感測器對於機械人同無人機等應用特別有用,因為喺嗰啲地方,方向同運動追蹤係好關鍵嘅。你可以參考影片嚟進一步了解設置同代碼(影片喺 00:20)。

ELE_MPU6050_208
ELE_MPU6050_208

硬件講解

MPU-9250 模組整合咗三個感測器:加速度計、陀螺儀同磁力計。加速度計量度加速度力,令你可以判斷速度同方向。陀螺儀提供角速度,幫你理解方向隨時間嘅變化。最後,磁力計好似指南針咁,提供磁場數據,有助於導航。

呢種感測器組合喺需要精確運動追蹤嘅應用入面特別有用,例如無人機或者智能手機。MPU-9250 透過 I2C 或 SPI 通訊,令佢可以靈活配合唔同嘅微控制器設置。

數據手冊詳情

製造商 InvenSense
零件編號 MPU-9250
邏輯/IO 電壓 1.8 V(I/O),3.3 V(供電)
供電電壓 2.4 - 3.6 V
輸出電流(每通道) 3.2 mA(正常操作)
峰值電流(每通道) 19.8 mA(最大)
PWM 頻率指引 唔適用
輸入邏輯閾值 0.3 * VDD(低),0.7 * VDD(高)
電壓降 / RDS(on) / 飽和 唔適用
熱限制 -40 至 85 °C
封裝 QFN
備註 / 變體 包含內部電壓穩壓器
  • 確保提供正確嘅電壓(2.4 - 3.6 V),以免損壞模組。
  • 如果 breakout 板冇內置,請為 SDA 同 SCL 線加上拉電阻。
  • 確認 I2C 地址(預設係 0x68),並按需要調整 ADO 腳位嚟用其他地址。
  • 檢查連接嘅穩定性,以防止錯誤讀數。
  • 定期校準感測器,以確保量度準確。

接線說明

Arduino wiring for  MPU6050 using A4 and A4 for SDA and SCL
Arduino 接線圖,用 A4 同 A4 做 SDA 同 SCL 連接 MPU6050
Arduino Wiring for MPU-6050
Arduino Wiring for MPU-6050
Arduino wiring for  MPU6050 using A4 and A4 for SDA and SCL
Arduino wiring for MPU6050 using A4 and A4 for SDA and SCL

要將 MPU-9250 接到 Arduino,首先將 MPU-9250 嘅 VCC 腳位連接到 Arduino 嘅 5V 腳位。模組有內部穩壓器,所以用 5V 供電係安全嘅。跟住將 GND 腳位連接到 Arduino 嘅接地(GND)。至於數據通訊,將 SDA 腳位連接到 Arduino 嘅 A4 腳位,將 SCL 腳位連接到 A5 腳位。呢個配置係好多 Arduino 板嘅標準設定。

如果你用緊 Arduino Mega,SDA 同 SCL 腳位分別喺第 20 同第 21 腳。確保連接穩固,以免出現通訊錯誤。如果你需要更改 MPU-9250 嘅 I2C 地址,可以將 ADO 腳位連接到 5V,將地址設為 0x69。斷開連接就會回復返 0x68。

Arduino Schematic for  MPU6050 using A4 and A4 for SDA and SCL
Arduino 原理圖,用 A4 同 A4 做 SDA 同 SCL 連接 MPU6050

代碼示例同講解

喺 Arduino sketch 入面,我哋首先包含 MPU9250 函式庫,並建立一個感測器實例:

#include "MPU9250.h"
MPU9250 IMU(Wire,0x68);

代碼喺 setup() 函數入面初始化感測器並開始通訊:

void setup() {
  Serial.begin(115200);
  status = IMU.begin();
  if (status < 0) {
    Serial.println("IMU 初始化失敗");
    while(1) {}
  }
}

loop() 函數入面,我哋持續讀取並打印感測器數據:

void loop() {
  IMU.readSensor();
  Serial.print("AccelX: ");
  Serial.print(IMU.getAccelX_mss(),6);
  // 其他軸嘅更多打印語句
}

呢個部分讀取加速度計、陀螺儀同磁力計嘅數值,並顯示喺 Serial Monitor 上面。記得檢查文章下面載入嘅完整代碼,以了解完整嘅實現。

示範 / 預期效果

當你運行代碼時,應該會見到感測器數值喺 Serial Monitor 上實時更新。加速度計數值會隨住你移動感測器而波動,而陀螺儀數值會顯示旋轉速率。磁力計會俾你三個軸嘅磁場強度,可以用嚟判斷方向。要小心浮動輸入,因為佢哋可能導致唔準確嘅讀數(影片喺 12:30)。

章節

  • 簡介 (00:00)
  • 硬件講解 (01:30)
  • 接線說明 (04:00)
  • 代碼示例同逐步講解 (06:00)
  • 示範 (10:00)

圖片

ELE_MPU6050_208
ELE_MPU6050_208
Arduino Wiring for MPU-6050
Arduino Wiring for MPU-6050
Arduino Schematic for  MPU6050 using A4 and A4 for SDA and SCL
Arduino Schematic for MPU6050 using A4 and A4 for SDA and SCL
Arduino wiring for  MPU6050 using A4 and A4 for SDA and SCL
Arduino wiring for MPU6050 using A4 and A4 for SDA and SCL
126-Arduino code for an MPU-9250 accelerometer, gyroscope, and magnetometer
語言: C++
/*
 * Library: https://github.com/bolderflight/MPU9250
Basic_I2C.ino
Brian R Taylor
brian.taylor@bolderflight.com

Copyright (c) 2017 Bolder Flight Systems

Permission is hereby granted, free of charge, to any person obtaining a copy of this software 
and associated documentation files (the "Software"), to deal in the Software without restriction, 
including without limitation the rights to use, copy, modify, merge, publish, distribute, 
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is 
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or 
substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING 
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
 * Updated by Ahmad Shamshiri on July 9, 2018 for Robojax.com
 * in Ajax, Ontario, Canada
 * Watch instruction video for this code: 
For this sketch you need to connect:
VCC to 5V and GND to GND of Arduino
SDA to A4 and SCL to A5

S20A is 3.3V voltage regulator MIC5205-3.3BM5
*/

#include "MPU9250.h"

// An MPU9250 object with the MPU-9250 sensor on I2C bus 0 with address 0x68
MPU9250 IMU(Wire,0x68);
int status;

void setup() {
  // Serial to display data
  Serial.begin(115200);
  while(!Serial) {}

  // Start communication with IMU 
  status = IMU.begin();
  if (status < 0) {
    Serial.println("IMU initialization unsuccessful");
    Serial.println("Check IMU wiring or try cycling power");
    Serial.print("Status: ");
    Serial.println(status);
    while(1) {}
  }
}

void loop() {
  // Read the sensor
  IMU.readSensor();
  // Display the data
  Serial.print("AccelX: ");
  Serial.print(IMU.getAccelX_mss(),6);
  Serial.print("\t");
  Serial.print("AccelY: ");  
  Serial.print(IMU.getAccelY_mss(),6);
  Serial.print("\t");
  Serial.print("AccelZ: ");  
  Serial.println(IMU.getAccelZ_mss(),6);
  
  Serial.print("GyroX: ");
  Serial.print(IMU.getGyroX_rads(),6);
  Serial.print("\t");
  Serial.print("GyroY: ");  
  Serial.print(IMU.getGyroY_rads(),6);
  Serial.print("\t");
  Serial.print("GyroZ: ");  
  Serial.println(IMU.getGyroZ_rads(),6);

  Serial.print("MagX: ");  
  Serial.print(IMU.getMagX_uT(),6);
  Serial.print("\t");  
  Serial.print("MagY: ");
  Serial.print(IMU.getMagY_uT(),6);
  Serial.print("\t");
  Serial.print("MagZ: ");  
  Serial.println(IMU.getMagZ_uT(),6);
  
  Serial.print("Temperature in C: ");
  Serial.println(IMU.getTemperature_C(),6);
  Serial.println();
  delay(200);
}

資源與參考資料

文件📁

冇文件可用。