Using the accelerometer

The TeensyWiNo embeds a triple-axis accelerometer and a digital compass (magnetometer) in the same component : the LSM303CTR from STMicroelectronics. It is an I2C device that allows you to to measure acceleration or direction towards the center or the earth (by measuring gravity), and measure magnetic force in order to locate magnetic north.

What are the libraries needed ?

First of all, you will need to install relevant libraries.

Unified Sensor Driver

This library (Apache 2.0 licence) reduce all data to a single sensors_event_t and sets on specific, standardised SI units for each sensor family the same sensor types return values that are comparable with any other similar sensor. It becomes fairly easy to use all kind of sensors!

You will need it for most of sensors. Just pop at https://github.com/winocc/sensor and download it into your Arduino librairies folder.

LSM303CTR Driver

This driver is based on the former library and was originally developped by Adafruit. If this library is not already included in your Arduino installation, download and install this driver.

More information on the LSM303CTR can be found in the relevant datasheet.

My first code running

Once all librairies installed, just open the accelsensor example code that you will find under File > Example > Adafruit LSM303.

You can view it online here : https://github.com/winocc/LSM303/blob/master/examples/accelsensor/accelsensor.pde

Looking at the serial Monitor, you should get an output like :

X: 0.89 Y: 0.23 Z: 9.53 m/s^2
X: 0.89 Y: 0.23 Z: 9.52 m/s^2
X: 0.89 Y: 0.22 Z: 9.52 m/s^2
X: 0.88 Y: 0.22 Z: 9.53 m/s^2

In this situation, WiNo is standing still on the table and this is totally normal not to have 0 values for all coordinates : every object is under the influence of earth gravity (9.81 m/s^2).

Looking deeper, we can see that measured gravity by the WiNo is slighlty different. We get a measured earth gravity of 9.57 m/s^2.

Her again, this is totally normal. Every sensor needs to be more or less calibrated. You will find on our github account an example code with software calibration : on github or, again, under File > Example > Adafruit LSM303.

Going deeper

You can find some informations about "Computing tilt measurement and tilt-compensated e-compass", "Ellipsoid or sphere fitting for sensor calibration " or "Exploiting the gyroscope to update tilt measure and e-compass" here.

In some documents it is advised to use Quaternions and Cordic Algorithm

Here is a potentially useful reasearch paper but unfortunately a link to the source code is not provided :( Another one here.