Accelerometer

View Source

Step 1

Make a directory inside your "tessel-code" folder: enter mkdir accelerometer into your command line, then change directory into that folder: cd accelerometer

Step 2

Plug the accelerometer module into Tessel port A with the hexagon/icon side down and the electrical components on the top, then plug Tessel into your computer via USB.

Step 3

Install by typing npm install accel-mma84 into the command line.

Step 4

Save this code in a text file called accelerometer.js:

// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/

/*********************************************
This basic accelerometer example logs a stream
of x, y, and z data from the accelerometer
*********************************************/

var tessel = require('tessel');
var accel = require('accel-mma84').use(tessel.port['A']);

// Initialize the accelerometer.
accel.on('ready', function () {
    // Stream accelerometer data
  accel.on('data', function (xyz) {
    console.log('x:', xyz[0].toFixed(2),
      'y:', xyz[1].toFixed(2),
      'z:', xyz[2].toFixed(2));
  });

});

accel.on('error', function(err){
  console.log('Error:', err);
});

Step 5

In your command line, tessel run accelerometer.js
Watch x, y, and z values appear in your terminal! Move the Tessel module around to see acceleration along different axes.

Bonus: Change the code to make the accelerometer output information once per second (every 1000ms).

To see what else you can do with the accelerometer module, see the module docs here.

Step 6

What else can you do with a accelerometer module? Try a community-created project.


What are you making? Share your invention!

If you run into any issues you can check out the accelerometer forums.

Choose another module

Or if you're all set, move on to Wifi.

Fork on Github