SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By saymyname
#116631
I am interfacing the Memsic 2125 accelerometer to the Arduino Pro Mini 5 V. I need to distinguish between the accelerometer being in the upright position and being upside down. As I rotated the accelerometer 180 degrees, the PulseIn values increased but then decreased back to the original upright value. I don't understand why this is happening and if it is right.
here is my code that I got from a tutorial:
const int yPin = 5; // Y output of the accelerometer
// variables to read the pulse widths:
int pulseY;
void setup() {
// initialize serial communications:
Serial.begin(9600);
// initialize the pins connected to the accelerometer
// as inputs:
pinMode(yPin, INPUT);
}

void loop() {

// read pulse from y-axis:
pulseY = pulseIn(yPin,HIGH);

// print the acceleration
Serial.print(pulseY);
Serial.println();

delay(1000);
}


This is the y-axis output as I rotated it 180 degrees along the axis:

4972 //upright position
4976
5260
5531
5691
5864
6149
6208
6175
6134
6011
5885
5729
5608
5482
5402
5252
5087
4979
4972 //upside down

If this seems right, how can I distinguish between the accelerometer being upright and upside down?
Thank you.
By esklar81
#116634
saymyname,

My initial hunch, when I read your posting, was that you had started with the accelerometer Y axis horizontal and rotated it through vertical and back to horizontal.

In what direction did you rotate it, as described using the coordinates shown in the "datasheet"? If you started with the pins vertical, then neither the X axis nor the Y axis should have been detecting gravity, because they are perpendicular to the pins.

I suggest you try other starting orientations and axes of rotation.

Happy Hunting,
Eric