当前位置:   article > 正文

Arduino操作MPU6050模块_arduino mpu6050

arduino mpu6050

MPU6050是集成三轴陀螺仪,三轴加速度计,温度传感器于一体的模块。本文档基于Adafruit_MPU6050实现MPU6050模块基本操作。

Adafruit_MPU6050库:

https://github.com/adafruit/Adafruit_MPU6050

Adafruit_MPU6050依赖以下库,需要在Arduino编译器里分别添加:

Adafruit_BusIO: https://github.com/adafruit/Adafruit_BusIO
Adafruit_Sensor: https://github.com/adafruit/Adafruit_Sensor
Adafruit_GFX_Library: https://github.com/adafruit/Adafruit-GFX-Library
Adafruit_SSD1306: https://github.com/adafruit/Adafruit_SSD1306

1 接线:
在这里插入图片描述

如图:
VCC -> 5V
GND -> GND
SCL -> A5
SDA -> A4
INT -> 2

2 编写读取程序:

Adafruit_MPU6050完整说明文档 https://adafruit.github.io/Adafruit_MPU6050/html/class_adafruit___m_p_u6050.html

#include <Adafruit_MPU6050.h>
#include <Adafruit_sensor.h>
#include <Wire.h>

Adafruit_MPU6050 mpu;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);

  // initialize mpu
  if (!mpu.begin()) {
    Serial.println("Failed to start MPU6050");
  } else {
    Serial.println("Start MPU6050");
  }

  // display MPU6050 settings
  mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  Serial.print("Accelerometer range set to: ");
  switch (mpu.getAccelerometerRange()) {
  case MPU6050_RANGE_2_G:
    Serial.println("+-2G");
    break;
  case MPU6050_RANGE_4_G:
    Serial.println("+-4G");
    break;
  case MPU6050_RANGE_8_G:
    Serial.println("+-8G");
    break;
  case MPU6050_RANGE_16_G:
    Serial.println("+-16G");
    break;
  }
  mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  Serial.print("Gyro range set to: ");
  switch (mpu.getGyroRange()) {
  case MPU6050_RANGE_250_DEG:
    Serial.println("+- 250 deg/s");
    break;
  case MPU6050_RANGE_500_DEG:
    Serial.println("+- 500 deg/s");
    break;
  case MPU6050_RANGE_1000_DEG:
    Serial.println("+- 1000 deg/s");
    break;
  case MPU6050_RANGE_2000_DEG:
    Serial.println("+- 2000 deg/s");
    break;
  }

  mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
  Serial.print("Filter bandwidth set to: ");
  switch (mpu.getFilterBandwidth()) {
  case MPU6050_BAND_260_HZ:
    Serial.println("260 Hz");
    break;
  case MPU6050_BAND_184_HZ:
    Serial.println("184 Hz");
    break;
  case MPU6050_BAND_94_HZ:
    Serial.println("94 Hz");
    break;
  case MPU6050_BAND_44_HZ:
    Serial.println("44 Hz");
    break;
  case MPU6050_BAND_21_HZ:
    Serial.println("21 Hz");
    break;
  case MPU6050_BAND_10_HZ:
    Serial.println("10 Hz");
    break;
  case MPU6050_BAND_5_HZ:
    Serial.println("5 Hz");
    break;
  }
}


void displayReadings() {
  // get new sensor events with the readings
  sensors_event_t a, g, temp;
  mpu.getEvent(&a, &g, &temp);

  // print out the values
  // acceleration values
  Serial.print("Acceleration X: ");
  Serial.print(a.acceleration.x);
  Serial.print(", Y: ");
  Serial.print(a.acceleration.y);
  Serial.print(", Z: ");
  Serial.print(a.acceleration.z);
  Serial.println(" m/s^2");

  //rotation values
  Serial.print("Rotation X: ");
  Serial.print(g.gyro.x);
  Serial.print(", Y: ");
  Serial.print(g.gyro.y);
  Serial.print(", Z: ");
  Serial.print(g.gyro.z);
  Serial.println(" rad/s");

  // temperature values
  Serial.print("Temperature: ");
  Serial.print(temp.temperature);
  Serial.println(" degC");
}

void loop() {
  // put your main code here, to run repeatedly:
  displayReadings();
  delay(500);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114

1

#include <Adafruit_MPU6050.h>
#include <Adafruit_sensor.h>
#include <Wire.h>
Adafruit_MPU6050 mpu;
  • 1
  • 2
  • 3
  • 4

调用以上库, 创建Adafruit_MPU6050对象mpu

2

  if (!mpu.begin()) {
    Serial.println("Failed to start MPU6050");
  } else {
    Serial.println("Start MPU6050");
  }

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

mpu.begin():启动MPU6050,返回值为boolean,true代表启动成功

3

mpu.setAccelerometerRange(MPU6050_RANGE_8_G);
  • 1

setAccelerometerRange():设定加速度计量程,参数有MPU6050_RANGE_2_G,MPU6050_RANGE_4_G,MPU6050_RANGE_8_G,MPU6050_RANGE_16_G

4

mpu.setGyroRange(MPU6050_RANGE_500_DEG);
  • 1

setGyroRange():设置陀螺仪量程,参数有MPU6050_RANGE_250_DEG,MPU6050_RANGE_500_DEG,MPU6050_RANGE_1000_DEG,MPU6050_RANGE_2000_DEG

5

mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);
  • 1

setFilterBandwidth():设置滤波器带宽,参数有MPU6050_BAND_260_HZ,MPU6050_BAND_184_HZ,MPU6050_BAND_94_HZ,MPU6050_BAND_44_HZ,MPU6050_BAND_21_HZ,MPU6050_BAND_10_HZ,MPU6050_BAND_5_HZ

6

sensors_event_t a, g, temp;
mpu.getEvent(&a, &g, &temp);
  • 1
  • 2

mpu.getEvent()方法获得加速度,角度,温度。我们可以输出x,y,z方向上的加速度和角度

Adfruit_MPU6050库里还包括设置中断,停止测量某一数值等功能,详细见官方说明文档

https://adafruit.github.io/Adafruit_MPU6050/html/class_adafruit___m_p_u6050.html

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/575022
推荐阅读
相关标签
  

闽ICP备14008679号