Arduino + Accelerometer: Tilt Detection

I made a little project using the ADX330 accelerometer and my arduino... I know its simple and its been done before (which I found out to my dismay AFTER i made this) but I am quite proud cause I am only a few weeks into the arduino and I made this code from scratch - all it took was the consumption of 10 cups of coffee and a few head scratching moments... ;)

anyway, enjoy:

Well done. I hope you enjoyed getting that working. What do you have planned for your next project?

2 jezuz

I bought the accelerometer and used it in 3D Ruby program for control of 3D-cube :)

thx guys, il post the code when i get home... i took a look at 3d ruby... and it seems pretty awesome, il play around with that a little, but my next project is a lillypad + accelerometer robotic hand cntrol project... im still working on the code behind it but its actually fairly simple... il let u knw when im done ;D

iight guys here is the code...

int groundpin = 18; // analog input pin 4 int powerpin = 19; // analog input pin 5 int xpin = 3; // x-axis of the accelerometer int ypin = 2; // y-axis int zpin = 1; // z-axis (only on 3-axis models) long xy_max = 470; long xy_min = 550;

int forwardPin = 9; int backPin = 10; int leftPin = 11; int rightPin = 12; int centerPin = 8;

long x = 0; long y = 0;

void setup() { Serial.begin(9600);

pinMode(forwardPin, OUTPUT); pinMode(backPin, OUTPUT); pinMode(leftPin, OUTPUT); pinMode(rightPin, OUTPUT);

pinMode(groundpin, OUTPUT); pinMode(powerpin, OUTPUT); digitalWrite(groundpin, LOW); digitalWrite(powerpin, HIGH); }

void loop() {

if (y < xy_max) { //backwards

digitalWrite(backPin, HIGH);

Serial.println("The arduino is moving(tilted) backwards");

} else if (y >= xy_max){

digitalWrite(backPin, LOW);

}

if (y > xy_min) { //forwards

digitalWrite(forwardPin, HIGH);

Serial.println("The arduino is moving(tilted) forwards");

} else if (y <= xy_min){

digitalWrite(forwardPin, LOW);

}

if (x > xy_min) { //left

digitalWrite(leftPin, HIGH);

Serial.println("The arduino is moving(tilted) left");

} else if (x <= xy_min){

digitalWrite(leftPin, LOW);

}

if (x < xy_max) { //right

digitalWrite(rightPin, HIGH);

Serial.println("The arduino is moving(tilted) right");

} else if (x >= xy_max){

digitalWrite(rightPin, LOW);

}

if (y >= xy_max && y <= xy_min && x <= xy_min && x >= xy_max) { // nothing

digitalWrite(forwardPin, LOW); digitalWrite(backPin, LOW); digitalWrite(centerPin, HIGH); digitalWrite(leftPin, LOW); digitalWrite(rightPin, LOW); Serial.println("The arduino is currently not moving(tilted) in any direction"); } }

Thanks for sharing.

You can simplify the position checks, for example you can do the y position check as follows:

if (y < xy_max) { //backwards digitalWrite(backPin, HIGH); Serial.println("The arduino is moving(tilted) backwards"); } else{ digitalWrite(backPin, LOW); }

This works because if y is not less than xy_max it must be greater than or equal , so you don't need to do that check

If you just wanted to control the led without the serial message you can simplify further. The following code will set the pin HIGH when x is less than xy_max and low otherwise

digitalWrite(backPin, (x

the expression (x

Have fun!

ure absolutely right, i just did that so it was easier to understand for me (i was doing it late, and when im tired i always try to write out code completely so that i understand it better, and thus dont make any stupid mistakes) but thx:) and il have some pix of the robotic arm up later tonight if ure interested ;)

Looking forward to seeing the pictures :)

the project has been put on hold due to difficulties with the damn robotic arm's motors... and im quite busy with schoolwork at the time... il be sure to get right back ontop of the work as soon as i can... ;)

ok, so i pcb'd the project..finally... but i was out of the good wires so i ended up just putting some crappy short ones so that atleast i could test if it sorta works, and so it would look valid... here are the picks:

http://img410.imageshack.us/gal.php?g=dscn0994.jpg

hey I am using Triple Axis Accelerometer Breakout - MMA7260Q. I dont understand the behavior of accelerometer. From where can I know xymin and xymax ? I tried to figure it out by myself. COuldnt do it.. please help if someone knows..

Nice :-)

Nice project! :) I'm going to start working with accelerometers too with the wii nunchuck. I have the connector from SparkFun

Thanks guys. Blast from the past, though (this thing must have been on page 30 by now).

I'm going to start working with accelerometers too with the wii nunchuck. I have the connector from SparkFun

I tried my luck with the wii nunchuck and it failed me miserably (I didn't use the premade connector tho, so perhaps I mucked up when resoldering the breakout wires :'(). If you get yours working, I'd enjoy seeing it in action. :)

hey I am using Triple Axis Accelerometer Breakout - MMA7260Q. I dont understand the behavior of accelerometer. From where can I know xymin and xymax ? I tried to figure it out by myself. COuldnt do it.. please help if someone knows..

Hello, freescale published a nice application notes ( http://www.freescale.com/files/sensors/doc/app_note/AN3107.pdf ) I guess it can help you. regards, reha

what are these values :

long xy_max = 470; long xy_min = 550;

what are these values :

long xy_max = 470; long xy_min = 550;

These are the limits for the values to use in the "if" statements.

They are backwards, as you can tell (max < min), but that's really just semantics.

What happens is a set of if statements check to see if the accelerometer x and y outputs are greater than (in this case) the min, or less than the x, and light up the led's accordingly.

Sir where we can buy that kind of Accelerometer? Thanks for reply

Great post, thanks for sharing. I'm looking forward to trying this with an MMA3671L, which is 3-axis. I'm sort of unclear how you derived your constants (xy_max and xy_min). Were these part of the specification for your chip, or did you derive these experimentally by just tilting the board around and looking at outputs on serial? Thanks!

So, I did this using just the X and Y accel pins from a 5dof board (http://www.sparkfun.com/products/9268), and had to recalibrate the xy_min and xy_max to:

long xy_max = 300; long xy_min = 350;

...but got it working!

I did the recalibration by just examining the serial output for each pin as I moved the board in space and noting the values as I moved it: