Go Down

Topic: Accelerometer output unsteady (Read 584 times) previous topic - next topic

jools

Hi folks.  I'm trying to set up a device that will display "UP" "DOWN" "LEFT" "RIGHT" on an LCD display, using an MMA7361 accelerometer as the input sensor.  I've got it all set up, and the initial code written, but I can seem to get a stable output from the accelerometer.  I'm using 2 analog inputs as the x and y inputs (not using the z axis) and digital outs to the LCD.  When the code is initially executed the accelerometer is sitting flat, and it takes baseline readings for x and y.  While the program is running however,  the x and y values are jumping al over the place, even while the sensor sits still.  I'm not sure if the problem lies in the accelerometer, or if there is some sort of interference being introduced somewhere within the controller. Here's the code:

Code: [Select]
#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
int xIn=A4;
int yIn=A3;
int xbl;
int ybl;
int sl=1;


void setup(){
 lcd.begin(20, 4);
 Serial.begin(9600);
 pinMode(xIn, INPUT);      
 pinMode(yIn, INPUT);
 pinMode(A5, INPUT);
 pinMode(sl, OUTPUT);
 digitalWrite(sl, HIGH);
 xbl = analogRead(xIn);
 delay(500);
 ybl = analogRead(yIn);
 delay(500);
}


void loop(){
 int xPos=analogRead(xIn);
 delay(500);
 int yPos=analogRead(yIn);
 delay(500);
 int offset=65;
 
  if (xPos>xbl+offset) {
   lcd.clear();
   lcd.setCursor(7,1);
   lcd.write("LEFT");
   delay(500);
  }
  if (xPos<xbl-offset) {
    lcd.clear();
    lcd.setCursor(7,1);
    lcd.write("RIGHT");
    delay(500);
  }
  if (yPos>ybl+offset) {
    lcd.clear();
    lcd.setCursor(7,1);
    lcd.write("DOWN");
    delay(500);
  }
  if (yPos<ybl-offset) {
    lcd.clear();
    lcd.setCursor(8,1);
    lcd.write("UP");
    delay(500);
  }
  //Displays readings to Serial Monitor
   Serial.print("xbl ");
   Serial.print(xbl);
   Serial.print(" X: ");  
   Serial.print(xPos);
   //delay(1000);
   Serial.print(" ybl ");
   Serial.print(ybl);
   Serial.print(" Y: ");
   Serial.println(yPos);
   //delay(1000);
}


Has anybody ever come across an issue like this.  I'm pretty new to Arduino, so if I left any info out just let me know. Thanks.

jools

Alright... got the problem soved, but still don't quite understand.  The accelerometer requires a high on the S (Sleep) pin in order to operate. I was using pin 1 (digital out) to provide that voltage, and applied it in setup().  For some reason this was messing things up.  I took it off pin 1, and am now just applying a constant 5v from the power supply board I'm using. Works fine, everthing steadied right out.

mixania

I had the same Accelerometer and same problem. The Accelerometer is very simple to use and good for beginners. I had the same problem when I hooked it up to the breadboard. But then i got much better results when I actually soldered it to some wires. I think this is because conductivity increase when you solder the wires to it plus they stop disconnecting.

Here is some reference, I suggest connecting it with special wires or soldering it:
http://www.geeetech.com/wiki/index.php/MMA7361_Triple_Axis_Accelerometer_Breakout

I also used this tutorial but it's in French. Use Google Translate to translate it into English:
http://sebarduino.blogspot.com/2011/07/mma7361.html

Hope this helps!
Arduino Uno R3
Mac OSX Lion

Go Up