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?

More than 5 years have passed since last update.

Arduinoで3軸ジャイロセンサを使う

Posted at

この記事は、arduinoで3軸ジャイロセンサ(L3GD20)を使うためのものです。x,y,z軸それぞれの角速度を計測できます。

接続方法

Acce.JPG

プログラム

参考サイト
https://analogicintelligence.blogspot.com/2016/09/i2cspil3gd20.html

gyro3axis.ino
# include <SPI.h>

const int L3GD20_CS = SS;

const byte L3GD20_ADDR = 0x6a;
const byte L3GD20_WHOAMI = 0x0f;
const byte L3GD20_CTRL_REG1 = 0x20;
const byte L3GD20_OUT_X_L = 0x28;
const byte L3GD20_OUT_X_H = 0x29;
const byte L3GD20_OUT_Y_L = 0x2A;
const byte L3GD20_OUT_Y_H = 0x2B;
const byte L3GD20_OUT_Z_L = 0x2C;
const byte L3GD20_OUT_Z_H = 0x2D;

const byte L3GD20_RW = 0x80;
const byte L3GD20_MS = 0x40;

byte L3GD20_read(byte reg) {
  byte ret = 0;

  digitalWrite(L3GD20_CS, LOW);
  SPI.transfer(reg | L3GD20_RW);
  ret = SPI.transfer(0);
  digitalWrite(L3GD20_CS, HIGH);

  return ret;
}

void setup() {
  digitalWrite(SS, HIGH);
  pinMode(SS, OUTPUT);

  SPI.begin();
  SPI.setBitOrder(MSBFIRST);
  SPI.setClockDivider(SPI_CLOCK_DIV8);

 Serial.begin(9600);
 while (!Serial) {}

  Serial.println(L3GD20_read(L3GD20_WHOAMI), HEX);
  digitalWrite(L3GD20_CS, LOW);
  SPI.transfer(L3GD20_CTRL_REG1);
  SPI.transfer(B00001111);
  digitalWrite(L3GD20_CS, HIGH);
}

float xaxis,yaxis,zaxis;

void loop() {
  short h, l;
  float x, y, z;

  l = L3GD20_read(L3GD20_OUT_X_L);
  h = L3GD20_read(L3GD20_OUT_X_H);
  x = (h << 8) | l;
  l = L3GD20_read(L3GD20_OUT_Y_L);
  h = L3GD20_read(L3GD20_OUT_Y_H);
  y = (h << 8) | l;
  l = L3GD20_read(L3GD20_OUT_Z_L);
  h = L3GD20_read(L3GD20_OUT_Z_H);
  z = (h << 8) | l;

  xaxis += x+209.0;
  yaxis += y-155.0;
  zaxis += z+115.0;

  Serial.print(xaxis);
  Serial.print(",");
  Serial.print(yaxis);
  Serial.print(",");
  Serial.println(zaxis);
  delay(20);
}
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?