0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

安価な6軸MPU6050を試す

Posted at

はじめに

6軸MPU6050は
加速度3軸+角速度(ジャイロ)3軸を
計測できるセンサ。

amazonで購入可能。
6個入で2000円を切るので、1個300円ぐらい。

Foriot MPU-6050

6個 GY-521 MPU6050 3軸加速度計 ジャイロモジュール MPU-6050 6 DOF 6軸加速度計 DIYキット 16ビットADコンバーター データ出力 IIC I2C [DC 3-5V]

接続例

I2C接続なので、配線数は4本。
(残りの4穴は使わない)

MPU6050 Arduino
VCC 5V
GND GND
SCL SCL
SDA SDA

SCLとSDAは赤いリセットボタン近くにある2穴へ入れる。
image.png

image.png

プログラム

#include <Wire.h>

const int MPU = 0x68;

int16_t ax, ay, az;//加速度
int16_t gx, gy, gz;//角速度

void setup() {
  Serial.begin(115200);//シリアルプロッタもこの値に合わせる
  Wire.begin();

  // wake up
  Wire.beginTransmission(MPU);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
}

void loop() {
  Wire.beginTransmission(MPU);
  Wire.write(0x3B);
  Wire.endTransmission(false);
  Wire.requestFrom(MPU, 14, true);

  ax = Wire.read() << 8 | Wire.read();
  ay = Wire.read() << 8 | Wire.read();
  az = Wire.read() << 8 | Wire.read();

  Wire.read(); Wire.read(); // temp skip

  gx = Wire.read() << 8 | Wire.read();
  gy = Wire.read() << 8 | Wire.read();
  gz = Wire.read() << 8 | Wire.read();

  // ---- シリアルプロッタ用:スペース区切り ----
  /*
  Serial.print(ax); Serial.print(' ');
  Serial.print(ay); Serial.print(' ');
  Serial.print(az); Serial.print(' ');
  Serial.print(gx); Serial.print(' ');
  Serial.print(gy); Serial.print(' ');
  Serial.println(gz);
  */

  // 加速度のみ 正規化
  // -2G〜+2Gを16bit(65536)出力なので16384(=65536/4)で割ると静止状態で1Gが出るはず。
  // 出るはずだが、実際には誤差というか、ズレがある。補正必須かも。
  float axg = ax / 16384.0;
  float ayg = ay / 16384.0;
  float azg = az / 16384.0;
  Serial.print(axg); Serial.print(' ');
  Serial.print(ayg); Serial.print(' ');
  Serial.println(azg);

  delay(20); // 50Hzくらい
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?