Problem with ADXRS Gyro and Arduino Mega

Hi all!

So, I’ve been using a ADXRS Gyro on an Arduino Mega:

(Here is the code):

 /*
  ADXL3xx
  
  Reads an Analog Devices ADXL3xx accelerometer and communicates the
  acceleration to the computer.  The pins used are designed to be easily
  compatible with the breakout boards from Sparkfun, available from:
  http://www.sparkfun.com/commerce/categories.php?c=80
  http://www.arduino.cc/en/Tutorial/ADXL3xx
  The circuit:
  5v = analog 4 = 59
  ground = analog 5 = 60
  rate = analog 6 = 61
  2,5v = analog 7 = 62
  st2 = analog 8 = 63
  st1 = analog 9 = 64
  st3 = analo 10 = 65
  y accel = analog 11 = 66
  x accel = analog 12 = 67
 */
 // these constants describe the pins. They won't change:
 const int groundpin = 60;             // ground = analog 5 = 60
 const int powerpin = 59;              // 5v = analog 4 = 59
 const int xpin = 67;                  // x-axis of the accelerometer
 const int ypin = 66;                  // y-axis
 const int ST1 = 64;                   // self-test 1
 const int ST2 = 63;                   // self-test 2
 const int ST3 = 65;                   // self-test 3
 const int volt = 62;                   // 2,5 volt
 const int Rateout = 61;               // Rate pin

 void setup()
 {
   // initialize the serial communications:
   Serial.begin(9600);
   // Provide ground and power by using the analog inputs as normal
   // digital pins.  This makes it possible to directly connect the
   // breakout board to the Arduino.  If you use the normal 5V and
   // GND pins on the Arduino, you can remove these lines.
   pinMode(groundpin, OUTPUT);
   pinMode(powerpin, OUTPUT);
   digitalWrite(groundpin, LOW); 
   digitalWrite(powerpin, HIGH);
 }
 void loop()
 {
   
  // print the sensor values:
   Serial.print("xpin: ");
   Serial.print(analogRead(xpin));
   // print a tab between values:
   Serial.print("\t");
   Serial.print("ypin: ");
   Serial.print(analogRead(ypin));
   // print a tab between values:
   Serial.print("\t");
   Serial.print("gyro: ");
   Serial.print(analogRead(Rateout));
   // print a tab between values:
   Serial.print("\t");
   Serial.print("Volt: ");
   Serial.print(analogRead(volt));
   delay(1000);
 }

It’s compiling, but when I upload on the microcontroller, the data I get is not correct.

Here are some examples:

xpin: 452      ypin: 411      gyro: 1019       Volt: 512
xpin: 467      ypin: 437      gyro: 1019        Volt: 512
xpin: 466      ypin: 436      gyro: 1019        Volt: 512
xpin: 467      ypin: 432      gyro: 1019        Volt: 512
xpin: 463      ypin: 434      gyro: 1019        Volt: 512
xpin: 467      ypin: 430      gyro: 1019        Volt: 512
xpin: 464      ypin: 434      gyro: 1019        Volt: 512
xpin: 464      ypin: 428      gyro: 1019        Volt: 512
xpin: 463      ypin: 430      gyro: 1019        Volt: 512
xpin: 461      ypin: 429      gyro: 1019        Volt: 512
xpin: 463      ypin: 426      gyro: 1019        Volt: 512
xpin: 460      ypin: 429      gyro: 1019        Volt: 512
xpin: 462      ypin: 424      gyro: 1019        Volt: 512
xpin: 458      ypin: 427      gyro: 1019        Volt: 512
xpin: 459      ypin: 425      gyro: 1019        Volt: 512
xpin: 461      ypin: 425      gyro: 1019        Volt: 512
xpin: 459      ypin: 427      gyro: 1019        Volt: 512
xpin: 462      ypin: 422      gyro: 1019        Volt: 512
xpin: 461      ypin: 429      gyro: 1019        Volt: 512
xpin: 461      ypin: 425      gyro: 1019        Volt: 512
xpin: 463      ypin: 425      gyro: 1019        Volt: 512
xpin: 461      ypin: 426      gyro: 1019        Volt: 512
xpin: 465      ypin: 426      gyro: 1019        Volt: 512
xpin: 460      ypin: 428      gyro: 1019        Volt: 512
xpin: 463      ypin: 423      gyro: 1019        Volt: 512
xpin: 461      ypin: 424      gyro: 1019        Volt: 512

