2

I have the following sensor stick: https://www.sparkfun.com/products/10724

It has an accelerometer, gyroscope, and magnetometer.

I have connected it to an arduino and am reading the accelerometer values while the sensor is completely flat and the values jump all over the place:

ie 0 2 3 7 -5 12 35 -3 ...

This is how I am reading them:

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);

  // Because our main loop runs at 50Hz we adjust the output data rate to 50Hz (25Hz bandwidth)
  Wire.beginTransmission(ACCEL_ADDRESS);
  Wire.write(0x2C);  // Rate
  Wire.write(0x09);  // Set to 50Hz, 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();

  if (i == 6)  // All bytes received?
  {
    // No multiply by -1 for coordinate system transformation here, because of double negation:
    // We want the gravity vector, which is negated acceleration vector.
    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)
  }
}

Am I doing something wrong that these values aren't the same?

3
  • What does your circuit look like? That may help... Jul 24 '14 at 23:20
  • Its just the arduino and sensor... sda -> pin A4 && scl -> pin A5 Jul 24 '14 at 23:21
  • What values do you get when you do shake it?
    – BrettAM
    Jul 25 '14 at 0:02
2

In 'FULL_RES' mode the number you get is amount of 4mg units.

So 1 is 4mg, 2 is 8mg, -4 = -32mg.

Also note that your current code doesn't interpret the first bit (so the 13th from the right) as the sign bit, so you'll never get negative values. (Question related to this)

PS I think it is actually more like 3.9mg.

2
  • How do I go about interpreting it as the sign bit? Jul 25 '14 at 21:32
  • Like I said, just follow the link in my answer. Basically just shift left 5 positions and then divide by 32. (accel[0] = (accel[0]<<5)/32)
    – Gerben
    Jul 26 '14 at 10:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.