Introduction: LSM303DLHC Accelerometer and Magnetometer Exploration

The LSM303 combines readings from the earth’s electromagnetic field as a magnetometer with readings of earth’s gravitational force as a triple-axis accelerometer. This information makes it a compact navigation system.

As it is, the LSM303 can be used as a basic compass, movement sensor or simply detecting free fall. However, if one combined the LSM303 with a triple-axis gyroscope sensor, such as the L3GD20, it would have more possible uses. This is for example how your phone acquires all its navigational information. Also, it would make for a more professional complete navigation system for aerial, terrestrial and marine use.

Step 1: Assembly and Wiring

Depending if you have a fully assembled LSM303, you may have to solder a header strip to the sensor. Each pin needs to be soldered to achieve a good electrical connection. If soldering isn’t something you have much experience with, check out this tutorial (https://learn.adafruit.com/adafruit-guide-excellen...) from Adafruit.

The LSM303 sensor needs to be placed in acertain distance from all other traces of metal or magnets. If not, this will cause disturbances in the readings and errors will occur. This is why you should use wires of a certain length as a buffer to keep the sensor at a safe distance.

If your project with the LSM303 is depending on a lot of rotation around its own axis, it will be a good idea to get your hands on a slip ring so your wiring won’t get all twisted.

The LSM303 is easy to connect to your arduino, as it only requires four wires. GND to GND, VIN to 5v (or 3.3v, depending on what you have available, SDA to SDA, and SCL to SCL. And remember – long wires.

Step 2: Calibration

The factory calibration of the LSM303 is accurate enough for most purposes. However, if you need more exact readings for your project, simply use the code which Adafruit has put up on their site (https://learn.adafruit.com/lsm303-accelerometer-s...)

The code reads the minimums and maximums on all three axes. When the code is uploaded, slowly rotate the LSM303 several times in the roll, pitch and heading axis. After repeating this several times, the values of the maximum and minimum for each axis can be used to adjust the output of the sensor.

Step 3: Smoothing and Data Fetching

Since the sensor is extremely precise, it picks up even the slightest change in the gravitational force. This causes a lot of small disturbances in the output values that we would like to avoid. This can be achieved by smoothing the readings before they are outputted as results.

This code takes care of giving you nice results and delivering it your computer. (https://github.com/martinhj/LSM303DLHCreading)

Step 4: Possibilities of the LSM303

Based on the introduction of the LSM303, this can now be used further to make small projects with your arduino and sensor.

Step 5: Gesture Lock

This sketch makes the use of the readings from the magnetometer and accelerometer LSM303DLHC. It also uses the libraries for this sensor made by adafruit (http://www.adafruit.com/products/1120?&main_page=p...).

It recognizes gestures in a simplified manner by checking if certain thresholds are breached when the sensor is moved. The codes for the arduino can be downloaded from this (https://github.com/martinhj/gestureLock) Github link.

Step 6: RGB LED

This RGB LED uses the magnetometer qualities of the LSM303 to control a different colored light to guide you in your desired direction - north, east, south or west.

In addition to the basic wiring of the LSM303 to the arduino, you will need more wires, a breadboard, one resistor and a RGB LED. The illustration to the left shows how to wire it all up. Red RGB to digital pin 9, green RGB to digital pin 10, blue RGB to digital pin 11, and ground to GND.

The codes for the arduino can be downloaded from this (https://github.com/martinhj/rgbMagnetometer) Github link.

Step 7: Processing Compass

This is a simple illustration of the results from the magnetometer represented in Processing.

Turning the sensor around will turn the arrow around on the screen.

https://github.com/martinhj/compass

(using the LSM303DLHCreading sketch presented in an earlier step).

Step 8: Processing 3D Box

This is a simple illustration of the results from the accelerometer represented in Processing. It takes the acceleration readings from the LSM303DLHC and turns around a box in 3D space. Since the acceleration of gravity always influences the sensor there will be readings pointing towards the earth’s surface. These readings will change when you turn the sensor in different directions.

https://github.com/martinhj/accelBox

(using the LSM303DLHCreading sketch presented in an earlier step).

Step 9: Further Reading

If you haven’t gotten enough of the magnetometer and accelerometer sensor yet, this instructable (https://www.instructables.com/id/Night-sky-explorer...) really shows the potential of the LSM303 by using it to bring the stars of the night sky intoH your own home.

Also, this tutorial (https://learn.adafruit.com/lsm303-accelerometer-sl...) from Adafruit, which also was of great help and inspiration in this project, on how to make your own north going Zaxen from Dr. Seuss’ children’s stories.

Another great source of information about the LSM303 can be found over at Pololu’s (http://www.pololu.com/file/0J434/LSM303DLH-compass...).

Finally, more detailed information about the LSM303 can be found in its datasheet (http://www.adafruit.com/datasheets/LSM303DLHC.PDF).