9DOF Stick ADXL345 Problem Left/Right values

Hi there,

i bought 9DOF Stick i succesfully upload and run AHRS code with examples.

but after that i found problem in my project i need values -x and x+ from accelerometer.

so i can detect Left and right movement along x-axis… but ADXL345 just give me positive values.

if i shift it to the left it give me positive value.

does anybody know how detect left/right movement from it ?

Thanks

(iam newbie)

Perhaps you could show us the code that combines the 2 bytes for each axis back into 1 reading ? Do the other axes work, in that they show both + and - values when appropriate ? Have you tried aiming the X axis both straight up and then down to see what 1G due to gravity reads out as ? How have you got the FIFO setup ?

I think it is more of an issue of orientation, and conditioning the signal. Assuming the X axis is pointing at the directions it will be moving, first you will have to find the center. This is achieved by holding the module completely still and taking a reading(preferably a sum of multiple readings). This is your center. So when the value comes in you would subtract the center from it, thus giving you a positive and negative value. Positive in one direction, and negative in the other. Make sure to make the variable signed, otherwise it is impossible for you to get a negative number.

Thanks for answers.

i guess my problem is because i noticed

when i have my device flat. it show me 0.04 -0.05 . if i just take it and put on table verticaly.

it show me 250 values . why its that ? i should be same …when i have it flat or verticaly isnt it ?

then it seems iam making acceleration but iam not it just stand but verticaly.

here is my code:

#include <Wire.h>
#define ACCEL_ADDRESS ((int) 0x53) // 0x53 = 0xA6 / 2

// Arduino backward compatibility macros
#if ARDUINO >= 100
  #define WIRE_SEND(b) Wire.write((byte) b) 
  #define WIRE_RECEIVE() Wire.read() 
#else
  #define WIRE_SEND(b) Wire.send(b)
  #define WIRE_RECEIVE() Wire.receive() 
#endif



void setup(){
  Wire.begin();
   Serial.begin(9600);  // start serial for output
  
  Serial.println("setup");
  Accel_Init();
}

void loop(){
 
  
  
    int i = 0;
  byte buff[6];
  
  Wire.beginTransmission(ACCEL_ADDRESS); 
  WIRE_SEND(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_RECEIVE();  // Read one byte
    i++;
  }
  Wire.endTransmission();
  
  int x,y,z;
  char str[512]; 
  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.
    x = (((int) buff[3]) << 8) | buff[2];  // X axis (internal sensor y axis)
    y = (((int) buff[1]) << 8) | buff[0];  // Y axis (internal sensor x axis)
    z = (((int) buff[5]) << 8) | buff[4];  // Z axis (internal sensor z axis)
     
     Serial.println(x);

     
    
  }
  else
  {
   Serial.println("!ERR: reading accelerometer");
  }
  
}



void Accel_Init()
{
  Wire.beginTransmission(ACCEL_ADDRESS);
  WIRE_SEND(0x2D);  // Power register
  WIRE_SEND(0x08);  // Measurement mode
  Wire.endTransmission();
  delay(5);
  Wire.beginTransmission(ACCEL_ADDRESS);
  WIRE_SEND(0x32);  // Data format register
  WIRE_SEND(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_SEND(0x2C);  // Rate
  WIRE_SEND(0x09);  // Set to 50Hz, normal operation
  Wire.endTransmission();
  delay(5);
}

flimbo:
Thanks for answers.

i guess my problem is because i noticed

when i have my device flat. it show me 0.04 -0.05 . if i just take it and put on table verticaly.

it show me 250 values . why its that ? i should be same …when i have it flat or verticaly isnt it ?

then it seems iam making acceleration but iam not it just stand but verticaly.

When you stand it vertically, it is sensing the 1G force of gravity. Depending on how you have it setup, the 250 counts might make sense. One possible LSB weighting is 0.004 Gs/LSB, so 250 LSBs would give you a 1G reading = gravity.

As for the device not sensing the left/right movements … if that was your original problem … you need to understand that any accelerometer only senses acceleration, a change in the speed. If you push your device and it get’s up to a constant speed, the accelerometer should only be different from ~0 reading while the speed is changing. In my example I’d expect some reading for a short time and then it’ll read ~0 again. Is that what you were seeing ?

Yes,

but when iam pusing it to the right it starts give me values 0.5,0.8 …etc and during this phase suddendly it give me also some negative bunch of values so its

0.5

0.8

1.2

1.5

-0.6

-0.5

0.3

0.6

or this negative values are because accleration is slowing down. hmm… but then its too much sensitive, coz iam pushing it slowly just on one side.

flimbo:
or this negative values are because accleration is slowing down.

Bingo !

As for being too sensitive … you can always write the code to ignore values less than some “high” threshold.

yes :smiley:

Thanks all for answers ! :violin: