วิธีใช้โมดูล MPU-6050 Accelerometer และ Gyroscope
MPU-6050 เป็นเซนเซอร์อเนกประสงค์ที่รวมมาตรวัดความเร่ง 3 แกนและไจโรสโคป 3 แกนไว้ในแพ็คเกจเดียว ในบทช่วยสอนนี้ เราจะสำรวจวิธีการเชื่อมต่อโมดูลนี้กับ Arduino เพื่อวัดความเร่งและความเร็วเชิงมุม ผลลัพธ์จะเป็นโปรแกรมง่ายๆ ที่แสดงข้อมูลแบบเรียลไทม์จากเซนเซอร์ ช่วยให้คุณสามารถตรวจสอบทั้งความเร่งและการวางแนวของอุปกรณ์ของคุณได้

โดยการปฏิบัติตามคำแนะนำนี้ คุณจะได้เรียนรู้วิธีการตั้งค่าฮาร์ดแวร์ การเดินสายไฟส่วนประกอบอย่างถูกต้อง และเขียนโค้ดที่จำเป็นเพื่อให้ MPU-6050 ทำงานได้ ซึ่งจะช่วยให้คุณมีพื้นฐานสำหรับโปรเจกต์เพิ่มเติมที่เกี่ยวข้องกับการตรวจจับการเคลื่อนไหวและการตรวจจับการวางแนว เพื่อความชัดเจนยิ่งขึ้น โปรดตรวจสอบวิดีโอที่มาพร้อมกับบทช่วยสอนนี้ (ในวิดีโอที่ 00:00)
อธิบายฮาร์ดแวร์
ส่วนประกอบหลักในโปรเจกต์นี้คือเซนเซอร์ MPU-6050 ซึ่งให้ข้อมูลทั้งมาตรวัดความเร่งและไจโรสโคป มาตรวัดความเร่งวัดความเร่งเชิงเส้นตามแกน X, Y และ Z ในขณะที่ไจโรสโคปวัดอัตราการหมุนรอบแกนเดียวกันนี้ การรวมกันนี้ทำให้ MPU-6050 เป็นตัวเลือกที่ยอดเยี่ยมสำหรับการใช้งานที่ต้องการการติดตามการเคลื่อนไหว
นอกจากนี้ โมดูลนี้ใช้อินเทอร์เฟซ I2C สำหรับการสื่อสาร ซึ่งช่วยให้การเชื่อมต่อง่ายขึ้นและอนุญาตให้เชื่อมต่ออุปกรณ์หลายตัวบนบัสเดียวกันได้ เซนเซอร์ได้รับพลังงานผ่านพิน VCC ซึ่งโดยทั่วไปต้องใช้แรงดันไฟฟ้าระหว่าง 3.3V ถึง 5V ขั้วกราวด์เชื่อมต่อกับกราวด์ร่วมของ Arduino
รายละเอียดจาก Datasheet
| ผู้ผลิต | InvenSense |
|---|---|
| หมายเลขชิ้นส่วน | MPU-6050 |
| แรงดันไฟฟ้าลอจิก/IO | 3.3 – 5 V |
| แรงดันไฟฟ้าจ่าย | 3.3 – 5 V |
| กระแสเอาต์พุต (ต่อช่อง) | … |
| กระแสสูงสุด (ต่อช่อง) | … |
| คำแนะนำความถี่ PWM | … |
| เกณฑ์ลอจิกอินพุต | … |
| แรงดันตก / RDS(on) / ความอิ่มตัว | … |
| ขีดจำกัดความร้อน | … |
| แพ็คเกจ | QFN |
| หมายเหตุ / รุ่นต่างๆ | … |
- เชื่อมต่อพิน VCC เข้ากับ 3.3V หรือ 5V
- เชื่อมต่อพิน GND เข้ากับกราวด์
- ใช้การสื่อสาร I2C ผ่าน SDA (ข้อมูล) และ SCL (สัญญาณนาฬิกา)
- ตรวจสอบให้แน่ใจว่ามีตัวต้านทาน pull-up บนสาย SDA และ SCL
- ตรวจสอบระดับแรงดันไฟฟ้าที่เหมาะสมเพื่อหลีกเลี่ยงความเสียหายต่อเซนเซอร์
คำแนะนำการเดินสายไฟ

ในการเดินสายไฟ MPU-6050 เข้ากับ Arduino ของคุณ ให้เริ่มต้นด้วยการเชื่อมต่อพิน VCC ของ MPU-6050 เข้ากับพิน 5V บน Arduino จากนั้นเชื่อมต่อพิน GND ของ MPU-6050 เข้ากับพิน GND หนึ่งตัวบน Arduino สำหรับการสื่อสาร I2C ให้เชื่อมต่อพิน SDA ของ MPU-6050 เข้ากับพิน A4 ของ Arduino และพิน SCL เข้ากับพิน A5 ตรวจสอบให้แน่ใจว่า Arduino ของคุณถูกตั้งค่าให้ใช้การสื่อสาร I2C โดยรวมไลบรารีที่เหมาะสมในโค้ดของคุณ
ตรวจสอบการเชื่อมต่อของคุณอีกครั้งก่อนที่จะจ่ายไฟให้ระบบ หากใช้เบรดบอร์ด ตรวจสอบให้แน่ใจว่าการเชื่อมต่อแน่นหนาและไม่มีไฟฟ้าลัดวงจร หากโมดูลไม่ตอบสนองตามที่คาดหวัง ให้ตรวจสอบการเดินสายไฟและตรวจสอบที่อยู่ I2C ของ MPU-6050 ในโค้ดของคุณ
ตัวอย่างโค้ดและคำอธิบาย
โค้ดต่อไปนี้เริ่มต้น 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 เราจะอัปเดตข้อมูลเซนเซอร์อย่างต่อเนื่องและพิมพ์ไปยัง serial monitor โค้ดต่อไปนี้แสดงวิธีการอ่านและพิมพ์ข้อมูลอุณหภูมิและความเร่ง:
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 และพิมพ์ไปยัง serial monitor คุณสามารถสังเกตค่าที่อัปเดตแบบเรียลไทม์ได้
การสาธิต / สิ่งที่คาดหวัง
เมื่อทุกอย่างเชื่อมต่อและอัปโหลดโค้ดแล้ว คุณสามารถคาดหวังว่าจะเห็นข้อมูลอุณหภูมิและความเร่งพิมพ์ใน serial monitor ทุกวินาที หากคุณเอียงหรือขยับ 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