Click here to Skip to main content
13,152,181 members (37,789 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 4 Feb 2009

ST Accelerometer Parser

, 12 Feb 2009
Rate this:
Please Sign up or sign in to vote.
A C# class used to communicate with ST's LIS3LV02DL Accelerometer


I bought a LIS3LV02DL accelerometer from ST Microelectronics. Some useful code (VB) was attached along with the device itself, but things needed to be sorted out...  Provided here are:

  • LIS3LV02DL_Parser, used to communicate with the device
  • AccelerationMeasurement, used to store and provide acceleration data
  • A sample Console Application, Program.cs, showing how to use the classes above


The accelerometer measures the static (gravity) and dynamic acceleration along the three carthesian axes: x, y, z. It can be configured to run in 40-640 Hz, using two different scales/sensitivities ±2g or ± 6g. More information about the device itself can be found here

Using the Code 

When you plug the device into the USB connector, a virtual Comport should appear. Take note of the port number (Right–click My Computer, click Manage, and then click Device Manager and Ports) that is assigned to your device, you'll need it to make this work.

A new LIS3LV02DL_Parser is created using that very same number:

LIS3LV02DL_Parser memsParser = new LIS3LV02DL_Parser(int portNr, TimeSpan t); 

Or, if you'd like to read the measurements whenever you like:

LIS3LV02DL_Parser memsParser = new LIS3LV02DL_Parser(int portNr); 

Now we can open the SerialPort using memsParser.Open().

The Parser can be configured to send a new AccelerationMeasurement at fixed intervals (first line of code above) or you can use the memsParser.Get() method if you'd like to read it out for yourself. Using the constructor in the first example raises an event, sending a new AccelerationMeasurement when the TimeSpan t has passed.

Before moving on with the Parser tutorial, I'll give you some code snippets from the measurement class.


A new A-measurement is created as:

 AccelerationMeasurement A = new AccelerationMeasurement( bool is16Bit,
bool FullScale, short[] X, short[] Y, short[] Z, DateTime _Start); 

Where the X,Y,Z short arrays contains the 16-bit raw data read from the device. But this class contains some properties for refined data as well. Let's have a look at two of them:

// Returns mean acceleration for X, Y and Z axes. Includes static (gravity).
 public int[] Mean_XYZ { get { ...; } }  
// Returns Total mean acceleration.
public int Total_Mean { get { return tot; } }

One note here; the Total Mean Acceleration is the length of the total acceleration vector, calculated as: a<sub>t</sub> = f*sqrt(a<sub>x</sub><sup>2</sup>+a<sub>y</sub><sup>2</sup>+a<sub>z</sub><sup>2</sup>), where f is a calibration factor.

This class also contains properties for Max/Min, Frequency, Time, etc.

Moving On...

We'll continue setting up the device using the second constructor above and with the device's standard scale and frequency: 

memsParser.SET_SCALE_2G(); // ±2g
memsParser.SET_FREQ_40Hz(); // 40 Hz

We'll also use 12-bits right justified data blocks (most sensitive to small changes): 

memsParser.SET_MODE_12BIT(); // 12-bits, right

Now we are going to start the device and read 1s data 10 times and print it to Console:

memsParser.Start(); // Start Device in continuous mode 
AccelerationMeasurement Amsmt;
            for (int k = 0; k < 10; k++)
                // Wait one sec  (Collect data)
                // Get data
                Amsmt = memsParser.Get();
                // Print (Net) Total Mean Acceleration (mg)
                    String.Format("Measurement {0} = {1} mg",

The mean is the mean of all a<sub>t</sub> (see above) during the measurement interval (40 Hz so should be around 40 single measurements in the Amsmt class). 

Finally we stop the device and Dispose of the Parser class:

memsParser.Stop();    // Stop Device
memsParser.Dispose(); // Close SerialPort and release resources 

The code is filled with comments so if you thought this article was too brief, don't hesitate to have a look in the studio. Many a question marks will probably vanish along the way...  

Points of Interest 

I had a problem with my device - got "Unable to open Comport - incorrect parameter" errors in Hyperterminal, software provided with the device and my own code. This was probably due to some Windows XP problem with the provided driver.  

Anyway I took it so far as to rewrite the Parser to be used in conjunction with the RS232.vb class written by the manufacturer, thus avoiding the System.IO.Ports.SerialPort class. If anyone has the same problem, you can contact me and I'll be happy to send you the code. ST's support should get credit though, they eventually sent me a new device...    


  • 4/2/2009: First rev. published
  • 5/2/2009: Minor changes, added support for 12-bit right parsing 
  • 12/2/2009: Corrected formulae affecting AccMsmt class 


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Other Lund University
Sweden Sweden
Peder has a background in engineering physics and is currently PhD student in medical radiation physics at Lund University.

You may also be interested in...

Comments and Discussions

QuestionLIS3LV02DL Pin
palm_86998-Jun-09 10:14
memberpalm_86998-Jun-09 10:14 
GeneralRequest info for model of accelerometer Pin
pippopeppe5-May-09 1:05
memberpippopeppe5-May-09 1:05 
GeneralRe: Request info for model of accelerometer Pin
flmz_815-May-09 2:39
memberflmz_815-May-09 2:39 
GeneralRe: Request info for model of accelerometer Pin
pippopeppe5-May-09 23:24
memberpippopeppe5-May-09 23:24 
GeneralRe: Request info for model of accelerometer Pin
pippopeppe6-May-09 23:11
memberpippopeppe6-May-09 23:11 
GeneralRe: Request info for model of accelerometer Pin
flmz_817-May-09 0:30
memberflmz_817-May-09 0:30 
GeneralRe: Request info for model of accelerometer Pin
pippopeppe7-May-09 3:26
memberpippopeppe7-May-09 3:26 
GeneralRe: Request info for model of accelerometer Pin
flmz_817-May-09 4:44
memberflmz_817-May-09 4:44 
GeneralNice! Pin
Nils Thorell5-Feb-09 20:18
memberNils Thorell5-Feb-09 20:18 
GeneralRe: Nice! Pin
flmz_8112-Feb-09 4:51
memberflmz_8112-Feb-09 4:51 
GeneralGreat Pin
sam.hill5-Feb-09 5:39
membersam.hill5-Feb-09 5:39 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170924.2 | Last Updated 12 Feb 2009
Article Copyright 2009 by flmz_81
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid