ITG-3200 Hookup Guide

Contributors: JoelEB

Pages

Firmware

We’re finally ready to start looking at the firmware. We’ve written an example Arduino sketch to help you get started. You can download it here, or, as always, you visit the ITG-3200 GitHub page for the latest updates.

The sample sketch reads the gyroscope data for the X, Y, and Z axes and prints it to the serial port. This is raw gyroscope data, and it has not been converted to degrees per second yet. Bigger numbers mean the device is rotating faster. Positive numbers indicate one direction of rotation while negative numbers indicate the opposite rotation direction. Since this is a triple-axis gyroscope, we can measure the rotational rate of the board no matter which way the board is rotating. Rotation is usually measured in degrees per second. If the board spins around an axis exactly one time in a second, the gyroscope would measure 360 degrees per second.

Now, let’s break up the code in to sections to go over what’s happening a little more in depth.

language:c
//The Wire library is used for I2C communication
#include <Wire.h>

//This is a list of registers in the ITG-3200. Registers are parameters that determine how the sensor will behave, or they can hold data that represent the
//sensors current status.
//To learn more about the registers on the ITG-3200, download and read the datasheet.
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;

//This is a list of settings that can be loaded into the registers.
//DLPF, Full Scale Register Bits
//FS_SEL must be set to 3 for proper operation
//Set DLPF_CFG to 3 for 1kHz Fint and 42 Hz Low Pass Filter
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;

//I2C devices each have an address. The address is defined in the datasheet for the device. The ITG-3200 breakout board can have different address depending on how
//the jumper on top of the board is configured. By default, the jumper is connected to the VDD pin. When the jumper is connected to the VDD pin the I2C address
//is 0x69.
char itgAddress = 0x69;

This is the configuration section of the sketch. It looks more complicated than it is! First we include the “Wire.h” library, which comes standard with the Arduino IDE. This library is used for I2C communication, which is the communication protocol used by the ITG-3200.

Next, there is list of variables which are assigned to different registers on the ITG-3200. Registers are used mostly to do two things: configure parameters for a sensor, or hold data that the sensor has collected. When we want to interact with a sensor’s register, we must tell the sensor which register address we want to work with. After the list of registers is a short list of register parameters. This is only a list for the parameters used by this sketch. There are many more parameters listed in the ITG-3200 datasheet that aren’t used in this example.

Finally, after the list of parameters, is the itgAddress variable. This is the I2C address of the ITG-3200. The I2C address for the sensor is also listed in the datasheet. Remember, this address is directly impacted by the configuration of the solder jumper on the top of the PCB.

language:c
//In the setup section of the sketch the serial port will be configured, the i2c communication will be initialized, and the itg-3200 will be configured.
void setup()
{
  //Create a serial connection using a 9600bps baud rate.
  Serial.begin(9600);

  //Initialize the I2C communication. This will set the Arduino up as the 'Master' device.
  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);

  //Configure the gyroscope
  //Set the gyroscope scale for the outputs to +/-2000 degrees per second
  itgWrite(itgAddress, DLPF_FS, (DLPF_FS_SEL_0|DLPF_FS_SEL_1|DLPF_CFG_0));
  //Set the sample rate to 100 hz
  itgWrite(itgAddress, SMPLRT_DIV, 9);
}

The Setup section of the code is pretty short. First, we create a Serial connection so that we can print data to a terminal window. Then we initialize the I2C communication protocol. Now the Arduino is ready to start interacting with the ITG-3200. Most sensors have some kind of identification register. A good way to verify that the communication is working properly is to read the identification register and ensure the result is valid. After reading the identification register a couple of values are written to some registers on the ITG-3200 to configure the gyroscope to read data at 100hz and measure rotation rates up to 2000 degrees per second. The itgRead and itgWrite functions will be explained a little later. Once the device has been configured the actual gyroscope data can be read.

language:c
//The loop section of the sketch will read the X,Y and Z output rates from the gyroscope and output them in the Serial Terminal
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();
  //Print the output rates to the terminal, seperated by a TAB character.
  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);
}

The Loop section of the code is usually the ‘meat’ of the sketch, in this case the loop is very straightforward. The sketch reads the X, Y, and Z gyroscope values using the readX(), readY() and readZ() functions. After storing these values, they are printed to the serial terminal. We delay 10 ms at the end of the loop so that we don’t try reading information from the sensor faster than it can be provided. For the Setup and Loop sections of the code to look so simple we had to use a couple of functions, let’s see how the functions work.

