Getting up to speed with accelerometers and Netduino

Sign in to queue


We've hear of accelerometers a thousand times, but just what are they, really? How do they work? And more importantly from a Coding4Fun perspective, just how can we build cool devices and apps with them?

Funny you should ask... How about a tutorial that talks about how accelerometers work, builds a Netduino hardware project and talks code too?

Accelerometers: ADXL345 Library and Netduino Tutorial


Whilst accelerometers may require a bit of understanding to use, it doesn't mean that they are difficult, or have any sort of black magic.

In this tutorial I will walk you through using your accelerometer to control a cube, we will see how different approaches change how the cube responds, and thus which approach is best for a given application.

What is an accelerometer?

An accelerometer is part of a family of motion sensors consisting of accelerometers, gyroscopes and compasses. In this tutorial we will be talking specifically about accelerometers, however in future tutorials we will bring in these other sensors, working up to a full internal measurement unit.

The accelerometer is a device which measures acceleration. Simple enough, but there are two different types of acceleration: static acceleration (tilt) and dynamic acceleration (movement).

The accelerometer outputs a measurement telling the user in what direction on its axis force is being applied. The good news is, the accelerometer picks up all kinds of movement, meaning you can get just about anything from it. The bad news is, the accelerometer picks up all kinds of movement, meaning you have no idea what your looking at!


The article provides a nice look at just how accelerometer works;


So here is our first problem with accelerometers, how to glean meaning from these sets of forces. With all this information about which way gravity is pointing and how the user is moving the device, it becomes difficult to get what we need from the sensor.

The second problem with accelerometers is that they are extremely noisy, and I don't mean they play loud music at 3am. Their reading is always fluctuating, they have natural resonant noise, they receive vibrations through the device that's holding them, all manor of things creep into our signal that we need to filter out.

So basically, we need a way to both understand the data coming from the accelerometer, and filter it so it is no longer noisy.

How do we solve that problem?

There are a few different ways of solving these problems, during the next section I shall walk you through the entire process of communicating with the an example sensor, getting some raw data, filtering the data, and using the output.

The solution.

There are many different makes of accelerometers out there, Analog Devices make very good accelerometers, as do Invensense. There are many many different types you can choose, and choosing the best one for your application requires a whole tutorial of its own! Therefore we will go with an accelerometer we use all the time here at Love Electronics, the ADXL345.

This funky little chip is a bargain. It is a triple axis digital accelerometer, with a good range of measurements (up to 16 g) with very good resolution (13 bit, 4 mg/LSB). Whilst most of these specifications are overkill for cube, it never hurts to have better data. The more accurate data we start with, the better our output will be.

If you want to follow along with this tutorial, you will need an ADXL345 Breakout Board and a netduino, along with a Windows PC with Visual Studio 2010 (express edition available free).

You will also need some way of connecting the serial port of the netduino to your Windows PC, as the netduino does not currently support sending serial data through the USB connection. I recommend using a USB to Serial Breakout Board.

From this point the article starts building some hardware and talking code;



Reading real data.

Great, now you have connected to the accelerometer, we need to read the data from the device. To do this use the copy the following code into your Main method in the Program.cs file, and read through the comments to see what each part of the code does.

// Create an instance of the ADXL345 accel.
ADXL345 accel = new ADXL345();
// Ensure that we are connected to the accel
// (this will throw an exception if the accel does not respond).
// Tell the accel we are interested in a range of +/- 2g.
accel.Range = 2;
// Tell the accel we want it to use full resolution.
accel.FullResolution = true;
// Enable the measurements on the device.
// Set the data rate to output at 50Hz

double yAxisGs, xAxisGs; // This stores the data received by the accel.
double yAxisRadians, xAxisRadians; // This stores the converted value in radians.

// The timer allows us to specify the frequency of our measurements, we have set
// the timer period to 20ms, which matches the rate we have set the accel
// to report at.
int timerPeriod = 20;
Timer timer = new Timer(delegate(object o)
    // Get the newest data from the accelerometer.

    // Get the acceleration from the two axis in G's.
    yAxisGs = accel.ScaledYAxisG;
    xAxisGs = accel.ScaledXAxisG;

    // We are only interested in values from -1g to +1g, trim other values.
    if (yAxisGs > 1) yAxisGs = 1; else if(yAxisGs < -1) yAxisGs = -1;
    if (xAxisGs > 1) xAxisGs = 1; else if (xAxisGs < -1) xAxisGs = -1;

    // Calculate the angle each of the axis is at using the Asin function.
    yAxisRadians = ExMath.Asin(yAxisGs);
    xAxisRadians = ExMath.Asin(xAxisGs);

    Debug.Print("X: " + xAxisRadians + "\t\tY: " + yAxisRadians);

}, null, 0, timerPeriod);

// Make sure the device runs the timer and doesnt end execution.
while (true)

If you try to run this, you will get a compilation error that Visual Studio cannot find ExMath, this is because it is in the LoveElectronics.Resources project, which was in the Love Electronics Accelerometer Zip File you downloaded, simply add this project to your solution as you did the accelerometer project, and do Add Reference...once more to get ExMath available to your application. Now simply add
using LoveElectronics.Resources;

as you did before and your application should now compile.

The article continues on showing how to filter the data and present it in visual form;


If you'd like to add accelerometer capabilities to your next Netduino project, or just interested in how they work, this article should be interesting reading.

Here’s a few more links you might find interesting:

The Discussion

Add Your 2 Cents