...

Compass and Accelerometers


LSM303DLH 3D Compass and Accelerometer Carrier with Voltage Regulators:

Got this from Pololu.
https://github.com/pololu/LSM303 (library and notes on how to interface with Arduino)

Sample Arduino code to read from the LSM303 module, and the built in UART to dump to the serial monitor.

#include <Wire.h>
#include <LSM303.h>

/*
  Arduino  Mega  Device
     A5     D21   SCL
     A4     D20   SDA
*/

/*
  Note: don't really need that delay in setup(), if everything is
  being powered and run from the Arduino.
  When powering the accelerometer separately, (with common ground)
    - power up the accelerometer
    - power up the Arduino
    - you have delay time to start up the serial monitor

*/
LSM303 compass;

void setup() {
  Serial.begin(9600);
  delay(6000);
  Serial.println("init...!");
  Wire.begin();
  compass.init();
  compass.enableDefault();
  Serial.println("Accelerometer Test!");
}

void loop() {
  compass.read();

  Serial.print("A ");
  Serial.print("X: ");
  Serial.print((int)compass.a.x);
  Serial.print(" Y: ");
  Serial.print((int)compass.a.y);
  Serial.print(" Z: ");
  Serial.print((int)compass.a.z);

  Serial.print(" M ");
  Serial.print("X: ");
  Serial.print((int)compass.m.x);
  Serial.print(" Y: ");
  Serial.print((int)compass.m.y);
  Serial.print(" Z: ");
  Serial.println((int)compass.m.z);

  delay(100);
}

GY-26 Digital Compass Sensor Module:

GY-26-USART is a low cost plane digital compass module. The working principle is utilizing magnetoresistive sensor sensing the Earth's magnetic field component to get an azimuth angle. It communicates with upper computer through UART. Output format is ASCII. With solid state compass design, it has stable operation, high accuracy and advanced hard iron compensation function. It can overcome surrounding magnetic interference. Baud rate is optional. The product has three work modes; Normal mode, continuous mode and calibration mode. You can choose UART or IIC communication to use it.

Datasheet

Sample Arduino code:

void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
}
 
void loop() {
    Serial1.write(0x31);
    if (Serial1.available()) {
        int inByte = Serial1.read();
        Serial.write(inByte); 
    }
}              


/*
output is 8 bytes included per frame. 
should be able to return values ​​0-360, using just the 2,3 and 4 byte.
*/
char valuebyte[8]; 
int degs = 0; 
int counter = 0;
byte hasValue = 0;
 
void setup() {
    Serial.begin(9600);
    Serial1.begin(9600);
}
 
void loop() {
    hasValue = 0;
    Serial1.write(0x31);
    while(hasValue == 0){
        if(Serial1.available()){
            valuebyte[counter] = Serial1.read();
            counter = (counter + 1) % 8;
            if (counter == 0){
                degs = (valuebyte[2]-48)*100 + (valuebyte[3]-48)*10 + (valuebyte[4]-48);
                hasValue = 1; 
            }
        }
    }
    Serial.println(degs);
    delay(400);
}

Got this from dealExtreme.


GY-61 ADXL335 3-axis Analog Output Accelerometer Module

See this item on Sparkfun for details?

There is no on-board regulation, provided power should be between 1.8 and 3.6VDC.
int x, y, z;

void setup()
{
  Serial.begin(9600);      // sets the serial port to 9600
}

void loop()
{
  x = analogRead(0);       // read analog input pin 0
  y = analogRead(1);       // read analog input pin 1
  z = analogRead(2);       // read analog input pin 1
  Serial.print("accelerations are x, y, z: ");
  Serial.print(x, DEC);    // print the acceleration in the X axis
  Serial.print(" ");       // prints a space between the numbers
  Serial.print(y, DEC);    // print the acceleration in the Y axis
  Serial.print(" ");       // prints a space between the numbers
  Serial.println(z, DEC);  // print the acceleration in the Z axis
  delay(100);              // wait 100ms for next reading
}

Got from this seller's ebay listing.