Computers, Development Boards & Shields
Educational & Robotics Kits
Electronic Sensors
Motors & Controllers
Communication & Display
Electronic Components
Soldering & Lab Tools

0
MMA7361 accelerometer not reading 1G on the -Z axis

Hi, I recently purchased the MMA7361 accelerometer. It works great for the most part except that I am getting an odd reading for the (-z axis).
I calibrate it correctly on a flat surface and every other direction reads the correct 1G except for the -Z axis. It reads about .86-.87 G instead of 1G.

My pin connections are shown in the attached code (a modified version of a helpful code I found on this site).

I was wondering if you could provide help me with this problem. Thanks!

// Code for MMA7361 3-axis accelerometer: reading analog then converting to Gforce

const static boolean GFORCE = true;
const static boolean METERS = false;

byte sleepPin= 4;
byte GS= 7;
byte x= A0;
byte y = A1;
byte z = A2;

int gc = 92;

int z0 = 660; //defaults to read values
int x0 = 660; //defaults to read values
int y0 = 677; //defaults to reasonable values
boolean calibrated=false;

void setup() {
Serial.begin(9600);
begin();//runs setup
calibrate();
}

void loop() {
delay(1000); //Delay for readability

Serial.println("_________________________________");
Serial.print("X-Reading: ");
Serial.print(analogRead(x), DEC);
Serial.print(" , Y-Reading: ");
Serial.print(analogRead(y), DEC);
Serial.print(" , Z-Reading: ");
Serial.println(analogRead(z), DEC);
Serial.println("");

Serial.println("In G-FORCES: ");
readGx();
readGy();
readGz();
Serial.println();
Serial.println("In Meters Per Second-Squared: ");
readMx();
readMy();
readMz();
}

void begin() {
pinMode(sleepPin,OUTPUT);//output mode
pinMode(GS,OUTPUT);//output mode
pinMode(x, INPUT);//input mode
pinMode(y, INPUT);//input mode
pinMode(z, INPUT);//input mode

digitalWrite(GS,LOW);//sets GS mode
digitalWrite(sleepPin, HIGH);//turns off sleep mode and activates device
digitalWrite(x, HIGH);//turn on pull up resistor
digitalWrite(y, HIGH);//turn on pull up resistor
digitalWrite(z, HIGH);//turn on pull up resistor
}

void calibrate() {
Serial.println("Please place device right-side up on a flat surface. ");
delay(2000);//give user 4 seconds to comply.
Serial.println("Calibrating ");

z0 = readAverage(z, 10) - gc;//remove 1 G from the reading to get an "accurate” 0G
Serial.print("Z Axis Calibrated:"); Serial.println(z0);
x0 = readAverage(x, 10);
Serial.print("X Axis Calibrated: "); Serial.println(x0);
y0 = readAverage(y, 10);
Serial.print("Y Axis Calibrated: "); Serial.println(y0);

calibrated = true;
}

int readAverage(int pin, int samples) {
long readings = 0;

for(int i = 0; i < samples; i++) {
readings += analogRead(pin);
}

return (readings/samples);
}

float convert(float myReading, float my0G) {
return convert(myReading, my0G, GFORCE);//defaults to G-Forces
}

//converts to m/s^2 using conventional value for gravity.
//if you are on a different planet you may need to use a
//value for your conversions.
float convert(float myReading, float my0G, bool myUnits) {
float myResult=((my0G-myReading)/(gc));
if(myUnits)
return myResult; //G-Forces
else
return (myResult * 9.80665);
}

void readGx() {
Serial.print("X Reading: ");
Serial.println(convert(analogRead(x), x0), DEC);
}

void readGy() {
Serial.print("Y Reading: ");
Serial.println(convert(analogRead(y), y0), DEC);
}

void readGz() {
Serial.print("Z Reading: ");
Serial.println(convert(analogRead(z), z0), DEC);
}

void readMx() {
Serial.print("X Reading: ");
Serial.println(convert(analogRead(x), x0, METERS), DEC);
}

void readMy() {
Serial.print("Y Reading: ");
Serial.println(convert(analogRead(y), y0, METERS), DEC);
}

