Go Down

Topic: I2C endTransmission always return non-zero (Read 1 time) previous topic - next topic



Here is the code to get raw data from ITG3200. I got reading from ITG3200 for 3 minutes, but the data corrupted after that. Later, I found that endTransmission always return non-zero (itgWrite returns 2, itgRead returns 1). Is this normal? How could I fix this?

Thank you,

Code: [Select]

Basic Example Sketch for the ITG-3200 (http://www.sparkfun.com/products/9801)
SparkFun Electronics 2011
Ryan Owens

This code is public domain buy you buy me a beer if you use this and we meet someday (Beerware license).

To use this example code, attach:
Arduino  :  ITG-3200 Breakout
3.3V  :  VDD
3.3V  :  VIO
GND   :  GND
SDA   :  A4
SCL   :  A5

Load the sketch and open the serial window at 9600 bps. Arduino will output the raw X,Y and Z axis data being read from the gyroscope.


//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 = 0x68;

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

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

//This function will write a value to a register on the itg-3200.
//  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
  //Tell the I2C address which register we are writing to
  //Send the value to write to the specified register
  //End the communication sequence
  int test=Wire.endTransmission();

//This function will read the data from a specified register on the ITG-3200 and return the value.
//  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
//  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.
  //Send the Register Address
  //End the communication sequence.
  int test=Wire.endTransmission();
  //Ask the I2C device for data
  Wire.requestFrom(address, 1);
  //Wait for a response from the I2C device
    //Save the data sent from the I2C device
    data = Wire.read();
  //End the communication sequence.
  //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;

Hi Jason,

See "(I2C) Wire.endTransmission always returns non-zero." located here:




Hi Chris,

Thanks for you reply. I found that post as well, but it was on Feb. I thought they fixed the bug already, so does it still exist in 1.5.2?


I dunno.

I ended up coding around it, and haven't looked back since.

Take a look in \<arduino-x.x.x>\hardware\arduino\sam\system\libsam\source\twi.c and see if it's the same as shown in that post...

Be weary of wire.cpp too.



Hi Chris,

I just checked the code, they did not fix the bug in 1.5.2. How did you get around the problem?


Hi Jason,

Because I'm presently not working on this code, I tried to pluck out the relevant bits.




For anyone still wondering about this (like I was), this is a confirmed bug in the Wire library for Due. There is a fix here, just waiting to be merged into the Arduino Github repo:


So hopefully they will merge this soon and it will be available in the next beta 1.5 release. For now you can add those changes to the Wire.cpp file and build your own version of the Arduino IDE and use that to upload to your board.

Go Up

Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

via Egeo 16
Torino, 10131