language:c
//This function will write a value to a register on the itg-3200.
//Parameters:
//  char address: The I2C address of the sensor. For the ITG-3200 breakout the address is 0x69.
//  char registerAddress: The address of the register on the sensor that should be written to.
//  char data: The value to be written to the specified register.
void itgWrite(char address, char registerAddress, char data)
{
  //Initiate a communication sequence with the desired i2c device
  Wire.beginTransmission(address);
  //Tell the I2C address which register we are writing to
  Wire.send(registerAddress);
  //Send the value to write to the specified register
  Wire.send(data);
  //End the communication sequence
  Wire.endTransmission();
}
//This function will read the data from a specified register on the ITG-3200 and return the value.
//Parameters:
//  char address: The I2C address of the sensor. For the ITG-3200 breakout the address is 0x69.
//  char registerAddress: The address of the register on the sensor that should be read
//Return:
//  unsigned char: The value currently residing in the specified register
unsigned char itgRead(char address, char registerAddress)
{
  //This variable will hold the contents read from the i2c device.
  unsigned char data=0;

  //Send the register address to be read.
  Wire.beginTransmission(address);
  //Send the Register Address
  Wire.send(registerAddress);
  //End the communication sequence.
  Wire.endTransmission();

  //Ask the I2C device for data
  Wire.beginTransmission(address);
  Wire.requestFrom(address, 1);

  //Wait for a response from the I2C device
  if(Wire.available()){
    //Save the data sent from the I2C device
    data = Wire.receive();
  }

  //End the communication sequence.
  Wire.endTransmission();

  //Return the data read during the operation
  return data;
}
//This function is used to read the X-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
//NOTE: This value is NOT in degrees per second. 
//Usage: int xRate = readX();
int readX(void)
{
  int data=0;
  data = itgRead(itgAddress, GYRO_XOUT_H)<<8;
  data |= itgRead(itgAddress, GYRO_XOUT_L);  

  return data;
}
//This function is used to read the Y-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
//NOTE: This value is NOT in degrees per second. 
//Usage: int yRate = readY();
int readY(void)
{
  int data=0;
  data = itgRead(itgAddress, GYRO_YOUT_H)<<8;
  data |= itgRead(itgAddress, GYRO_YOUT_L);  

  return data;
}
//This function is used to read the Z-Axis rate of the gyroscope. The function returns the ADC value from the Gyroscope
//NOTE: This value is NOT in degrees per second. 
//Usage: int zRate = readZ();
int readZ(void)
{
  int data=0;
  data = itgRead(itgAddress, GYRO_ZOUT_H)<<8;
  data |= itgRead(itgAddress, GYRO_ZOUT_L);  

  return data;
}

There are five functions in this sketch, but three of them are very similar. The first function, itgWrite(), is used to write a value to a register on the ITG-3200. To use this function three parameters must be provided: the address, the registerAddress, and the data. The address is the I2C address of the sensor. As it turns out, more than one sensor can be connected to theI2C pins at the same time. In order for the sensors to know who is supposed to be getting the data, they each have a unique address. That’s what we’re providing with the ‘address’ parameter. The second parameter is the registerAddress. Like we discussed earlier, most sensors have a set of registers, and each register has it’s own address. The last parameter is the data to be written to the address. We can configure a parameter on a sensor by writing data to a register address.

The next function is the itgRead() function. This function allows us to read the data stored in the register of a sensor. The itgRead function requires two parameters, and it returns a character value. The parameters are similar to those in the itgWrite() function; the address is the I2C address of the sensor we want to read from, and the registerAddress is the address of the register we want to read. The function will send the contents of the register back.

Running the Sketch

Once you’ve connected the ITG-3200 breakout board to the Arduino you can upload the ITG3200 Basic Arduino sketch. To see the data from the gyroscope, just open the serial terminal with a baud rate setting of 9600. You’ll see values start streaming through the terminal window almost immediately. On each line of the terminal, there are three values: x, y, and z rotation values. Remember, we didn’t convert this data to degrees per second so the values that are being streamed are the ADC values from the ITG-3200. You may also notice that even if the gyroscope is sitting still (not rotating in any direction) the values aren’t reporting 0. This is because there is an inherent bias in the gyroscope. To get accurate measurements you’ll need to calibrate the readings. You can do this in the sketch by reading the values output from the sensor while it is sitting still and storing them into some variables. Then later, when the sensor values are being read, just offset the readings by the calibration values.


Want more information about SparkFun's classes? Interested in getting involved with teaching electronics? Just want to talk? Sign up for our newsletter, or contact our education department.

SparkFun is a company built around one core idea – sharing ingenuity. We think everyone should have the hardware and resources to learn and play with cool electronic gadgetry.

Share, give, learn, SparkFun.

Do you regularly instruct classes and workshops in a formal or informal learning environment? SparkFun offers Educator Discounts to people teaching and sharing electronics.

Find out more.