Home Sensor Projects Arduino LSM303 sensor example

Arduino LSM303 sensor example

by shedboy71

The LSM303 3D Accelerometers/Magnetometer Models are a system-in-package featuring a 3D digital linear acceleration sensor and a 3D digital magnetic sensor. These best-in-class eCompass devices enable superior PDR or unique use cases in emerging applications, including drones and personal navigation systems.

All full-scales available are fully selectable by the user. The device includes an I2C serial bus interface that supports standard and fast mode 100kHz and 400kHz.

Features

  • ST is enabling more accurate personal navigation and tracking for fitness and similar apps, with a new all-in-one eCompass IC
  • Advanced magnetic-sensing and MEMS technologies enable benchmark accuracy, with superior stability, power-saving and integration
  • 3 magnetic field channels and 3 acceleration channels
  • 16-bit data output
  • SPI and/or I2C serial interfaces
  • Analog supply voltage 2.16V to 3.6V
  • Power-down mode / low-power mode
  • Programmable interrupt generators for freefall, motion detection and magnetic field detection
  • Embedded temperature sensor

Here is a picture of the module I bought

 

Now lets look at a layout showing how to connect the module to our Arduino

Schematic and layout

As this module has a 3v3 voltage regulator you can connect this to your Arduino’s 5v.

Code

[codesyntax lang=”cpp”]

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

LSM303 compass;

char report[80];

void setup()
{
  Serial.begin(9600);
  Wire.begin();
  compass.init();
  compass.enableDefault();
}

void loop()
{
  compass.read();

  snprintf(report, sizeof(report), "A: %6d %6d %6d    M: %6d %6d %6d",
    compass.a.x, compass.a.y, compass.a.z,
    compass.m.x, compass.m.y, compass.m.z);
  Serial.println(report);

  delay(500);
}

[/codesyntax]

 

Testing

Open the Serial Monitor window and you should see something like this

A: 1408 -10144 13072 M: -54 149 -458
A: 2944 -8896 13152 M: -56 121 -469
A: -2704 -10432 -1216 M: 346 431 200
A: 896 -8992 20512 M: 172 456 -259
A: 21296 -14512 25712 M: -171 481 275
A: -14000 16512 -12064 M: 242 141 475
A: 7296 -10800 -7216 M: -409 362 220
A: 31648 1840 -5344 M: -551 205 52
A: -272 30544 -6688 M: 248 -440 335
A: -6992 -7504 -3472 M: -13 566 -91
A: -1632 -16992 20464 M: -53 584 49

 

Links

Accelerometer + 3 Axis Magnetometer Module Sensor

You may also like

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More