Intro


This tutorial is for a Sparkfun Triple Axis Accelerometer breakout board using a Freescale MMA7260 Accelerometer. This accelerometer can be used with an extension Library Mma7260q which simplifies some initialization values and tilt detection. I will post code examples which use the library and which do not. The library does show some interesting topics regarding initialization of range and tilt detection for you Arduino. At the bottom of the page is a list of links which point to tutorials, forum discussions, purchase links and projects using accelerometers.

ADX335

...more info coming soon.

Freescale MMA7260


SparkfunBreakoutBoard.jpg
Sparkfun Breakout Board
Breakout board has 8 pins and uses 3.3v power. It outputs acceleration data via the X,Y,Z pins on the breakout board. These represent accelerations on the respective axis. Tilt can be evaluated by comparing these accelerations versus the earths gravity, allowing this breakout board to be a 3 axis tilt sensor. It has 4 different sensitivity levels. Use the GS1 and GS2 pins to set levels. SLP pin is for sleep mode



GS1
GS2

LOW
LOW
1.5g
LOW
HIGH
2g
HIGH
LOW
4g
HIGH
HIGH
6g


Testing Output with Multimeter

Photo on 2012-12-05 at 16.39.jpg
Test output voltage of axis with multimeter

First with either accelerometer you want to test its output with a multimeter. You should get fairly stable data otherwise check your wiring and make sure all connections are snug. You'll need to hookup power and ground from the Arduino to the breakout board. Then connect the output from one axis to the positive of the multimeter, and the ground of the multimeter to the ground of your Arduino. Set your multimeter to read in the proper DC voltage range 0-20, on mine. Then tilt the breakout board and watch voltage changes. Try to find maximum, minimum and center values. Record maximum and minimum values they will be helpful later in the code. With my MMA7260 i was initially expecting values on each axis to be recorded within the range of 0-3.3V, however after testing with my multimeter I found that each axis had it's own range.
X : min .65V, max 2.25V
Y : min .84V, max 2.44V
Z : min .95V, max 2.5V

These voltage should be used as range constraints for incoming axis data when you want to remap to reflect an angle, midi value or whatever type of data you are using the Accelerometer to control.

Connect Accelerometer to Arduino

MMA7206BreakoutBoard.ArduinoUNO.jpg
Connected to Arduino

Pin Connection Table

Arduino

MMA7260

3.3v

Vcc

GND
GND
A0
X
A1
Y
A2
Z
GND
GS1
GND
GS2
3.3v
SLP
This will set your accelerometer to never be in sleep mode with a sensistivity of 1.5gs


Code


I tried loading the example sketch with mma7260q library Arduino IDE v1.0.2 you get an error with this library saying missing "WProgram.h" file. You have to go the directory containing the "mma7260.cpp" file, which you should have moved into the Arduino/libraries/Mma7260q folder, and change the below lines.
(note you should only have to do this if using Arduino v1.* or above)

change
#include "WProgram.h"
to
#include "Arduino.h"

Below is sample code to load onto your Arduino then open the Serial Monitor to see the values
/*
Arduino Code implementing the Mma7260 Library by Ruben Laguna
*/
#include <mma7260q.h>
int xPin = 0; /*analog pin that X output from accelerometer is connected to*/
int yPin = 1; /*analog pin that Y output from accelerometer is connected to*/
int zPin = 2; /*analog pin that Z output from accelerometer is connected to*/
 
/*set up MMA7260Q*/
Mma7260q mma7260q = Mma7260q(xPin,yPin,zPin);
 
void setup(){
 
Serial.begin(9600); start serial port for print messages
delay(100);
 
/*
initial calibration of the MMA7260q
* you can get the correct values for your MMA7260q
by running this program and copying the max and min values
after rotating the mma7260q 360degrees around all axis
*/
mma7260q.autoZeroCalibration(1,1,1);
mma7260q.autoZeroCalibration(766,720,914);
 
}
void loop(){
 
float rho, phi, theta;
mma7260q.readTilt(&rho,&phi,&theta);
Serial.print("x = : ");
Serial.print(rho);
Serial.print(" y = : ");
Serial.print(phi);
Serial.print(" z = : ");
Serial.println(theta);
 
delay(500);
}
 

This sketch is helpful for smoothing out data from the accelerometer. The author first fills a table with samples. This is followed by putting the table in ascending order. Then the highest and lowest values from the table are overlooked while the rest get averaged.
(links section has code url)

/*
  Analog input, measures angle
  created 16 may 2010
  by Ehkrap Tnahsarp
 
*/
 
 
const int analogInPin = 0;  // Analog input pin that the potentiometer is attached to
float sensorValue = 0;
int angle = 0;
int sample[20];
int i, j, temp;
 
 
void setup() {
 
  Serial.begin(9600);       // initialize serial communications at 9600 bps:
 
  }
 
 
void loop() {
 
  for(i=0; i < 20; i++)                   // take 20 sample readings
{
 
  sensorValue = analogRead(analogInPin);  // read the analog in value:
 
  sample[i] = 0.75*( 220 - sensorValue);     //  considering 220 is value at Horizantal position
 
}
 
 
 
 for(i=0; i < 20; i++)            // BUBBLE SORTINHG puts readings in ascending order
      for(j=0; j < 20; j++)
      {
 
        if( sample[j] > sample[j+1])
         {   temp = sample[j];
            sample[j] = sample[j+1];
            sample[j+1] = temp;
         }
 
      }
 
 
 for ( i=5; i <= 15; i++)      // sum of angle values eleminating first five and last five
 
 angle = angle + sample[i];    // inconsistance readings
 angle = angle/10;            // Average of angle
 
 
  Serial.print("\t angle = ");      // Print the output
  Serial.print(angle);
 
 
} // end of loop



Links


Tom Igoe tutorial
Ruben Laguna's Example Page
Ruben Laguna's Arduino Library for MMA7260
Trashcan Accelerometer Project: contains formulas, code snippets
Project Helicopter wiki page
Freescale MMA7260 Datasheet
Sparkfun Page
Arduino ADXL3xx Accelerometer
AdaFruit ADXL3xx Tutorial
Auto Zero Calculation
Measuring Tilt with Low-g Accelerometers
Tilt Sensing Using Linear Accelerometers
Forum topic, Example Code
Ehkrap Tnahsarp code example