14浏览
查看: 14|回复: 5

[项目] 【Arduino 动手做】SEP32获取MPU-6050陀螺加速度和温度数值

[复制链接]
MPU-6050 是一个带有 3 轴加速度计和 3 轴陀螺仪的模块。

MPU-6050 模块加速度计陀螺仪温度传感器
陀螺仪测量旋转速度 (rad/s),这是角度位置沿 X、Y 和 Z 轴(滚动、俯仰和偏航)随时间的变化。这允许我们确定对象的方向。

Roll Pitch 偏航角
加速度计测量加速度 (物体速度的变化率)。它感应重力等静态力(9.8m/s2) 或动态力(如振动或运动)进行验证。MPU-6050 可测量 X、Y 和 Z 轴上的加速度。理想情况下,在静态对象中,Z 轴上的加速度等于重力,并且在 X 轴和 Y 轴上应为零。

使用加速度计中的值,可以使用三角函数计算滚动角和俯仰角。但是,无法计算偏航。

我们可以将来自两个传感器的信息结合起来,以获得有关传感器方向的更准确信息。

【Arduino 动手做】SEP32获取MPU-6050陀螺加速度和温度数值图3

【Arduino 动手做】SEP32获取MPU-6050陀螺加速度和温度数值图1

【Arduino 动手做】SEP32获取MPU-6050陀螺加速度和温度数值图4

【Arduino 动手做】SEP32获取MPU-6050陀螺加速度和温度数值图2

【Arduino 动手做】SEP32获取MPU-6050陀螺加速度和温度数值图5

【Arduino 动手做】SEP32获取MPU-6050陀螺加速度和温度数值图6

【Arduino 动手做】SEP32获取MPU-6050陀螺加速度和温度数值图7

驴友花雕  中级技神
 楼主|

发表于 6 小时前

【Arduino 动手做】ESP32获取MPU-6050陀螺加速度和温度数值

