Wemos LSM303 example

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

lsm303-module

This is a schematic of a typical module such as the one above

lsm303-module-schematic

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

Parts List

Here are the parts I used

 

Name Links
Wemos Mini
LSM303
Connecting cables

 

Schematic and layout

Quite a straightforward connection being an I2C device, here is what you need to connect

 

wemos-and-lsm303_bb

 

 

Code

You need to import the https://github.com/pololu/lsm303-arduino library, this is the example code

 

#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);
}

 

Testing

Open the Serial Monitor window and you should see something like this, move rthe module about to see the values change

A: 11760 -10560 -3648 M: -177 599 341
A: 8192 -256 -12224 M: -290 155 512
A: -6352 -2288 592 M: -240 609 133
A: 13424 3312 -9952 M: -458 168 343
A: 8848 -816 -9088 M: -398 152 431
A: 11104 4848 -7776 M: -457 75 355
A: 12896 6992 -7104 M: -465 60 348
A: 12720 7152 -6960 M: -462 53 348
A: 12752 7312 -6784 M: -459 48 343
A: 12800 7408 -6656 M: -459 44 342
A: 12816 7472 -6592 M: -464 41 337
A: 12864 7616 -6496 M: -466 36 337
A: 12896 7680 -6416 M: -464 35 336
A: 12864 7712 -6272 M: -465 34 333