ADXL345 & ITG-3200 Data Concerns

Over the past few weeks I have been working on a self-balancing robot project. I have a Sparkfun 9DOF sensor stick that contains the sensors I have been implementing. However once receiving data from the ADXL345 accelerometer and ITG-3200 gyro it seems that for any given axis, + values are measured as 0<data<260 while - values return data in the data>65000 range. I find this confusing and wondered if anyone had any insight as to what is happening and how I can better understand this data. There seems to be no mention in either data sheet that would shed any light on my problem.

Many thanks, I have attached a sample of the data.

Andrew

Hi Andrew,
What codes are you using? I just "build" :sweat_smile: a balancing robot using the ITG-3205 Gyro and ADXL345 Acc starting with Fabio's FREEIMU code and it seemed to return good data.
Hope it helps,
Alex

The -65000 numbers looks suspiciously like you have some problem with signed or unsigned ints somewhere. Check the type of the number you are actually receiving from the device, and make sure you don't have to modifiy it somehow.

The current code is as follows:

float accel[3];
float gyro[3];

void setup()
{
  // Init serial output
  Serial.begin(57600);
  
    // Init sensors
  delay(50);  // Give sensors enough time to start
  I2C_Init();
  Accel_Init();
  Gyro_Init();
}

void loop()
{
  Read_Accel();
  Serial.print("#A:");
  Serial.print(accel[0]); Serial.print(",");
  Serial.print(accel[1]); Serial.print(",");
  Serial.print(accel[2]); Serial.println();
  
  Read_Gyro();
  Serial.print("#G:");
  Serial.print(gyro[0]); Serial.print(",");
  Serial.print(gyro[1]); Serial.print(",");
  Serial.print(gyro[2]); Serial.println();
}

// *******************I2C code to read the sensors************************
#include <Wire.h>

#define TO_DEG(x) (x * 57.2957795131)  // *180/pi

// Sensor I2C addresses
#define ACCEL_ADDRESS ((int) 0x53) // 0x53 = 0xA6 / 2
#define GYRO_ADDRESS  ((int) 0x68) // 0x68 = 0xD0 / 2


void I2C_Init()
{
  Wire.begin();
}

void Accel_Init()
{
  Wire.beginTransmission(ACCEL_ADDRESS);
  Wire.write(0x2D);  // Power register
  Wire.write(0x08);  // Measurement mode
  Wire.endTransmission();
  delay(5);
  Wire.beginTransmission(ACCEL_ADDRESS);
  Wire.write(0x31);  // Data format register
  Wire.write(0x08);  // Set to full resolution
  Wire.endTransmission();
  delay(5);
  
  // Adjust the output data rate to 100Hz (50Hz bandwidth)
  Wire.beginTransmission(ACCEL_ADDRESS);
  Wire.write(0x2C);  // Rate
  Wire.write(0x0A);  // Set to 100Hz, normal operation
  Wire.endTransmission();
  delay(5);
}

// Reads x, y and z accelerometer registers
void Read_Accel()
{
  int i = 0;
  byte buff[6];
  
  Wire.beginTransmission(ACCEL_ADDRESS); 
  Wire.write(0x32);  // Send address to read from
  Wire.endTransmission();
  
  Wire.beginTransmission(ACCEL_ADDRESS);
  Wire.requestFrom(ACCEL_ADDRESS, 6);  // Request 6 bytes
  while(Wire.available())  // ((Wire.available())&&(i<6))
  { 
    buff[i] = Wire.read();  // Read one byte
    i++;
  }
  Wire.endTransmission();
  {
    accel[0] = (((int) buff[3]) << 8) | buff[2];  // X axis (internal sensor y axis)
    accel[1] = (((int) buff[1]) << 8) | buff[0];  // Y axis (internal sensor x axis)
    accel[2] = (((int) buff[5]) << 8) | buff[4];  // Z axis (internal sensor z axis)
  }
}

void Gyro_Init()
{
  // Power up reset defaults
  Wire.beginTransmission(GYRO_ADDRESS);
  Wire.write(0x3E);
  Wire.write(0x80);
  Wire.endTransmission();
  delay(5);
  
  // Select full-scale range of the gyro sensors
  // Set LP filter bandwidth to 42Hz
  Wire.beginTransmission(GYRO_ADDRESS);
  Wire.write(0x16);
  Wire.write(0x1B);  // DLPF_CFG = 3, FS_SEL = 3
  Wire.endTransmission();
  delay(5);
  
  // Set sample rato to 100Hz
  Wire.beginTransmission(GYRO_ADDRESS);
  Wire.write(0x15);
  Wire.write(0x09);  //  SMPLRT_DIV = 9 (100Hz)
  Wire.endTransmission();
  delay(5);

  // Set clock to PLL with z gyro reference
  Wire.beginTransmission(GYRO_ADDRESS);
  Wire.write(0x3E);
  Wire.write(0x00);
  Wire.endTransmission();
  delay(5);
}

// Reads x, y and z gyroscope registers
void Read_Gyro()
{
  int i = 0;
  byte buff[6];
  
  Wire.beginTransmission(GYRO_ADDRESS); 
  Wire.write(0x1D);  // Sends address to read from
  Wire.endTransmission();
  
  Wire.beginTransmission(GYRO_ADDRESS);
  Wire.requestFrom(GYRO_ADDRESS, 6);  // Request 6 bytes
  while(Wire.available())  // ((Wire.available())&&(i<6))
  { 
    buff[i] = Wire.read();  // Read one byte
    i++;
  }
  Wire.endTransmission();
  {
    gyro[0] = -1 * ((((int) buff[2]) << 8) | buff[3]);    // X axis (internal sensor -y axis)
    gyro[1] = -1 * ((((int) buff[0]) << 8) | buff[1]);    // Y axis (internal sensor -x axis)
    gyro[2] = -1 * ((((int) buff[4]) << 8) | buff[5]);    // Z axis (internal sensor -z axis)
  }
}
    accel[0] = (((int) buff[3]) << 8) | buff[2];  // X axis (internal sensor y axis)

Walk us through what you understand this code to do, please.

My understanding is that it is shifting the high order byte by 8 places and then appending the low order byte to form an int. Now, where I get lost is why you are then storing this int in a float array.

PaulS:

    accel[0] = (((int) buff[3]) << 8) | buff[2];  // X axis (internal sensor y axis)

Walk us through what you understand this code to do, please.

My understanding is that it is shifting the high order byte by 8 places and then appending the low order byte to form an int. Now, where I get lost is why you are then storing this int in a float array.

Although I can not take all of the credit of this code (I used various components of the code provided here: GitHub - Razor-AHRS/razor-9dof-ahrs: AHRS Firmware for the SparkFun 9DOF Razor IMU and SparkFun 9DOF Sensor Stick) my understanding of this code is indeed that the bytes are shifted while the float array is implemented to reduce variables and simplify callback (the original code was much larger). I could be wrong and if so I would be glad of any suggestions.

You are not getting float data from the gyro or the accelerometer. Why are you storing ints in float arrays?

Well that is my mistake then, I'll see if I can re-write it without the arrays and see how that goes.

I'll see if I can re-write it without the arrays

The use of arrays isn't the problem. It's using arrays of the wrong type that is the problem.

I see, I did remove the arrays in case and as you said, the problem still persists. I'm sorry for all the questions, I'm just starting to figure some of this out and I've had some great help and support so far. Would the implementation of an integer array fix this issue?