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 接线:
Arduino操作MPU6050模块_第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 
#include 
#include 

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

#include 
#include 
#include 
Adafruit_MPU6050 mpu;

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

2

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

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

3

mpu.setAccelerometerRange(MPU6050_RANGE_8_G);

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

4

mpu.setGyroRange(MPU6050_RANGE_500_DEG);

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

5

mpu.setFilterBandwidth(MPU6050_BAND_21_HZ);

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);

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

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

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

你可能感兴趣的:(实习记录,c++,arduino,MPU6050,单片机,陀螺仪)