获取陀螺仪、加速度和温度读数(串行监视器)
项目代码

  1. // Basic demo for accelerometer readings from Adafruit MPU6050
  2. // ESP32 Guide: https://RandomNerdTutorials.com/esp32-mpu-6050-accelerometer-gyroscope-arduino/
  3. // ESP8266 Guide: https://RandomNerdTutorials.com/esp8266-nodemcu-mpu-6050-accelerometer-gyroscope-arduino/
  4. // Arduino Guide: https://RandomNerdTutorials.com/arduino-mpu-6050-accelerometer-gyroscope/
  5. #include <Adafruit_MPU6050.h>
  6. #include <Adafruit_Sensor.h>
  7. #include <Wire.h>
  8. Adafruit_MPU6050 mpu;
  9. void setup(void) {
  10.   Serial.begin(115200);
  11.   while (!Serial)
  12.     delay(10); // will pause Zero, Leonardo, etc until serial console opens
  13.   Serial.println("Adafruit MPU6050 test!");
  14.   // Try to initialize!
  15.   if (!mpu.begin()) {
  16.     Serial.println("Failed to find MPU6050 chip");
  17.     while (1) {
  18.       delay(10);
  19.     }
  20.   }
  21.   Serial.println("MPU6050 Found!");
  22.   mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  23.   Serial.print("Accelerometer range set to: ");
  24.   switch (mpu.getAccelerometerRange()) {
  25.   case MPU6050_RANGE_2_G:
  26.     Serial.println("+-2G");
  27.     break;
  28.   case MPU6050_RANGE_4_G:
  29.     Serial.println("+-4G");
  30.     break;
  31.   case MPU6050_RANGE_8_G:
  32.     Serial.println("+-8G");
  33.     break;
  34.   case MPU6050_RANGE_16_G:
  35.     Serial.println("+-16G");
  36.     break;
  37.   }
  38.   mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  39.   Serial.print("Gyro range set to: ");
  40.   switch (mpu.getGyroRange()) {
  41.   case MPU6050_RANGE_250_DEG:
  42.     Serial.println("+- 250 deg/s");
  43.     break;
  44.   case MPU6050_RANGE_500_DEG:
  45.     Serial.println("+- 500 deg/s");
  46.     break;
  47.   case MPU6050_RANGE_1000_DEG:
  48.     Serial.println("+- 1000 deg/s");
  49.     break;
  50.   case MPU6050_RANGE_2000_DEG:
  51.     Serial.println("+- 2000 deg/s");
  52.     break;
  53.   }
  54.   mpu.setFilterBandwidth(MPU6050_BAND_5_HZ);
  55.   Serial.print("Filter bandwidth set to: ");
  56.   switch (mpu.getFilterBandwidth()) {
  57.   case MPU6050_BAND_260_HZ:
  58.     Serial.println("260 Hz");
  59.     break;
  60.   case MPU6050_BAND_184_HZ:
  61.     Serial.println("184 Hz");
  62.     break;
  63.   case MPU6050_BAND_94_HZ:
  64.     Serial.println("94 Hz");
  65.     break;
  66.   case MPU6050_BAND_44_HZ:
  67.     Serial.println("44 Hz");
  68.     break;
  69.   case MPU6050_BAND_21_HZ:
  70.     Serial.println("21 Hz");
  71.     break;
  72.   case MPU6050_BAND_10_HZ:
  73.     Serial.println("10 Hz");
  74.     break;
  75.   case MPU6050_BAND_5_HZ:
  76.     Serial.println("5 Hz");
  77.     break;
  78.   }
  79.   Serial.println("");
  80.   delay(100);
  81. }
  82. void loop() {
  83.   /* Get new sensor events with the readings */
  84.   sensors_event_t a, g, temp;
  85.   mpu.getEvent(&a, &g, &temp);
  86.   /* Print out the values */
  87.   Serial.print("Acceleration X: ");
  88.   Serial.print(a.acceleration.x);
  89.   Serial.print(", Y: ");
  90.   Serial.print(a.acceleration.y);
  91.   Serial.print(", Z: ");
  92.   Serial.print(a.acceleration.z);
  93.   Serial.println(" m/s^2");
  94.   Serial.print("Rotation X: ");
  95.   Serial.print(g.gyro.x);
  96.   Serial.print(", Y: ");
  97.   Serial.print(g.gyro.y);
  98.   Serial.print(", Z: ");
  99.   Serial.print(g.gyro.z);
  100.   Serial.println(" rad/s");
  101.   Serial.print("Temperature: ");
  102.   Serial.print(temp.temperature);
  103.   Serial.println(" degC");
  104.   Serial.println("");
  105.   delay(500);
  106. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 6 小时前

【Arduino 动手做】ESP32获取MPU-6050陀螺加速度和温度数值

【Arduino 动手做】SEP32获取MPU-6050陀螺加速度和温度数值图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 6 小时前

【Arduino 动手做】ESP32获取MPU-6050陀螺加速度和温度数值

在 OLED 显示屏上显示陀螺仪和加速度读数
项目代码

  1. // Basic OLED demo for accelerometer readings from Adafruit MPU6050
  2. // ESP32 Guide: https://RandomNerdTutorials.com/esp32-mpu-6050-accelerometer-gyroscope-arduino/
  3. // ESP8266 Guide: https://RandomNerdTutorials.com/esp8266-nodemcu-mpu-6050-accelerometer-gyroscope-arduino/
  4. // Arduino Guide: https://RandomNerdTutorials.com/arduino-mpu-6050-accelerometer-gyroscope/
  5. #include <Adafruit_MPU6050.h>
  6. #include <Adafruit_SSD1306.h>
  7. #include <Adafruit_Sensor.h>
  8. Adafruit_MPU6050 mpu;
  9. Adafruit_SSD1306 display = Adafruit_SSD1306(128, 64, &Wire);
  10. void setup() {
  11.   Serial.begin(115200);
  12.   // while (!Serial);
  13.   Serial.println("MPU6050 OLED demo");
  14.   if (!mpu.begin()) {
  15.     Serial.println("Sensor init failed");
  16.     while (1)
  17.       yield();
  18.   }
  19.   Serial.println("Found a MPU-6050 sensor");
  20.   // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  21.   if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
  22.     Serial.println(F("SSD1306 allocation failed"));
  23.     for (;;)
  24.       ; // Don't proceed, loop forever
  25.   }
  26.   display.display();
  27.   delay(500); // Pause for 2 seconds
  28.   display.setTextSize(1);
  29.   display.setTextColor(WHITE);
  30.   display.setRotation(0);
  31. }
  32. void loop() {
  33.   sensors_event_t a, g, temp;
  34.   mpu.getEvent(&a, &g, &temp);
  35.   display.clearDisplay();
  36.   display.setCursor(0, 0);
  37.   Serial.print("Accelerometer ");
  38.   Serial.print("X: ");
  39.   Serial.print(a.acceleration.x, 1);
  40.   Serial.print(" m/s^2, ");
  41.   Serial.print("Y: ");
  42.   Serial.print(a.acceleration.y, 1);
  43.   Serial.print(" m/s^2, ");
  44.   Serial.print("Z: ");
  45.   Serial.print(a.acceleration.z, 1);
  46.   Serial.println(" m/s^2");
  47.   display.println("Accelerometer - m/s^2");
  48.   display.print(a.acceleration.x, 1);
  49.   display.print(", ");
  50.   display.print(a.acceleration.y, 1);
  51.   display.print(", ");
  52.   display.print(a.acceleration.z, 1);
  53.   display.println("");
  54.   Serial.print("Gyroscope ");
  55.   Serial.print("X: ");
  56.   Serial.print(g.gyro.x, 1);
  57.   Serial.print(" rps, ");
  58.   Serial.print("Y: ");
  59.   Serial.print(g.gyro.y, 1);
  60.   Serial.print(" rps, ");
  61.   Serial.print("Z: ");
  62.   Serial.print(g.gyro.z, 1);
  63.   Serial.println(" rps");
  64.   display.println("Gyroscope - rps");
  65.   display.print(g.gyro.x, 1);
  66.   display.print(", ");
  67.   display.print(g.gyro.y, 1);
  68.   display.print(", ");
  69.   display.print(g.gyro.z, 1);
  70.   display.println("");
  71.   display.display();
  72.   delay(100);
  73. }
复制代码


回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 5 小时前

【Arduino 动手做】ESP32获取MPU-6050陀螺加速度和温度数值

【Arduino 动手做】SEP32获取MPU-6050陀螺加速度和温度数值图1
回复

使用道具 举报

驴友花雕  中级技神
 楼主|

发表于 5 小时前

【Arduino 动手做】ESP32获取MPU-6050陀螺加速度和温度数值

【Arduino 动手做】ESP32获取MPU-6050陀螺加速度和温度数值
项目链接:https://randomnerdtutorials.com/ ... -gyroscope-arduino/
项目作者:Random Nerd Tutorials
项目代码:https://randomnerdtutorials.com/ ... -gyroscope-arduino/

【Arduino 动手做】SEP32获取MPU-6050陀螺加速度和温度数值图1

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4 备案 沪公网安备31011502402448

© 2013-2025 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail