The LSM303DLH is a triple axis accelerometer combined with a triple axis magnetic sensor. 
This breakout board uses the LSM303DLH to give you the data you need to feed into a microcontroller and calculate tilt-compensated output.

Applications

  • Compensated compassing
  • Map rotation
  • Position detection
  • Motion-activated functions
  • Free-fall detection
  • Intelligent power-saving for handheld devices
  • Display orientation
  • Gaming and virtual reality input devices
  • Impact recognition and logging
  • Vibration monitoring and compensation

Specifications

  • Power supply: 3.6~8V DC
  • Chip: LSM303DLH
  • ±1.3 to ±8,1 gauss magnetic field full-scale
  • ±2 g/±4 g/±8 g dynamically selectable fullscale
  • 16-bit data out
  • I2C serial interface
  • 2 independent programmable interrupt
  • generators for free-fall and motion detection
  • Embedded self-test
  • Accelerometer sleep-to-wakeup function
  • 6D orientation detection
  • Dimensions: 20.5mmx20.5mm 
  • Weight: 1g 

Parts list

  • LSM303 breakout board     x1

 

 
/* LSM303DLH Example Code
 
   LSM303 Breakout ---------- Arduino
         Vin                   5V
         GND                   GND
         SDA                   A4
         SCL                   A5
*/
#include <Wire.h>
#include <math.h>
 
#define SCALE 2  // accel full-scale, should be 2, 4, or 8
 
/* LSM303 Address definitions */
#define LSM303_MAG  0x1E  // assuming SA0 grounded
#define LSM303_ACC  0x18  // assuming SA0 grounded
 
#define X 0
#define Y 1
#define Z 2
 
/* LSM303 Register definitions */
#define CTRL_REG1_A 0x20
#define CTRL_REG2_A 0x21
#define CTRL_REG3_A 0x22
#define CTRL_REG4_A 0x23
#define CTRL_REG5_A 0x24
#define HP_FILTER_RESET_A 0x25
#define REFERENCE_A 0x26
#define STATUS_REG_A 0x27
#define OUT_X_L_A 0x28
#define OUT_X_H_A 0x29
#define OUT_Y_L_A 0x2A
#define OUT_Y_H_A 0x2B
#define OUT_Z_L_A 0x2C
#define OUT_Z_H_A 0x2D
#define INT1_CFG_A 0x30
#define INT1_SOURCE_A 0x31
#define INT1_THS_A 0x32
#define INT1_DURATION_A 0x33
#define CRA_REG_M 0x00
#define CRB_REG_M 0x01
#define MR_REG_M 0x02
#define OUT_X_H_M 0x03
#define OUT_X_L_M 0x04
#define OUT_Y_H_M 0x05
#define OUT_Y_L_M 0x06
#define OUT_Z_H_M 0x07
#define OUT_Z_L_M 0x08
#define SR_REG_M 0x09
#define IRA_REG_M 0x0A
#define IRB_REG_M 0x0B
#define IRC_REG_M 0x0C
 
/* Global variables */
int accel[3];  // we'll store the raw acceleration values here
int mag[3];  // raw magnetometer values stored here
float realAccel[3];  // calculated acceleration values here
 
void setup()
{
  Serial.begin(9600);  // Serial is used for debugging
  Wire.begin();  // Start up I2C, required for LSM303 communication
  initLSM303(SCALE);  // Initialize the LSM303, using a SCALE full-scale range
}
 
void loop()
{
  getLSM303_accel(accel);  // get the acceleration values and store them in the accel array
  while(!(LSM303_read(SR_REG_M) & 0x01))
    ;  // wait for the magnetometer readings to be ready
  getLSM303_mag(mag);  // get the magnetometer values, store them in mag
  //printValues(mag, accel);  // print the raw accel and mag values, good debugging
   
  for (int i=0; i<3; i++)
    realAccel[i] = accel[i] / pow(2, 15) * SCALE;  // calculate real acceleration values, in units of g
   
  /* print both the level, and tilt-compensated headings below to compare */
  Serial.print(getHeading(mag), 3);  // this only works if the sensor is level
  Serial.print("\t\t");  // print some tabs
  Serial.println(getTiltHeading(mag, realAccel), 3);  // see how awesome tilt compensation is?!
  delay(100);  // delay for serial readability
}
 
void initLSM303(int fs)
{
  LSM303_write(0x27, CTRL_REG1_A);  // 0x27 = normal power mode, all accel axes on
  if ((fs==8)||(fs==4))
    LSM303_write((0x00 | (fs-fs/2-1)<<4), CTRL_REG4_A);  // set full-scale
  else
    LSM303_write(0x00, CTRL_REG4_A);
  LSM303_write(0x14, CRA_REG_M);  // 0x14 = mag 30Hz output rate
  LSM303_write(0x00, MR_REG_M);  // 0x00 = continouous conversion mode
}
 
void printValues(int * magArray, int * accelArray)
{
  /* print out mag and accel arrays all pretty-like */
  Serial.print(accelArray[X], DEC);
  Serial.print("\t");
  Serial.print(accelArray[Y], DEC);
  Serial.print("\t");
  Serial.print(accelArray[Z], DEC);
  Serial.print("\t\t");
   
  Serial.print(magArray[X], DEC);
  Serial.print("\t");
  Serial.print(magArray[Y], DEC);
  Serial.print("\t");
  Serial.print(magArray[Z], DEC);
  Serial.println();
}
 
float getHeading(int * magValue)
{
  // see section 1.2 in app note AN3192
  float heading = 180*atan2(magValue[Y], magValue[X])/PI;  // assume pitch, roll are 0
   
  if (heading <0)
    heading += 360;
   
  return heading;
}
 
float getTiltHeading(int * magValue, float * accelValue)
{
  // see appendix A in app note AN3192 
  float pitch = asin(-accelValue[X]);
  float roll = asin(accelValue[Y]/cos(pitch));
   
  float xh = magValue[X] * cos(pitch) + magValue[Z] * sin(pitch);
  float yh = magValue[X] * sin(roll) * sin(pitch) + magValue[Y] * cos(roll) - magValue[Z] * sin(roll) * cos(pitch);
  float zh = -magValue[X] * cos(roll) * sin(pitch) + magValue[Y] * sin(roll) + magValue[Z] * cos(roll) * cos(pitch);
 
  float heading = 180 * atan2(yh, xh)/PI;
  if (yh >= 0)
    return heading;
  else
    return (360 + heading);
}
 
void getLSM303_mag(int * rawValues)
{
  Wire.beginTransmission(LSM303_MAG);
  Wire.send(OUT_X_H_M);
  Wire.endTransmission();
  Wire.requestFrom(LSM303_MAG, 6);
  for (int i=0; i<3; i++)
    rawValues[i] = (Wire.receive() << 8) | Wire.receive();
}
 
void getLSM303_accel(int * rawValues)
{
  rawValues[Z] = ((int)LSM303_read(OUT_X_L_A) << 8) | (LSM303_read(OUT_X_H_A));
  rawValues[X] = ((int)LSM303_read(OUT_Y_L_A) << 8) | (LSM303_read(OUT_Y_H_A));
  rawValues[Y] = ((int)LSM303_read(OUT_Z_L_A) << 8) | (LSM303_read(OUT_Z_H_A));  
  // had to swap those to right the data with the proper axis
}
 
byte LSM303_read(byte address)
{
  byte temp;
   
  if (address >= 0x20)
    Wire.beginTransmission(LSM303_ACC);
  else
    Wire.beginTransmission(LSM303_MAG);
     
  Wire.send(address);
   
  if (address >= 0x20)
    Wire.requestFrom(LSM303_ACC, 1);
  else
    Wire.requestFrom(LSM303_MAG, 1);
  while(!Wire.available())
    ;
  temp = Wire.receive();
  Wire.endTransmission();
   
  return temp;
}
 
void LSM303_write(byte data, byte address)
{
  if (address >= 0x20)
    Wire.beginTransmission(LSM303_ACC);
  else
    Wire.beginTransmission(LSM303_MAG);
     
  Wire.send(address);
  Wire.send(data);
  Wire.endTransmission();
}

LSM303 Breakout Board - Tilt Compensated Compass

  • Brand: DFRobot
  • Product Code: DFR-SEN0079
  • Reward Points: 20
  • Availability: 1
  • £21.54

  • Ex Tax: £17.95
  • Price in reward points: 300

3-Axis Digital Gyro (Grove)

3-Axis Digital Gyro (Grove)

£19.18

3-Axis Digital Gyro is the world’s first single-chip, digital-output, 3-axis MEMS motion p ...

Qty: 3

3-Axis Digital Compass (Grove)

3-Axis Digital Compass (Grove)

£19.32

3-Axis Compass module, a member of grove family uses I²C based Honeywell HMC5883L digital compass

Qty: 2

3-Axis Digital Accelerometer (Grove) ±16g

3-Axis Digital Accelerometer (Grove) ±16g

£11.34

Based on the excellent ADXL-345, this digital 3-axis accelerometer has excellent EMI protection.

Qty: 12

3-Axis Digital Accelerometer (Grove) ±1.5g

3-Axis Digital Accelerometer (Grove) ±1.5g

£10.13

This 3-axis Accelerometer module is based on MMA7660FC with Digital Output I2C

Qty: 3

LSM303 Compass Module (.NET Gadgeteer Compatible)

LSM303 Compass Module (.NET Gadgeteer Compatible)

£28.01

This module carries LSM303DLH, which is a triple axis accelerometer combined with a triple axis ...

Out Of Stock

3-Axis Accelerometer - MMA7361

3-Axis Accelerometer - MMA7361

£13.98

The MMA7361 is a 3.3V part and outputs an analog voltage for each of the three outputs.

Qty: 1

3-Axis Accelerometer BMA220

3-Axis Accelerometer BMA220

£5.65

This Triple Axis Accelerometer with Bosch BMA220 is an ultra small triaxial, low-g acceleration ...

Qty: 3

3-Axis Magnetometer (Compass) Board - HMC5883L [1746]

3-Axis Magnetometer (Compass) Board - HMC5883L [1746]

£13.13

A magnetometer can sense where the strongest magnetic force is coming from, generally used to detect ...

Out Of Stock

LSM303DLHC High Performance E-Compass, 3D Accelerometer and 3D Magnetometer with I2C

LSM303DLHC High Performance E-Compass, 3D Accelerometer and 3D Magnetometer with I2C

£11.22

High performance e-compass, 3D accelerometer and 3D magnetometer module, I2C interface, LSM303DLHC ...

Qty: 3