如何使用MPU-6050加速度計同陀螺儀模組
MPU-6050 係一個多功能感測器,喺單一封裝入面結合咗三軸加速度計同三軸陀螺儀。喺呢個教學入面,我哋會探討點樣將呢個模組同 Arduino 連接,嚟量度加速度同角速度。最終成果會係一個簡單程式,顯示感測器嘅實時數據,等你可以監察裝置嘅加速度同方向。

跟住呢個指南,你會學到點樣設定硬件、正確接線,同埋寫必要嘅程式碼令 MPU-6050 正常運作。呢個會為你之後涉及動作感應同方向偵測嘅項目打好基礎。為咗更清楚,記得睇吓呢個教學附帶嘅影片(喺影片 00:00 位置)。
硬件解說
呢個項目嘅主要元件係 MPU-6050 感測器,佢提供加速度計同陀螺儀數據。加速度計量度沿住 X、Y、Z 軸嘅線性加速度,而陀螺儀就量度圍繞呢啲軸嘅旋轉速率。呢個組合令 MPU-6050 成為需要動作追蹤應用嘅絕佳選擇。
另外,呢個模組用 I2C 介面做通訊,咁樣簡化咗接線,亦容許多個裝置連接喺同一條匯流排上。感測器透過 VCC 針腳供電,通常需要 3.3V 至 5V 之間嘅電壓。接地就連接到 Arduino 嘅共同接地。
數據手冊細節
| 製造商 | InvenSense |
|---|---|
| 零件編號 | MPU-6050 |
| 邏輯/IO 電壓 | 3.3 – 5 V |
| 供電電壓 | 3.3 – 5 V |
| 輸出電流(每通道) | … |
| 峰值電流(每通道) | … |
| PWM 頻率指引 | … |
| 輸入邏輯閾值 | … |
| 電壓降 / RDS(on) / 飽和 | … |
| 熱限制 | … |
| 封裝 | QFN |
| 備註 / 變體 | … |
- 將 VCC 針腳連接到 3.3V 或 5V。
- 將 GND 針腳連接到接地。
- 透過 SDA(數據)同 SCL(時鐘)使用 I2C 通訊。
- 確保 SDA 同 SCL 線路上有上拉電阻。
- 檢查電壓水平是否正確,以免損壞感測器。
接線指示

要將 MPU-6050 連接到 Arduino,首先將 MPU-6050 嘅 VCC 針腳連接到 Arduino 上嘅 5V 針腳。跟住將 MPU-6050 嘅 GND 針腳連接到 Arduino 上其中一個 GND 針腳。至於 I2C 通訊,將 MPU-6050 嘅 SDA 針腳連接到 Arduino 嘅 A4 針腳,SCL 針腳就連接到 A5 針腳。確保你喺程式碼入面加入適當嘅程式庫,令 Arduino 使用 I2C 通訊。
喺供電之前,再三檢查你嘅接線。如果使用麵包板,確保連接穩固,冇短路。如果模組冇預期咁回應,檢查接線,並喺程式碼入面核對 MPU-6050 嘅 I2C 地址。
程式碼示例同解說
以下程式碼片段初始化 MPU-6050 並開始讀取數據。主要識別符包括 mpu6050,即係 MPU6050 類別嘅實例,同埋 timer,用嚟控制數據輸出嘅時間。
MPU6050 mpu6050(Wire);
long timer = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
}
喺呢個 setup 函數入面,我哋初始化串列通訊同 MPU-6050 感測器。mpu6050.calcGyroOffsets(true) 呢個呼叫好重要,因為佢會校準陀螺儀偏移,確保讀數準確。
喺 loop 函數入面,我哋持續更新感測器數據,並將佢打印到串列監視器。以下程式碼片段示範點樣讀取同打印溫度同加速度數據:
if(millis() - timer > 1000){
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());
}
呢個區塊檢查自上次輸出之後係咪過咗一秒。然後佢從 MPU-6050 攞溫度同加速度數值,並將佢哋打印到串列監視器。你可以觀察到呢啲數值實時更新。
示範 / 預期效果
一旦所有嘢接好線並上載咗程式碼,你可以預期每秒喺串列監視器見到溫度同加速度數據打印出嚟。如果你傾斜或移動 MPU-6050,加速度數值應該會相應改變。要注意,錯誤接線或供電不足可能導致讀數唔穩定,甚至完全冇數據(喺影片 01:30 位置)。
/*
*
* This code is basic usage of the MPU-6050 Accelerometer and Gyroscope.
*
* This code displays all data:
* -Gyroscope X, Gyroscope Y, Gyroscope Z
* -Gyroscope Angle X, Gyroscope Angle Y, Gyroscope Angle Z
* -Accel X, Accel Y, Accel Z
* -Accel Angle X, Accel Angle Y, Accel Angle Z,
*
* 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/uhh7ik02aDc
*
*/
#include <MPU6050_tockn.h>
#include <Wire.h>
MPU6050 mpu6050(Wire);
long timer = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
}
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();
}
}
/*
*
* This code is basic usage of the MPU-6050 Accelerometer and Gyroscope.
*
* This code displays:
* - Angle X, Angle Y, Angle Z,
*
* 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/uhh7ik02aDc
*
*/
#include <MPU6050_tockn.h>
#include <Wire.h>
MPU6050 mpu6050(Wire);
long timer = 0;
void setup() {
Serial.begin(9600);
Wire.begin();
mpu6050.begin();
mpu6050.calcGyroOffsets(true);
}
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();
}
}
你可能需要嘅嘢
-
亞馬遜Purchase MPU-6050 from Amazonamzn.to
資源與參考資料
-
外部Manufacturer websiteinvensense.com
文件📁
Arduino 函式庫 (zip)
-
Arduino library for MPU9250
robojax-MPU9250-master.zip3.38 MB
其他文件
-
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