Go Down

Topic: Need help in ITG-3200 three axis gyroscope! (Read 211 times) previous topic - next topic

assassin

I'm using ITG-3200 three axis gyrosocpe for my project(https://learn.sparkfun.com/tutorials/itg-3200-hookup-guide).

I followed the online tutorial step-by-step and uploaded the example code to my arduino pro(5V 16MHz). Here is the code:


#include <Wire.h>


char WHO_AM_I = 0x00;
char SMPLRT_DIV= 0x15;
char DLPF_FS = 0x16;
char GYRO_XOUT_H = 0x1D;
char GYRO_XOUT_L = 0x1E;
char GYRO_YOUT_H = 0x1F;
char GYRO_YOUT_L = 0x20;
char GYRO_ZOUT_H = 0x21;
char GYRO_ZOUT_L = 0x22;

char DLPF_CFG_0 = (1<<0);
char DLPF_CFG_1 = (1<<1);
char DLPF_CFG_2 = (1<<2);
char DLPF_FS_SEL_0 = (1<<3);
char DLPF_FS_SEL_1 = (1<<4);

char itgAddress = 0x69;

void setup()
{

  Serial.begin(115200);
 
  Wire.begin();
 
  //Read the WHO_AM_I register and print the result
  char id=0;
  id = itgRead(itgAddress, 0x00); 
  Serial.print("ID: ");
  Serial.println(id, HEX);
 
 
  itgWrite(itgAddress, DLPF_FS, (DLPF_FS_SEL_0|DLPF_FS_SEL_1|DLPF_CFG_0));
 
  itgWrite(itgAddress, SMPLRT_DIV, 9);
}


void loop()
{
  //Create variables to hold the output rates.
  int xRate, yRate, zRate;

  //Read the x,y and z output rates from the gyroscope.
  xRate = readX();
  yRate = readY();
  zRate = readZ();

 
  Serial.print(xRate);
  Serial.print('\t');
  Serial.print(yRate);
  Serial.print('\t');
  Serial.println(zRate); 

  //Wait 10ms before reading the values again. (Remember, the output rate was set to 100hz and 1reading per 10ms = 100hz.)
  delay(10);
}

void itgWrite(char address, char registerAddress, char data)
{
 
  Wire.beginTransmission(address);
 
  Wire.write(registerAddress);
 
  Wire.write(data);
 
  Wire.endTransmission();
}


unsigned char itgRead(char address, char registerAddress)
{

  unsigned char data=0;

  Wire.beginTransmission(address);

  Wire.write(registerAddress);

  Wire.endTransmission();

  Wire.beginTransmission(address);
  Wire.requestFrom(address, 1);
 

    data = Wire.read();
  }

  Wire.endTransmission();
 
  //Return the data read during the operation
  return data;
}


int readX(void)
{
  int data=0;
  data = itgRead(itgAddress, GYRO_XOUT_H)<<8;
  data |= itgRead(itgAddress, GYRO_XOUT_L); 
 
  return data;
}

int readY(void)
{
  int data=0;
  data = itgRead(itgAddress, GYRO_YOUT_H)<<8;
  data |= itgRead(itgAddress, GYRO_YOUT_L); 
 
  return data;
}


int readZ(void)
{
  int data=0;
  data = itgRead(itgAddress, GYRO_ZOUT_H)<<8;
  data |= itgRead(itgAddress, GYRO_ZOUT_L); 
 
  return data;
}
 

The gyroscope and arduino pro were both powered up by 3.3V, and GND were connected together. However, the values for three axis are all 0, it seems that the chip did not work properly. Could anyone help me figure out my problem?  :'(  :'(  :'(
 

Thanks!
It's now or never

Peter_n

#1
Mar 09, 2015, 09:46 am Last Edit: Mar 09, 2015, 09:57 am by Peter_n
Please use code tags when you show your sketch.
Did you try the i2c_scanner ? http://playground.arduino.cc/Main/I2cScanner

Did you write the sketch yourself, or did you find it somewhere ?
There should be no Wire.endTransmission() after Wire.requestFrom() and no Wire.beginTransmission() before it.
Reading a singly byte is a very large overhead.

Go Up