void readMz() {
Serial.print("Z Reading: ");
Serial.println(convert(analogRead(z), z0, METERS), DEC);
}

X-Reading: 651 , Y-Reading: 636 , Z-Reading: 547

In G-FORCES:
X Reading: 0.0760869503
Y Reading: 0.0760869503
Z Reading: 0.8804347991

In Meters Per Second-Squared:
X Reading: 0.8527522087
Y Reading: 0.5329700469
Z Reading: 8.7407102584
_________________________________
X-Reading: 651 , Y-Reading: 637 , Z-Reading: 548

In G-FORCES:
X Reading: 0.0652173900
Y Reading: 0.0543478250
Z Reading: 0.8586956024

In Meters Per Second-Squared:
X Reading: 0.5329700469
Y Reading: 0.5329700469
Z Reading: 8.6341161727
_________________________________
X-Reading: 651 , Y-Reading: 637 , Z-Reading: 547

In G-FORCES:
X Reading: 0.0652173900
Y Reading: 0.0543478250
Z Reading: 0.8586956024

In Meters Per Second-Squared:
X Reading: 0.5329700469
Y Reading: 0.5329700469
Z Reading: 8.4209280014
_________________________________
X-Reading: 651 , Y-Reading: 636 , Z-Reading: 547

In G-FORCES:
X Reading: 0.0652173900
Y Reading: 0.0543478250
Z Reading: 0.8586956024

In Meters Per Second-Squared:
X Reading: 0.4263761043
Y Reading: 0.5329700469
Z Reading: 8.5275211334
_________________________________
X-Reading: 650 , Y-Reading: 636 , Z-Reading: 547

In G-FORCES:
X Reading: 0.0543478250
Y Reading: 0.0543478250
Z Reading: 0.8804347991

In Meters Per Second-Squared:
X Reading: 0.4263761043
Y Reading: 0.5329700469
Z Reading: 8.4209280014
_________________________________
X-Reading: 652 , Y-Reading: 636 , Z-Reading: 548

In G-FORCES:
X Reading: 0.0543478250
Y Reading: 0.0652173900
Z Reading: 0.8695652008

In Meters Per Second-Squared:
X Reading: 0.4263761043
Y Reading: 0.5329700469
Z Reading: 8.4209280014
_________________________________
X-Reading: 650 , Y-Reading: 636 , Z-Reading: 548

In G-FORCES:
X Reading: 0.0543478250
Y Reading: 0.0652173900
Z Reading: 0.8695652008

In Meters Per Second-Squared:
X Reading: 0.4263761043
Y Reading: 0.4263761043
Z Reading: 8.6341161727
_________________________________
X-Reading: 651 , Y-Reading: 636 , Z-Reading: 550

In G-FORCES:
X Reading: 0.0543478250
Y Reading: 0.0543478250
Z Reading: 0.8695652008

In Meters Per Second-Squared:
X Reading: 0.5329700469
Y Reading: 0.6395641326
Z Reading: 8.4209280014
_________________________________
X-Reading: 653 , Y-Reading: 637 , Z-Reading: 549

In G-FORCES:
X Reading: 0.0434782600
Y Reading: 0.0652173900
Z Reading: 0.8586956024

In Meters Per Second-Squared:
X Reading: 0.4263761043
Y Reading: 0.6395641326
Z Reading: 8.7407102584
_________________________________
X-Reading: 650 , Y-Reading: 634 , Z-Reading: 546

In G-FORCES:
X Reading: 0.0652173900
Y Reading: 0.0869565200
Z Reading: 0.8586956024

In Meters Per Second-Squared:
X Reading: -0.2131880521
Y Reading: 0.3197820663
Z Reading: 7.9945516586

1 Answer

0

From looking at your code, it looks like you took the actual library file instead of using a sketch that calls the library. Because of that I don’t think the Calibrate function is ever being called.

Make sure you have the library installed and try loading the default example code. The library is set up so that while standing still in the position it is calibrated to it should always be -1z, 0x, 0y regardless of if a Virtuabotix Accelerometer is even connected to your Arduino Uno.

Looking forward to hearing how things go.

Your Answer

No comments yet.

Leave a Reply