The Accelerometer class constructs objects that represent a single Accelerometer sensor attached to the physical board.

Supported Accelerometers:

This list will continue to be updated as more component support is implemented.

Parameters

  • General Options

    PropertyTypeValue/DescriptionDefaultRequired
    controllerstringANALOG, MPU6050, ADXL345, ADXL335, MMA7361. The Name of the controller to use“ANALOG”no

  • Analog Options (controller: "ANALOG")

    PropertyTypeValue/DescriptionDefaultRequired
    pinsArray of Strings[“A*”]. The String analog pins that X, Y, and Z (optional) are attached tononeyes
    sensitivityNumberVaries by device. This value can be identified in the device's datasheet.96 (Tinkerkit)no
    arefNumberVoltage reference. This is the value of the VCC pin5no
    zeroVNumber or Array0-1023. The analog input when at rest, perpendicular to gravity. When an array, specifies the zeroV for the individual axes.478no
    autoCalibrateBooleantrue, false. Whether to auto-calibrate the zeroV values. The device must be initialized with X/Y axes perpendicular to the earth, and the Z axis pointing to the sky.falseno

  • MPU6050 Options (controller: "MPU6050")

    PropertyTypeValue/DescriptionDefaultRequired
    sensitivityNumber16 bit value. The sensitivity for the +- 250 setting of this device16384no

  • MMA7361 Options (controller: "MMA7361")

    PropertyTypeValue/DescriptionDefaultRequired
    sleepPinNumber or StringThe digital pin that controls the sleep pin on the device. If you don't set this pin, you need to pull it up to Vcc with a 10k resistor.nullno

Shape

Property NameDescriptionRead Only
idA user definable id value. Defaults to a generated uidNo
zeroVThe current zeroV value (or values). May be different from initial values if auto-calibrated.No
pinsThe pins defined for X, Y, and Z.No
pitchThe pitch angle in degrees.Yes
rollThe roll angle in degrees.Yes
xValue of x axis in G forces.Yes
yValue of y axis in G forces.Yes
zValue of z axis in G forces.Yes
accelerationThe magnitude of the acceleration in G forces.Yes
inclinationThe incline of the device in degrees.Yes
orientationThe orientation of the device (-3, -2, -1, 1, 2, 3).Yes

Component Initialization

Analog

Simplified array of X and Y axis pins, all defaults:

new five.Accelerometer(["A0", "A1"]);

Options object with pins property, array of X and Y axis pins, all defaults:

new five.Accelerometer({
  pins: ["A0", "A1"]
});

Options object with pins property, array of X and Y axis pins:

new five.Accelerometer({
  pins: ["A0", "A1"],
  sensitivity: 96, // optional
  aref: 5,         // optional
  zeroV: 478       // optional
});
// Create an analog Accelerometer object:
//
//   - attach X and Y to "A0" and "A1" respectively
//
new five.Accelerometer({
  pins: ["A0", "A1"],
  sensitivity: 96, // optional
  aref: 5,         // optional
  zeroV: 478       // optional
});

LIS344AL

MPU6050

// Create an MPU-6050 Accelerometer object:
//
//  - attach SDA and SCL to the I2C pins on
//     your board (A4 and A5 for the Uno)
//  - specify the MPU6050 controller
new five.Accelerometer({
  controller: "MPU6050",
  sensitivity: 16384 // optional
});

MPU6050

ADXL345

// Create an ADXL345 Accelerometer object:
//
//  - attach SDA and SCL to the I2C pins on
//     your board (A4 and A5 for the Uno)
//  - specify the ADXL345 controller
new five.Accelerometer({
  controller: "ADXL345"
});

ADXL345

ADXL335

// Create an ADXL335 Accelerometer object:
//
//  - attach Xout, Yout and Zout to A0, A1, A2
//  - specify the ADXL335 controller
new five.Accelerometer({
  controller: "ADXL335",
  pins: ["A0", "A1", "A2"]
});

ADXL335

MMA7361

// Create an MMA7361 Accelerometer object:
//
// - attach Xo, Yo, and Zo to A0, A1, A2
// - specify the MMA7361 controller
// - optionally, specify the sleepPin
// - optionally, set it to auto-calibrate
// - optionally, set the zeroV values.
//    This can be done when retrieving them
//    after an autoCalibrate
new five.Accelerometer({
  controller: "MMA7361",
  pins: ["A0", "A1", "A2"],
  sleepPin: 13,
  autoCalibrate: true,
  // override the zeroV values if you know what
  // they are from a previous autoCalibrate
  zeroV: [320, 365, 295]
});

MMA7361

Usage

var five = require("johnny-five"),
    board = new five.Board();

board.on("ready", function() {

  var accelerometer = new five.Accelerometer({
    pins: ["A0", "A1"]
  });

  accelerometer.on("change", function() {
    console.log("X: %d", this.x);
    console.log("Y: %d", this.y);
  });
});

API

  • hasAxis(name) Does the device support the axis?

    if (accelerometer.hasAxis("z")) {
      console.log(accelerometer.z)
    }
    
  • enable() Enable the device and events (enabled by default). For devices that can be put to sleep (like the MMA7361), wake it up.

    accelerometer.enable();
    
  • disable() Disable the device and events. For devices that can be put to sleep (like the MMA7361), put it to sleep.

    accelerometer.disable();
    

Events

  • change The "change" event is emitted whenever any reading changes.

  • data The "data" event is fired as frequently as the user defined freq will allow in milliseconds.

Examples

Hi! The Johnny-Five community is building new projects every day. We made this newsletter to tell you about what's new, what's good, and what's next for Open Source robotics. Join us in exploring what we can make together.

Fork me on GitHub