[SOLVED] Can't get my accelerometer to work -- output seems random

I am using a MMA7361 board sold by Modern Device: shop.moderndevice.com/products/mma7260qt-3-axis-accelerometer

I hooked the accelerometer's Vin to the 5 Volt output on the Arduino, its GND to the ground on the Arduino, and its x, y and z outputs up to the analog inputs 0, 1 and 2 on the Arduino.

The problem is, my results make no sense. When I leave the accelerometer sitting on the ground stationary, I get results that bounce around.

Here's my code:

#define xInputPin 0
#define yInputPin 1
#define zInputPin 2

int xValue, yValue, zValue;

void setup() {
    // start serial port at 9600 bits per second
  Serial.begin(9600);

}

void loop() {
      xValue = analogRead(xInputPin);
      yValue = analogRead(yInputPin);
      zValue = analogRead(zInputPin);
      Serial.print("X Value");      
      Serial.print("\t");
      Serial.print(xValue);
      Serial.print("\t");
      Serial.print("Y Value");      
      Serial.print("\t");
      Serial.print(yValue);
      Serial.print("\t");
      Serial.print("Z Value");      
      Serial.print("\t");
      Serial.print(zValue);
      Serial.print("\n");
      delay(500);
    }

Here's my results:

X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	890	Y Value	840	Z Value	810
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	869	Y Value	814	Z Value	784
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	850	Y Value	789	Z Value	755
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	831	Y Value	766	Z Value	731
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	798	Y Value	725	Z Value	690
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	806	Y Value	734	Z Value	698
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	830	Y Value	763	Z Value	729
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	884	Y Value	834	Z Value	806
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	992	Y Value	966	Z Value	936
X Value	34	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	835	Y Value	770	Z Value	738
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	1001	Y Value	980	Z Value	959
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	930	Y Value	868	Z Value	836
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	999
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1007	Z Value	982
X Value	0	Y Value	0	Z Value	0
X Value	1005	Y Value	982	Z Value	958
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	899	Y Value	849	Z Value	819
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	1009	Y Value	986	Z Value	962
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	864	Y Value	807	Z Value	776
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	987	Y Value	956	Z Value	916
X Value	25	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	801	Y Value	729	Z Value	693
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	941	Y Value	879	Z Value	844
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	998	Y Value	974	Z Value	943
X Value	28	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	689	Y Value	600	Z Value	576
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	742	Y Value	659	Z Value	631
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023
X Value	0	Y Value	0	Z Value	0
X Value	789	Y Value	714	Z Value	677
X Value	0	Y Value	0	Z Value	0
X Value	1023	Y Value	1023	Z Value	1023

Am I missing something simple?

To me, they look too bouncy even to have any sense filtered into them.

Normally, you will need filtering (low pass is fine) in the code.

Something like:

Adjust alpha for the level of smoothing.

#define alpha 0.8

void loop()
{
 static double lastX = 0;
 double newX = readAccelerometerX();
 double smoothX = alpha * lastX + (1 - alpha) * newX;
 lastX = newX;
 // ditto for the other 2 axes
}

I have this accelerometer and it's working fine. When lying around, it's around 512 on every channel with a couple levels changing up and down, when I enabled 6g accuracy. Care to share a picture? How are you connecting it to arduino?

liudr:
Care to share a picture? How are you connecting it to arduino?

Unfortunately, I can't put up a picture right now. But I have the accelerometer on a breadboard with wires from the x, y and z outputs and to the Vin and GND inputs. I've tried using both 5 Volts and 3.3 Volts to power the accelerometer.

When lying around, it's around 512 on every channel with a couple levels changing up and down, when I enabled 6g accuracy.

So x, y and z should all be at about 512 when the jumper to select 6g accuracy is used? I thought the z axis would be different, due to gravity.

Si:
Normally, you will need filtering (low pass is fine) in the code.

Thanks for the suggested filter code. I'll try that if I ever get some results that make sense.

Right I was thinking about my other 2-axis accelerometer. The z shows an offset from 512. The best way to get help is to post both pictures and code.

Well the chip is 3.3V and the module has a regulator on board to drop to that, so powering with 5V to the modules Vin pin is the correct thing.

If you've connected 5V to the Vdd pin of the module I'm afraid you will have fried it.

The output voltages should be centred on 1.65V BTW.

Thanks for the comments. Since it seemed as though my pin connections and code were as they should be, I checked the wiring and found a problem with my breadboard. Fixed that, and I'm getting good results now.

So now I'm ready to try the filtering and see what I can come up with. I'm going to use the accelerometer in a car to give feedback on the car's motion.

Thanks, all.