I’m happy with the Volt section, because its supposed to be 2.5v out of 5v. But, how come the y and x axis moves if the sensor is on my table?

Also, when I rotate the gyro (not in this example) it stays 1019. Even if I take out the sensor from the board, the volts go back to 0 but the values of x, y and gyro: they still vary.

Thanks!!

Well, your x and y don't vary much - what's the 0g point for the accelerometer you're using?

What happens to the x & y values when you tilt it?

Here is the datasheet of the IMU: http://www.sparkfun.com/datasheets/Accelerometers/IMU_Combo_Board-v2.pdf

xpin: 150      ypin: 192      gyro: 1019      Volt: 1023
xpin: 147      ypin: 195      gyro: 1019      Volt: 1023
xpin: 147      ypin: 194      gyro: 1019      Volt: 1018
xpin: 151      ypin: 199      gyro: 1019      Volt: 3
xpin: 148      ypin: 199      gyro: 1019      Volt: 1022
xpin: 148      ypin: 191      gyro: 1019      Volt: 0
xpin: 151      ypin: 196      gyro: 1019      Volt: 0
xpin: 151      ypin: 200      gyro: 1019      Volt: 0
xpin: 147      ypin: 195      gyro: 1019      Volt: 0
xpin: 150      ypin: 194      gyro: 1019      Volt: 1022
xpin: 147      ypin: 192      gyro: 1019      Volt: 0
xpin: 148      ypin: 196      gyro: 1019      Volt: 0
xpin: 150      ypin: 193      gyro: 1019      Volt: 2
xpin: 149      ypin: 192      gyro: 1019      Volt: 0
xpin: 149      ypin: 199      gyro: 1019      Volt: 1021
xpin: 149      ypin: 193      gyro: 1019      Volt: 1022
xpin: 150      ypin: 198      gyro: 1019      Volt: 1023
xpin: 147      ypin: 193      gyro: 1019      Volt: 1022
xpin: 151      ypin: 196      gyro: 1019      Volt: 1023
xpin: 151      ypin: 201      gyro: 1019      Volt: 0
xpin: 151      ypin: 197      gyro: 1019      Volt: 2

This is what happen after a certain time, I didn't move the IMU.

Thanks for replying so fast! :D

EDIT: I don't see any major variations when I move the IMU.

OK, I missed that you're powering the device from logic pins, assuming a ratiometric output, the accelerometer values look reasonable. Can't help you with the gyro, though, sorry.

What is the setup and state for the self-test pins?

Actually, I don't know... How should I use them?

(BTW, is that normal that the volts sometimes are near 0 ? )

EDIT: I don't see any major variations when I move the IMU.

In you first post, the x-y values were around the mid-400s, in the last post they're mid-100s. That's quite a major variation in my book. Can I suggest you concentrate on one device at a time?

A horizontal x-y accelerometer should read roughly mid-range (0g acceleration). If you tilt one axis vertical, that axis will experience a 1g acceleration, whilst the other should still read roughly 0g. Try some different orientations and ignore the gyro for now.

Ok, I will go with the x axis only and try.

Make sure from the datasheet that the self-test pins are in the correct state.

But what globally are the test-pins for? Thanks!

The self-test pins are for..well guess!

Usually, on an accelerometer, the self-test applies an electrostatic charge to one of the axes, which causes a deflection which can then be read as an accleleration.

Obviously, in operation, you don't want this, so it's best to turn it off.

I don't have the datasheet for the device, only the app note for the board

See top right of page two.

Ok, I get it. I will search for the datasheet and send it to you.

Btw, is the analog in port 12 is 67 ?

Btw, is the analog in port 12 is 67 ?

Sorry, no exeperience of the Mega.

Very odd spec on page 2 of the app note (bottom left) - it says the X accleration is "identical" to the Y accel, though perpendicular to it (that's good), though voltages don't tally. Suggest you contact Sparkfun for clarification.

ok.

Datasheet:

You need to makes sure the self-test pins are driven low.

If you've got a 5V reference, and a 5V accelerometer supply, then 0g (horizontal) should be around 512 counts, so tilting 90 degrees will give +/- 1V, so your readings should vary by about +/- 200 counts.

Self-test pin driven low:

   digitalWrite(ST1, LOW); 
   digitalWrite(ST2, LOW);
   digitalWrite(ST3, LOW);

Should I use analogWrite?

I've tried and no major variations when I rotate, move the IMU.

Is there a Serial.print("") command that I could use to do a «carriage return» in the data I receive?

Print a '\n'.